DBMS 1-4 Exp
DBMS 1-4 Exp
DBMS 1-4 Exp
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
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:
Syntax:
Example Query:
This query will create a new database in SQL and name the database as my_database.
CREATE DATABASE college;
OUTPUT:
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:
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;
QUERY:
To ADD 2 columns AGE and COURSE to table Student.
ALTER TABLE Students ADD (AGE number(3),COURSE varchar(40));
OUTPUT:
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.
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.
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:
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:
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:
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:
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:
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:
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.
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:
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
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:
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:
OUTPUT:
SHORYA GARG 2100320100152 CSE-C
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
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
i) client_master
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
Q3:- On the basis of above two tables answer the following queries:
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.
1.
2.
3.
4.
5.
SHORYA GARG 2100320100152 CSE-C
6.
7.
SHORYA GARG 2100320100152 CSE-C
8.
SHORYA GARG 2100320100152 CSE-C