Dbms Compelete Manual
Dbms Compelete Manual
1
Aim: 1. Design and Develop SQL DDL statements which demonstrate the
use of SQL objects such as Table, View, Index, Sequence, Synonym,
different constraints etc.
2. Write at least 10 SQL queries on the suitable database application using
SQL DML statements.
Software and Hardware Requirements: 1. Intel i3,3.3GHz,4gb ram.
2.Linux OS.
3.Terminal (CLI).
4.MySQL
Theory:
DATA DEFINITION LANGUAGE (DDL) QUERIES
DDL : Data Definition Language (DDL) statements are used to define the
database structure or schema.
Data Definition Language (DDL) are used different statements :
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
Create Database : From the MySQL command line, enter the command
CREATE DATABASE <DATABASENAME>;
Replace <DATABASENAMEs> with the name of your database.
For example, to create a database pune city, you might enter
CREATE DATABASE pune;
Select your database : Once the database has been created, you will
need to select it in order to begin editing it. Enter the command
USE pune;
You will see the message Database changed, letting you know that your
active database is now pune.
To Display a list of your available databases: Enter the command
SHOW DATABASES;
Create table : We define an SQL relation by using the CREATE TABLE
command. The following command creates a relation department in the
database.
example:
CREATE TABLE department(dept name VARCHAR(20),building
VARCHAR(15),budget INT(12));
Insert values in table : A newly created relation is empty initially. We
can use the insert command to load data into the relation. For example, if
we wish to insert the fact that there is an instructor named Smith in the
Biology department with instructor id 10211 and a salary of $66,000, we
write:
INSERT into instructor values (10211, ’Smith’, ’Biology’, 66000);
Drop table : To remove a relation from an SQL database, we use the
drop table command. The drop table command deletes all information
about the dropped relation from the database.
example: DROP TABLE <table_name>
Alter Table : We use the alter table command to add attributes to an
existing relation. All tuples in the relation are assigned null as the value
for the new attribute. The form of the alter table command is
alter table r add AD;
where r is the name of an existing relation, A is the name of the attribute
to be added, and D is the type of the added attribute. We can drop
attributes from a relation by the command
View : SQL allows a “virtual relation” to be defined by a query, and the
relation conceptually contains the result of the query. The virtual relation
is not precomputed and stored, but instead is computed by executing the
query whenever the virtual relation is used. Any such relation that is not
part of the logical model, but is made visible to a user as a virtual relation,
is called a view. To create view we use following command :
create view <view_name> as <query_expression>;
where <query_expression> is any legal query expression. The view name
is represented by v.
Create Index: A database index is a data structure that improves the
speed of operations in a table. Indexes can be created using one or more
columns, providing the basis for both rapid random lookups and efficient
ordering of access to records.
Simple and Unique Index: You can create a unique index on a table. A
unique index means that two rows cannot have the same index value.
Here is the syntax to create an Index on a table.
CREATE UNIQUE INDEX index_name ON table_name ( column1,
column2,...);
Conclusion: In this assignment, we have studied and demonstrated
various DDL statements in SQL.
// Output:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| customers |
| information_schema |
| mysql |
| performance_schema |
| persinsinfo |
| sakila |
| sys |
| teainds |
| world |
+--------------------+
9 rows in set (0.03 sec)
mysql> create database T3
-> ;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| customers |
| information_schema |
| mysql |
| performance_schema |
| persinsinfo |
| sakila |
| sys |
| t3 |
| teainds |
| world |
+--------------------+
10 rows in set (0.00 sec)
mysql> use t3
Database changed
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+--------------+
| Tables_in_t3 |
+--------------+
| persons |
+--------------+
1 row in set (0.02 sec)
mysql> insert into persons values(1, 'NIlesh', 'sharma','Nashik');
Query OK, 1 row affected (0.01 sec)
mysql> insert into persons values(2, 'Anushka', 'Sharma','Mumbai');
Query OK, 1 row affected (0.00 sec)
mysql> insert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
Query OK, 1 row affected (0.01 sec)
mysq>linsert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
mysql> update persons set firstname='Aishwarya' where id=2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+
| id | firstname | lastname | city |
+----+-----------+----------+------------+
| 1 | NIlesh | sharma | Nashik |
| 2 | Aishwarya | Sharma | Mumbai |
| 3 | Gayatri | Jadhav | Chalisgaon |
+----+-----------+----------+------------+
3 rows in set (0.00 sec)
mysql> delete from persons where id=1;
Query OK, 1 row affected (0.00 sec)
mysql> alter table persons add emailid varchar(255);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe persons;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| emailid | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table persons drop emailid;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table persons rename column id to rollno
-> ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe persons;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| rollno | int | NO | PRI | NULL | |
| firstname | varchar(255) | YES | | NULL | |
| lastname | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> truncate table persons;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from persons;
Empty set (0.00 sec)
mysql> drop table persons;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.01 sec)
5 rows in set (0.00 sec)
mysql> alter table persons add emailid varchar(255);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+---------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+---------+
| 1 | NIlesh | sharma | Nashik | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+---------+
5 rows in set (0.00 sec)
mysql> update persons set emailid="[email protected]"
where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname
-> ;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 2 | Anushka | Sharma | Mumbai | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname DESC
-> ;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by firstname;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 2 | Anushka | Sharma | Mumbai | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> select * from persons order by id DESC;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 5 | Durgesh | Agrawal | Manmad | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 1 | NIlesh | sharma | Nashik | [email protected] |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> create index i1 on persons (id, firstname);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> help
mysql> help auto_increment
Name: 'AUTO_INCREMENT'
Description:
The AUTO_INCREMENT attribute can be used to generate a unique identity
for new rows:
Examples:
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;
mysql> alter table persons modify column id int AUTO_INCREMENT;
Query OK, 5 rows affected (1.51 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from persons;
+----+-----------+----------+------------+----------------------------+
| id | firstname | lastname | city | emailid |
+----+-----------+----------+------------+----------------------------+
| 1 | NIlesh | sharma | Nashik | [email protected] |
| 2 | Anushka | Sharma | Mumbai | NULL |
| 3 | Sandip | Kadus | Ahamadnagr | NULL |
| 4 | Dhanshree | Bhusare | Dhamori | NULL |
| 5 | Durgesh | Agrawal | Manmad | NULL |
+----+-----------+----------+------------+----------------------------+
5 rows in set (0.00 sec)
mysql> INSERT INTO Persons (FirstName,LastName)
values('rohit','sharma');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Persons (FirstName,LastName)
values('rohit','sharma');
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(id)
);
mysql> create table persons (id int, firstname varchar(255), lastname
varchar(255), city varchar(255), primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into persons values(3, 'Gayatri', 'Jadhav','Chalisgaon');
Query OK, 1 row affected (0.00 sec)
mysql> select * from persons;
+----+-----------+----------+------------+
| id | firstname | lastname | city |
+----+-----------+----------+------------+
| 1 | NIlesh | sharma | Nashik |
| 2 | Anushka | Sharma | Mumbai |
| 3 | Gayatri | Jadhav | Chalisgaon |
+----+-----------+----------+------------+
3 rows in set (0.00 sec)
mysql> CREATE TABLE Orders (
-> OrderID int NOT NULL,
-> OrderNumber int NOT NULL,
-> PersonID int,
-> PRIMARY KEY (OrderID),
-> FOREIGN KEY (PersonID) REFERENCES Persons(id)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into orders values (1,22546,2);
mysql> select * from orders;
+---------+-------------+----------+
| OrderID | OrderNumber | PersonID |
+---------+-------------+----------+
| 1| 22546 | 2|
| 2| 22546 | 1|
| 3| 22546 | 3|
+---------+-------------+----------+
3 rows in set (0.00 sec)
mysql> select firstname , OrderID, OrderNumber from persons inner join
orders on persons.id=orders.personid
-> ;
+-----------+---------+-------------+
| firstname | OrderID | OrderNumber |
+-----------+---------+-------------+
| NIlesh | 2| 22546 |
| Anushka | 1| 12345 |
| Gayatri | 3| 53214 |
+-----------+---------+-------------+
3 rows in set (0.00 sec)
mysql> select firstname , OrderID, OrderNumber from persons inner join
orders on persons.id=orders.personid
-> ;
+-----------+---------+-------------+
| firstname | OrderID | OrderNumber |
+-----------+---------+-------------+
| NIlesh | 2| 22546 |
| Anushka | 1| 12345 |
| Gayatri | 3| 53214 |
+-----------+---------+-------------+
3 rows in set (0.00 sec)
mysql> select * from persons inner join orders on
persons.id=orders.personid;
+----+-----------+----------+------------+---------+-------------+----------+
| id | firstname | lastname | city | OrderID | OrderNumber | PersonID |
+----+-----------+----------+------------+---------+-------------+----------+
| 1 | NIlesh | sharma | Nashik | 2| 22546 | 1|
| 2 | Anushka | Sharma | Mumbai | 1| 12345 | 2|
| 3 | Gayatri | Jadhav | Chalisgaon | 3| 53214 | 3|
+----+-----------+----------+------------+---------+-------------+----------+
3 rows in set (0.00 sec)
mysql> create view v1 as select firstname, city from persons;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from v1;
+-----------+------------+
| firstname | city |
+-----------+------------+
| NIlesh | Nashik |
| Anushka | Mumbai |
| Gayatri | Chalisgaon |
+-----------+------------+
3 rows in set (0.00 sec)
Assignment No. 2
Aim: Design at least 10 SQL queries for suitable database application
using SQL DML statements: all types of Join, Sub-Query and View.
Software and Hardware Requirements: 1. Intel i3,3.3GHz,4gb ram.
2.Linux OS.
3.Terminal (CLI).
4.MySQL
Theory:
Introduction to Joins: An SQL JOIN clause is used to combine rows from two
or more tables, based on a common field between them.
Types of Join:
1. JOIN: Return rows when there is at least one match in both tables.
SQL JOIN Syntax:
SELECT column_name(s)
FROM table_name1,table_name2
WHERE table_name1.column_name=table_name2.column_name
2. LEFT JOIN: Return all rows from the left table, even if there are no
matches in the right table.
SQL LEFT JOIN Syntax:
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
3. RIGHT JOIN: Return all rows from the right table, even if there are no
matches in the left table.
SQL RIGHT JOIN Syntax:
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
4. FULL JOIN: Return rows when there is a match in one of the tables
SQL FULL JOIN Syntax:
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Types of Subqueries
Single Row Sub Query: Sub query which returns single row output. They
mark the usage of single row comparison operators, when used in WHERE
conditions.
Multiple row sub query: Sub query returning multiple row output. They
make use of multiple row comparison operators like IN, ANY, ALL. There
can be sub queries returning multiple columns also.
Correlated Sub Query: Correlated subqueries depend on data provided by
the outer query.Thistype of subquery also includes subqueries that use
the EXISTS operator to test the existence of data rows satisfying specified
criteria.
Example:
MongoDB
Scalable High-Performance Open-source, Document-orientated database.
• Built for Speed
• Rich Document based queries for Easy readability.
• Full Index Support for High Performance.
• Replication and Failover for High Availability.
• Auto Sharding for Easy Scalability.
• Map / Reduce for Aggregation.
Advantages of MongoDB
•Schema less : Number of fields, content and size of the document can be
differ from one
document to another.
•No complex joins
•Data is stored as JSON style
•Index on any attribute
•Replication and High availability
Mongo DB Terminologies for RDBMS concepts
RDMS MongoDB
Database Database
Table,View Collection
Row Document(Json,Bson)
Column Field
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard