DBMS Lab Manual PCCCS591

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

Database management System Lab manual

(PCCCS591)

2022-2023

5th Year

Department of CSE (AIML)/ CSBS

IEM Kolkata

Prof. Sumana Sinha

&

Prof. (Dr.) Moumita Roy


SQL (STRUCTURED QUERY LANGUAGE)

What is SQL?

It is a domain specific language and is declarative language.

What Can SQL do?

● SQL can execute queries against a database


● SQL can retrieve data from a database
● SQL can insert records in a database
● SQL can update records in a database
● SQL can delete records from a database
● SQL can create new databases
● SQL can create new tables in a database
● SQL can create stored procedures in a database
● SQL can create views in a database
● SQL can set permissions on tables, procedures, and views

Data types in SQL.

INT used for columns which will store integer values.

FLOAT used for columns which will store float values.

DOUBLE used for columns which will store float values.

VARCHAR used for columns which will be used to store characters and
integers, basically a string.

CHAR used for columns which will store char values(single character).

DATE used for columns which will store date values.

TEXT used for columns which will store text which is generally long in
length.
Types of SQL Commands:

1. Data definition commands, (DDL) deals with the schema.


● Create
● Alter
● Drop
● Truncate
● Rename

2. Data manipulation commands (DML)


● Select
● Insert
● Update
● Delete
3. Data Control language (DCL)
● Grant
● Revoke
4. Transaction Control language (TCL)
● Commit
● Rollback
● Savepoint
5. Constraint
● Primary key
● Foreign key
● Check
● Unique
● Default
● Not Null

SQL Data type


INT: used for columns which will store integer values.

FLOAT: used for columns which will store float values.

DOUBLE: used for columns which will store float values.

VARCHAR used for columns which will be used to store characters and
integers, basically a string.

CHAR: used for columns which will store char values(single character).

DATE: used for columns which will store date values.

TEXT: used for columns which will store text which is generally long
in length. For example, if

you create a table for storing profile information of a social networking


website, then for

about me section you can have a column of type TEXT.

Assignment Creation of table 1

Data definition commands (DDL)

Create Command

CREATE TABLE <TABLE_NAME> (

column_name_1 datatype_1,

column_name_2 datatype_2

…………………………………

column_name_n datatype_n

);
SQL: ALTER Command is used

To alter command is used for altering the table structure, such as,

● ·to add a column to existing table


● ·to rename any existing column
● ·to change datatype of any column or to modify its size.
● ·to drop a column from the table

ALTER Command: Add a new Column

ALTER TABLE table_name ADD( column_name datatype);

ALTER Command: Add multiple new Columns

ALTER TABLE table_name ADD( column_name1 datatype1,


column-name2 datatype2, )

ALTER Command: Add Column with default value

ALTER command can add a new column to an existing table with a


default value too. The default value is used when no value is inserted
in the column. Following is the syntax,

ALTER TABLE table_name ADD( column-name1 datatype1


DEFAULT some_value)

ALTER Command: Modify an existing Column

ALTER command can also be used to modify data type of any existing
column. Following is the

syntax,

ALTER TABLE table_name modify( column_name datatype);


ALTER Command: Rename a Column

ALTER command you can rename an existing column. Following is the


syntax,

ALTER TABLE table_name RENAME old_column_name TO


new_column_name;

ALTER Command: Drop a Column

ALTER command can also be used to drop or remove columns.


Following is the syntax,

ALTER TABLE table_name DROP( column_name);

TRUNCATE command removes all the records from a table. But


this command will not destroy the table's structure. When we use

TRUNCATE command on a table its (auto-increment) primary key


is also initialized. Following is its syntax,

TRUNCATE TABLE table_name

DROP command

DROP command completely removes a table from the database. This


command will also destroy the table structure and the data stored in it.
Following is its syntax,

DROP TABLE table_name

RENAME query, it can be used to set a new name for any existing
table. Following is the syntax,

RENAME TABLE old_table_name to new_table_name

ALTER Command: Rename a Column


Using ALTER command you can rename an existing column. Following
is the syntax,

ALTER TABLE table_name RENAME old_column_name TO


new_column_name;

ALTER Command: Drop a Column

ALTER command can also be used to drop or remove columns.


Following is the syntax,

ALTER TABLE table_name DROP( column_name);

UPDATE command is used to update any record of data in a table.

Following is its general syntax,


UPDATE table_name SET column_name = new_value WHERE
some_condition;
WHERE is used to add a condition to any SQL query, we will soon
study about it in detail.

ASSIGNMENT 2 Inserting values into table:

DML COMMAND

INSERT command is used to insert data into a table. Following is its


general syntax:

INSERT INTO table_name VALUES(data1, data2, ...)

Insert value into only specific columns


We can use the INSERT command to insert values for only some specific
columns of a row. We can specify the column names along with the
values to be inserted like this,

INSERT INTO student(id, name) values(value, value);

The above SQL query will only insert id and name values in the newly
inserted record.

INSERT NULL value to a column

Both the statements below will insert NULL value into age column of
the student table.

INSERT INTO student(id, name) values(value, value);

Or

INSERT INTO Student VALUES(102,'Alex', null)

Assignment 3

SQL Transaction: COMMIT, ROLLBACK AND SAVEPOINT

The following commands are used to control transactions.

● COMMIT − to save the changes.


● ROLLBACK − to roll back the changes.
● SAVEPOINT − creates points within the groups of transactions in
which to ROLLBACK.
● SET TRANSACTION − Place a name on a transaction.

The COMMIT Command

The COMMIT command is the transactional command used to save changes


invoked by a transaction to the database.

The COMMIT command is the transactional command used to save changes


invoked by a transaction to the database. The COMMIT command saves all
the transactions to the database since the last COMMIT or ROLLBACK
command.

The syntax for the COMMIT command is as follows.

CUSTOMERS Table

1.DELETE FROM CUSTOMER


WHERE AGE=25;

2. COMMIT;

OUTPUT two rows will be deleted


The ROLLBACK Command

The ROLLBACK command is the transactional command used to undo


transactions that have not already been saved to the database. This command
can only be used to undo transactions since the last COMMIT or ROLLBACK
command was issued.

The syntax for a ROLLBACK command is as follows − ROLLBACK;

Example: CUSTOMERS TABLE

1.DELETE FROM CUSTOMER


WHERE AGE=25;
2. ROLLBACK;

OUTPUT after executing ROLLBACK.

Thus, the delete operation would not impact the table and the SELECT
statement would produce the following result.

The SAVEPOINT Command

A SAVEPOINT is a point in a transaction when you can roll the transaction back to
a certain point without rolling back the entire transaction.

The syntax for a SAVEPOINT command is as shown below.

ROLLBACK TO SAVEPOINT_NAME;

Example

Consider the CUSTOMERS table having the following records.


SAVEPOINT SP1;

Savepoint created.

DELETE FROM CUSTOMERS WHERE ID=1;

1 row deleted.

SAVEPOINT SP2;

Savepoint created.

DELETE FROM CUSTOMERS WHERE ID=2;

1 row deleted.

SAVEPOINT SP3;

Savepoint created.

SQL> DELETE FROM CUSTOMERS WHERE ID=3;

1 row deleted.

Now that the three deletions have taken place, let us assume that you have changed
your mind and decided to ROLLBACK to the SAVEPOINT that you identified as
SP2. Because SP2 was created after the first deletion, the last two deletions are undone

1. ROLLBACK TO SP2;

Rollback complete;

SELECT * FROM CUSTOMERS;


ASSIGNMENT 4

Consider the following schema PARTS MANUFACTURER:

Suppliers(sid: integer, sname: string, address: string)

Parts(pid: integer, pname: string, color: string)

Catalog(sid: integer, pid: integer, cost: real)

1.Find the names of suppliers who supply some red part.

2. Find the sids of suppliers who supply some red or green part.

3. Find the sids of suppliers who supply some red part or are at 221
Packer Street.

4. Find the sids of suppliers who supply some red part and some green
part

5. Find the sids of suppliers who supply every part


ASSIGNMENT 5:

Consider the following schema for a LibraryDatabase:

BOOK (Book_id, Title, Publisher_Name, Pub_Year)

BOOK_AUTHORS (Book_id, Author_Name)

PUBLISHER (Name, Address, Phone)

BOOK_COPIES (Book_id, Branch_id, No-of_Copies)

BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out,


Due_Date)

LIBRARY_BRANCH (Branch_id, Branch_Name, Address)

Card (CaRd_No)

Write SQL queries to

1. Retrieve details of all books in the library – id, title, name of publisher,
authors, number of copies in each branch,etc.

2. Get the particulars of borrowers who have borrowed more than 3


books, but from Jan 2017 to Jun2017

3. Delete a book in the BOOK table. Update the contents of other tables
to reflect this data manipulation operation.

4. Partition the BOOK table based on year of publication. Demonstrate


its working with a simplequery.

5. Create a view of all books and its number of copies that are currently
available in the Library.
Assignment 6:

Consider the following schema for OrderDatabase:

SALESMAN (Salesman_id, Name, City, Commission)

CUSTOMER (Customer_id, Cust_Name, City, Grade,Salesman_id)

ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id,


Salesman_id)

Write SQL queries to

1. Count the customers with grades above Kolkata's average.

2. Find the name and numbers of all salesmen who had more than one
customer. 3. List all salesmen and indicate those who have and don’t
have customers in their cities (Use UNIONoperation.)

4. Create a view that finds the salesman who has the customer with the
highest order of a day.

5. Demonstrate the DELETE operation by removing salesman with id


1000. All his orders must also be deleted.

Assignment 7:

Consider the schema for MovieDatabase:

ACTOR (Act_id, Act_Name, Act_Gender)

DIRECTOR (Dir_id, Dir_Name, Dir_Phone)

MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir_id)


MOVIE_CAST (Act_id, Mov_id, Role)

RATING (Mov_id, Rev_Stars)

Write SQL queries to

1. List the titles of all movies directed by Hitchcock’.

2. Find the movie names where one or more actors acted in two or more
movies.

3. List all actors who acted in a movie before 2000 and also in a movie
after 2015 (use JOINoperation).

4. Find the title of movies and number of stars for each movie that has at
least one rating and find the highest number of stars that movie received.
Sort the result by movie title.

5. Update rating of all movies directed by ‘Steven Spielberg’ to5.

Assignment 8:

Consider the schema for CollegeDatabase:

STUDENT (USN, SName, Address, Phone, Gender)

SEMSEC (SSID, Sem, Sec) CLASS (USN, SSID)

SUBJECT (Subcode, Title, Sem, Credits)

IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)

Write SQL queries to

1. List all the student details studying in the fourth semester ‘C’ section.

2. Compute the total number of male and female students in each


semester and in each section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all
subjects.

4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.

5. Categorize students based on the following criterion: If FinalIA = 17


to 20 then CAT =‘Outstanding’ If FinalIA = 12 to 16 then CAT =
‘Average’ If FinalIA< 12 then CAT = ‘Weak’ Give these details only for
5th semester A and B section students.

Assignment 9

Consider the schema for CompanyDatabase:

EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)


DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)

DLOCATION (DNo,DLoc)

PROJECT (PNo, PName, PLocation, DNo)

WORKS_ON (SSN, PNo, Hours)

Write SQL queries to

1. Make a list of all project numbers for projects that involve an


employee whose last name is ‘Scott’, either as a worker or as a manager
of the department that controls the project.

2. Show the resulting salaries if every employee working on the ‘IoT’


project is given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’
department, as well as the maximum salary, the minimum salary, and the
average salary in this department

4. Retrieve the name of each employee who works on all the projects
controlled by department number

5 (use NOT EXISTS operator). For each department that has more than
five employees, retrieve the department number and the number of its
employees who are making more than Rs.6,00,000.

Assignment 10
Consider the following relations containing airline flight
information:

Flights(flno: integer, from: string, to: string, distance: integer,


departs: time, arrives: time)
Aircraft(aid: integer, aname: string, cruisingrange: integer)
Certified(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)

1.Find the eids of pilots certified for some Boeing aircraft.


2. Find the names of pilots certified for some Boeing aircraft.
3. Find the aids of all aircraft that can be used on non-stop flights from
Bonn to Madras.
4. Identify the flights that can be piloted by every pilot whose salary is
more than $100,000.
5. Find the names of pilots who can operate planes with a range greater
than 3,000 miles but are not certified on any Boeing aircraft.

You might also like