DBMS 1-4 Exp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

SHORYA GARG 2100320100152 CSE-C

EXPERIMENT 1
Aim:- CREATING ER DIAGRAMS USING CASE TOOLS

1.Draw an ER diagram for a small marketing company database, assuming your own data
requirements.
SHORYA GARG 2100320100152 CSE-C

2. A university registrar’s office maintains data about the following entities


(a) courses, including number, title, credits, syllabus and prerequisites;
(b) course offerings, including course number, year, semester section number,
instructor(s), timings and classroom;
(c) students, including student-id, name and program; and
(d) instructors, including identification number, name department and title.
Further the enrollment of students in courses and grades awarded to students in
each course they are enrolled for must be appropriately modeled.
Construct an ER diagram for the registrar’s office. Document all assumption that
you make about the mapping constraints.
SHORYA GARG 2100320100152 CSE-C

EXPERIMENT 2
Aim : - Execution of DDL Commands.
DDL (Data Definition Language) Statements: A data definition language (DDL) is a computer
language used to create and modify the structure of database objects in a database. These
database objects include views, schemas, tables, indexes, etc.
This term is also known as data description language in some contexts, as it describes the
fields and records in a database table. DDL commands are used for altering the database
structure such as creating new tables or objects along with all their attributes (data type,
table name, etc.).
Commonly used DDL in SQL querying are CREATE, ALTER, DROP, and TRUNCATE.
1) CREATE- Data Definition language (DDL): It is used to create the database or its objects
(like table, index, function, views, store procedure and triggers).
There are two CREATE statements available in SQL:

(i) CREATE DATABASE


(ii) CREATE TABLE

(i) CREATE DATABASE


A Database is defined as a structured set of data. So, in SQL the very first step to store the
data in a well-structured manner is to create a database. The CREATE DATABASE statement
is used to create a new database in SQL.

Syntax:

CREATE DATABASE database_name;


database_name: name of the database.

Example Query:
This query will create a new database in SQL and name the database as my_database.
CREATE DATABASE college;
OUTPUT:

(ii) CREATE TABLE


SHORYA GARG 2100320100152 CSE-C

We have learned above about creating databases. Now to store the data we need a table to
do that. The CREATE TABLE statement is used to create a table in SQL. We know that a table
comprises of rows and columns. So while creating tables we have to provide all the
information to SQL about the names of the columns, type of data to be stored in columns,
size of the data etc. Let us now dive into details on how to use CREATE TABLE statement to
create tables in SQL.

Syntax:

CREATE TABLE table_name


(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);

table_name: name of the table.


column1 name of the first column.
data_type: Type of data we want to store in the particular column.
For example, int for integer data.
size: Size of the data we can store in a particular column. For example if for a column we
specify the data_type as int and size as 10 then this column can store an integer number of
maximum 10 digits.
Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and
SUBJECT.
CREATE TABLE Students(
ROLL_NO int,
NAME varchar (20),
SUBJECT varchar (20)
);
SHORYA GARG 2100320100152 CSE-C

OUTPUT:

This query will create a table named Students. The ROLL_NO field is of type int and can store
an numeric value. The next two columns NAME and SUBJECT are of type varchar and can
store characters and the size 20 specifies that these two fields can hold maximum of 20
characters.

2) ALTER (ADD, DROP, MODIFY): ALTER statement is used to add, delete/drop or modify
columns in the existing table. An existing database object can be modified by the ALTER
statement. Using this command, the users can add up some additional column and drop
existing columns. Additionally, the data type of columns involved in a database table can be
changed by the ALTER command.
ALTER TABLE – ADD
ADD is used to add columns into the existing table. Sometimes we may require to add
additional information, in that case we do not require to create the whole database again,
ADD comes to our rescue.
Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);
ALTER TABLE – DROP
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the
table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
SHORYA GARG 2100320100152 CSE-C

ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified at
once.
*Syntax may vary slightly in different databases.
Syntax (Oracle, MySQL, MariaDB):
ALTER TABLE table_name MODIFY column_name column_type;

Syntax (SQL Server):


ALTER TABLE table_name ALTER COLUMN column_name column_type;

QUERY:
To ADD 2 columns AGE and COURSE to table Student.
ALTER TABLE Students ADD (AGE number(3),COURSE varchar(40));
OUTPUT:

MODIFY column COURSE in table Student


ALTER TABLE Students MODIFY COURSE varchar(20);
After running the above query maximum size of Course Column is reduced to 40 from 20.
OUTPUT:

DROP column COURSE in table Student.


ALTER TABLE Students DROP COLUMN COURSE;
OUTPUT:
SHORYA GARG 2100320100152 CSE-C

3) DROP
DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view.
A DROP statement in SQL removes a component from a relational database management
system (RDBMS).
Syntax:
DROP object object_name
Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.
DROP DATABASE database_name;
database_name: Name of the database to be deleted.

4) TRUNCATE
By using truncate command, users can remove table content, but structure of the table is
kept. In simple language, it removes all the records from the table structure. Users can’t
remove data partially through this command. In addition to this, every space allocated for
the data is removed by truncate command
Syntax:
TRUNCATE TABLE table_name;
table_name: Name of the table to be truncated.

DROP vs. TRUNCATE


Truncate is normally ultra-fast and it’s ideal for deleting data from a temporary table.
Truncate preserves the structure of the table for future use, unlike drop table where the
table is deleted with its full structure.
Table or Database deletion using DROP statement cannot be rolled back, so it must be used
wisely.
SHORYA GARG 2100320100152 CSE-C

6) RENAME
Sometimes we may want to rename our table to give it a more relevant name. For this
purpose we can use ALTER TABLE to rename the name of table.

*Syntax may vary in different databases.


Syntax (Oracle,MySQL,MariaDB):
ALTER TABLE table_name RENAME TO new_table_name;

Columns can be also be given new name with the use of ALTER TABLE.
Syntax (MySQL, Oracle):
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

QUERY:
Change the name of column NAME to FIRST_NAME in table Student.
ALTER TABLE Students RENAME COLUMN NAME TO FIRST_NAME;
OUTPUT:

Change the name of the table Student to Student_Details


ALTER TABLE Students RENAME TO Student_Details;
OUTPUT:
SHORYA GARG 2100320100152 CSE-C

EXPERIMENT 3
Aim : Execution of DML Commands.
DML (Data Manipulation Language): A data manipulation language (DML) includes
commands permitting users to manipulate data in a database. This manipulation involves
inserting data into database tables, retrieving existing data, deleting data from existing
tables and modifying existing data. DML is mostly incorporated in SQL database
List of DML Commands in SQL
Here is a shortlist of all DML commands and their specific functions in the SQL programming
language.
SELECT: Command to fetch data or values from the database
INSERT: Command to add new or fresh value to the database
UPDATE: Command to change or update the present/existing data to a newer value inside
the database
DELETE: Command to remove or delete the values or data information from the database’s
current table

1) SELECT: Select is the most commonly used statement in SQL. The SELECT Statement in
SQL is used to retrieve or fetch data from a database. We can fetch either the entire table or
according to some specified rules. The data returned is stored in a result table. This result
table is also called result-set.
With the SELECT clause of a SELECT command statement, we specify the columns that we
want to be displayed in the query result and, optionally, which column headings we prefer
to see above the result table.
Capabilities of the SELECT Statement
Data retrieval from data base is done through appropriate and efficient use of SQL. Three
concepts from relational theory encompass the capability of the SELECT statement:
projection, selection, and joining.
Projection: A project operation selects only certain columns (fields) from a table. The result
table has a subset of the available columns and can include anything from a single column to
all available columns.

Selection: A select operation selects a subset of rows (records) in a table (relation) that
satisfy a selection condition. The ability to select rows from out of complete result set is
called Selection. It involves conditional filtering and data staging. The subset can range from
no rows, if none of the rows satisfy the selection condition, to all rows in a table.
Joining: A join operation combines data from two or more tables based on one or more
common column values. A join operation enables an information system user to process the
SHORYA GARG 2100320100152 CSE-C

relationships that exist between tables. The join operation is very powerful because it allows
system users to investigate relationships among data elements that might not be
anticipated at the time that a database is designed.

Sample Table:

Basic Syntax:
SELECT column1, column2 FROM table_name
column1, column2: names of the fields of the table
table_name: from where we want to fetch

This query will return all the rows in the table with fields column1, column2.
To fetch the entire table or all the fields in the table:
SELECT * FROM table_name;

2) INSERT INTO: The INSERT INTO statement of SQL is used to insert a new row in a table.
There are two ways of using INSERT INTO statement for inserting rows:
Only values: First method is to specify only the value of data to be inserted without the
column names.
INSERT INTO table_name VALUES (value1, value2, value3,);
table_name: name of the table.
value1, value2,: value of first column, second column,… for the new record

Column names and values both: In the second method we will specify both the columns
which we want to fill and their corresponding values as shown below:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2,
value3,..);
table_name: name of the table.
column1: name of first column, second column …
value1, value2, value3 : value of first column, second column,… for the new record

Queries:
FIRST CREATE TABLE BEFORE INSERTING THE VALUE
CREATE TABLE STUDENT( ROLL_NO INT,NAME VARCHAR(10),AGE INT, ADDRESS
VARCHAR(10));
SHORYA GARG 2100320100152 CSE-C

OUTPUT:

Method 1 (Inserting only values) :


INSERT INTO Student VALUES('1','ROHIT','18','DELHI');
INSERT INTO Student VALUES('2','MOHIT','18','GURGAON');
INSERT INTO Student VALUES('3','SUJIT','20','ROHTAK');
INSERT INTO Student VALUES('4','AKASH','20','DELHI');
INSERT INTO Student VALUES('5','HARSH','19','DEHRADUN');
OUTPUT:

SELECT * FROM STUDENT;


Output:
SHORYA GARG 2100320100152 CSE-C

Method 2 (Inserting values in only specified columns):


INSERT INTO Student (ROLL_NO, NAME, Age, ADDRESS)
VALUES('6','RAJEEV','19','BANGALORE');
Output:

SELECT * FROM STUDENT;


Output:

Note: The columns for which the values are not provided are filled by null which is the
default values for those columns.
3) UPDATE Statement: The UPDATE statement in SQL is used to update the data of an
existing table in database. We can update single columns as well as multiple columns using
UPDATE statement as per our requirement.
Basic Syntax:
UPDATE table_name SET column1 = value1, column2 = value2,...
WHERE condition;
SHORYA GARG 2100320100152 CSE-C

table_name: name of the table


column1: name of first, second, third column....
value1: new value for first, second, third column....
condition: condition to select the rows for which the values of columns needs to be
updated.
NOTE: In the above query the SET statement is used to set new values to the particular
column and the WHERE clause is used to select the rows for which the columns are needed
to be updated. If we have not used the WHERE clause then the columns in all the rows will
be updated. So the WHERE clause is used to choose the particular rows.

Table:
SELECT * FROM STUDENT;
Output:

Example Queries
Updating single column: Update the column NAME and set the value to ‘PRATIK’ in all the
rows where Age is 20.
UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;
Output:

SELECT * FROM STUDENT;


Output:
SHORYA GARG 2100320100152 CSE-C

Updating multiple columns: Update the columns NAME to ‘SAURABH’ and ADDRESS to
‘SIKKIM’ where ROLL_NO is 1.
UPDATE Student SET NAME = 'SAURABH', ADDRESS = 'SIKKIM' WHERE ROLL_NO = 1;
Output:

SELECT * FROM STUDENT;


Output:

Note: For updating multiple columns we have used comma (,) to separate the names and
values of two columns.

4) DELETE Statement: The DELETE Statement in SQL is used to delete existing records from
a table. We can delete a single record or multiple records depending on the condition we
specify in the WHERE clause.
SHORYA GARG 2100320100152 CSE-C

Basic Syntax:
DELETE FROM table_name WHERE some_condition;
table_name: name of the table
some_condition: condition to choose particular record.
Note: We can delete single as well as multiple records depending on the condition we
provide in WHERE clause. If we omit the WHERE clause then all of the records will be
deleted and the table will be empty.

Example Queries:
Deleting single record: Delete the rows where NAME = ‘SAURABH’. This will delete only the
first row.
DELETE FROM Student WHERE NAME = 'SAURABH';
Output:

SELECT * FROM STUDENT;


Output:

Deleting multiple records: Delete the rows from the table Student where Age is 20. This will
delete 2 rows (third row and fifth row).
DELETE FROM Student WHERE Age = 20;
Output:
SHORYA GARG 2100320100152 CSE-C

Output:
SELECT * FROM STUDENT;

Delete all of the records: There is one queries to do this as shown below,
Query: DELETE FROM Student;
Output:
SHORYA GARG 2100320100152 CSE-C

EXPERIMENT 4
Aim: - Execution of DQL commands and Sorting of Data.

Using the WHERE SQL clause


WHERE clause is used to specify/apply any condition while retrieving, updating or deleting
data from a table. This clause is used mostly with SELECT, UPDATE and DELETE query.
When we specify a condition using the WHERE clause then the query executes only for
those records for which the condition specified by the WHERE clause is true.
Example
First create the following table with mentioned columns:
CREATE TABLE STUDENT(s_id int, name varchar(10), age int, address varchar(15));
OUTPUT:

Now insert the values into the table:


INSERT INTO STUDENT VALUES('101', 'Saurabh', '18', 'Chennai');
INSERT INTO STUDENT VALUES('102', 'Rohit', '20', 'Delhi');
INSERT INTO STUDENT VALUES('103', 'Vineet', '19', 'Bangalore');
INSERT INTO STUDENT VALUES('104', 'Ankit', '22', 'Mumbai');

OUTPUT:
SHORYA GARG 2100320100152 CSE-C

Now we will use the SELECT statement to display data of the table, based on a condition,
which we will add to our SELECT query using WHERE clause.

Let's write a simple SQL query to display the record for student with s_id as 101.
SELECT s_id, name, age, address FROM student WHERE s_id = 101;
Following will be the result of the above query.
Output:

Applying condition on Text Fields


In the above example we have applied a condition to an integer value field, but what if we
want to apply the condition on name field. In that case we must enclose the value in single
quote ' '. Some databases even accept double quotes, but single quotes is accepted by all.

SELECT s_id, name, age, address FROM student WHERE name = 'Vineet';
Following will be the result of the above query.
Output:
SHORYA GARG 2100320100152 CSE-C

Operators for WHERE clause condition


Following is a list of operators that can be used while specifying the WHERE clause
condition.

Operator Description
= Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
BETWEEN Between a specified range of values
LIKE This is used to search for a pattern in value.
IN In a given set of values
AND Displays a record if both the first condition and the second condition are
true
OR Displays a record if either the first condition or the second condition is true
Consider these queries:
SELECT s_id, name, age, address FROM student WHERE age >=19;
Output:

SELECT s_id, name, age, address FROM student WHERE address='Delhi' and address =
'Mumbai';
Output:
SHORYA GARG 2100320100152 CSE-C

SELECT s_id, name, age, address FROM student WHERE address='Delhi' or address =
'Mumbai';
Output:

SELECT s_id, name, age, address FROM student WHERE address='Delhi' and (Age= 20 or
Age= 22);
Output:

SELECT s_id, name, age, address FROM student WHERE Age BETWEEN 19 AND 22;
Output:

SQL LIKE clause


LIKE clause is used in the condition in SQL query with the WHERE clause. LIKE clause
compares data with an expression using wildcard operators to match pattern given in the
condition.
Wildcard operators
There are two wildcard operators that are used in LIKE clause.
Percent sign %: represents zero, one or more than one character.
Underscore sign _: represents only a single character.

Example of LIKE clause


SHORYA GARG 2100320100152 CSE-C

SELECT * FROM Student WHERE name LIKE 'A%';


The above query will return all records where name starts with character 'A'.
Output:

Using _ and %
SELECT * FROM Student WHERE name LIKE '_o%';
The above query will return all records from Student table where name contain 'o' as second
character.
Output:

Using % only
SELECT * FROM Student WHERE name LIKE '%t';
The above query will return all records from Student table where name contain 't' as last
character.
Output:

ORDER BY Clause
Order by clause is used with SELECT statement for arranging retrieved data in sorted order.
The Order by clause by default sorts the retrieved data in ascending order. To sort the data
in descending order DESC keyword is used with Order by clause.
SHORYA GARG 2100320100152 CSE-C

Syntax of Order By
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;
First create the following table with mentioned columns:
CREATE TABLE EMPLOYEE(eid int, Name varchar(10), age int, salary number);
OUTPUT:

Now insert the values into the table:


INSERT INTO EMPLOYEE VALUES('101', 'Anubhav', '22', '15000');
INSERT INTO EMPLOYEE VALUES('102', 'Gaurav', '25', '18000');
INSERT INTO EMPLOYEE VALUES('103', 'Rohan', '30', '16000');
INSERT INTO EMPLOYEE VALUES('104', 'Prabhav', '32', '20000');
INSERT INTO EMPLOYEE VALUES('105', 'Akash', '27', '15000');

OUTPUT:
SHORYA GARG 2100320100152 CSE-C

Consider Default order:


SELECT * FROM EMPLOYEE ORDER BY salary;
Output:

Using Order by DESC


SELECT * FROM EMPLOYEE ORDER BY salary DESC;
Output:

DISTINCT keyword
The distinct keyword is used with SELECT statement to retrieve unique values from the
table. Distinct removes all the duplicate records while retrieving records from any table in
the database.
Syntax for DISTINCT Keyword
SELECT DISTINCT column-name FROM table-name;
Example using DISTINCT Keyword
Consider the following Employee table. As you can see in the table below, there is employee
name, along with employee salary and age.
In the table below, multiple employees have the same salary, so we will be using DISTINCT
keyword to list down distinct salary amount, that is currently being paid to the employees.
Consider the following Employee table
eid name age salary
101 Anubhav 22 15000
SHORYA GARG 2100320100152 CSE-C

102 Gaurav 25 18000


103 Rohan 30 16000
104 Prabhav 32 20000
105 Akash 27 15000

SELECT DISTINCT salary FROM Employee;


The above query will return only the unique salary from Emp table.
Output:

SQL AND and OR Operator


The AND and OR operators are used with the WHERE clause to make more precise
conditions for fetching data from database by combining more than one condition together.

AND operator
AND operator is used to set multiple conditions with the WHERE clause, alongside, SELECT,
UPDATE or DELETE SQL queries.
Example of AND operator
Consider the following Employee table
eid name age salary
101 Anubhav 22 15000
102 Gaurav 25 18000
103 Rohan 30 16000
104 Prabhav 32 20000
105 Akash 27 15000

SELECT * FROM Employee WHERE salary > 10000 AND age <= 25;
The above query will return records where salary is greater than 10000 and age less than or
equal to 25. We have used the AND operator to specify two conditions with WHERE clause.
SHORYA GARG 2100320100152 CSE-C

Output:

OR operator
OR operator is also used to combine multiple conditions with WHERE clause. The only
difference between AND and OR is their behaviour.
When we use AND to combine two or more than two conditions, records satisfying all the
specified conditions will be there in the result.
But in case of OR operator, atleast one condition from the conditions specified must be
satisfied by any record to be in the result set.

SELECT * FROM Employee WHERE salary < 10000 OR age > 25;
The above query will return records where either salary is less than 10000 or age is greater
than 25.
Output:
SHORYA GARG 2100320100152 CSE-C

QUESTIONS

Q1. Create the following tables:

i) client_master

columnname datatype size client_no varchar2 6


name varchar2 20
address1 varchar2 30
address2 varchar2 30
city varchar2 15
state varchar2 15
pincode number6
bal_due number10,2

ii) Product_master
Columnname datatype sizeProduct_no varchar2 6
Description varchar2 20
Profit_percent number 10,2
Unit_measure varchar2 10
Qty_on_hand number 10
Reoder_lvl number 10
Sell_price number 10
Cost_price number 10
SHORYA GARG 2100320100152 CSE-C

Q2- Insert the following data into their respective tables:

Clientno Name city pincode state bal.due

0001 Ivan Bombay 400054 Maharashtra 15000


0002 Vandana Madras 780001 Tamilnadu 0
0003 Pramada Bombay 400057 Maharashtra 5000
0004 Basu Bombay 400056 Maharashtra 0
0005 Ravi Delhi 100001 Delhi 2000
0006 Rukmini Bombay 400050 Maharashtra 0
SHORYA GARG 2100320100152 CSE-C
SHORYA GARG 2100320100152 CSE-C

Data for Product Master:

Product No. Desciption Profit % Unit Qty Reorder Sell Cost


Percent measured on hand lvl price
price

P00001 1.44floppies 5 piece 100 20 525 500


P03453 Monitors 6 piece 10 3 12000 11200
P06734 Mouse 5 piece 20 5 1050 500
P07865 1.22 floppies 5 piece 100 20 525 500
P07868 Keyboards 2 piece 10 3 3150 3050
P07885 CD Drive 2.5 piece 10 3 5250 5100
P07965 540 HDD 4 piece 10 3 8400 8000
P07975 1.44 Drive 5 piece 10 3 1050 1000
P08865 1.22 Drive 5 piece 2 3 1050 1000
SHORYA GARG 2100320100152 CSE-C

Q3:- On the basis of above two tables answer the following queries:

i) Find out the names of all the clients.


ii) Retrieve the list of names and cities of all the clients.
iii) List the various products available from the product_master table.
iv) List all the clients who are located in Bombay.
v) Display the information for client no 0001 and 0002.
vi) Find the products with description as ‘1.44 drive’ and ‘1.22 Drive’.
vii) Find all the products whose sell price is greater then 5000.
viii) Find the list of all clients who stay in in city ‘Bombay’ or city ‘Delhi’ or ‘Madras’.
ix) Find the product whose selling price is greater than 2000 and less than or equal to 5000.
x) List the name, city and state of clients not in the state of ‘Maharashtra’.

i)

ii)

iii)
SHORYA GARG 2100320100152 CSE-C

iv)

v)

vi)
SHORYA GARG 2100320100152 CSE-C

vii)

viii)

ix)

x)

Que.4 Using the table client master and product master answer the following queries.

i. Change the selling price of ‘1.44 floppy drive to Rs.1150.00


ii. Delete the record with client 0001 from the client master table.
iii. Change the city of client_no’0005’ to Bombay.
iv. Change the bal_due of client_no ‘0001, to 1000.
v. Find the products whose selling price is more than 1500 and also find the new selling price
as original selling price *15.
vi. Find out the clients who stay in a city whose second letter is a.
vii. Find out the name of all clients having ‘a’ as the second letter in their names.
viii. List the products in sorted order of their description.
SHORYA GARG 2100320100152 CSE-C

1.

2.

3.

4.

5.
SHORYA GARG 2100320100152 CSE-C

6.

7.
SHORYA GARG 2100320100152 CSE-C

8.
SHORYA GARG 2100320100152 CSE-C

You might also like