Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views2 pages

Practical 1 SQL Report

Uploaded by

24030164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Practical 1 SQL Report

Uploaded by

24030164
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical 1: SQL DDL and DML Commands

Aim: To create and manipulate relational database tables using SQL DDL (Data Definition
Language) and DML (Data Manipulation Language) commands.

Schema:
Emp(eid: integer, ename: string, age: integer, salary: real, email_id: varchar(15)) Dept(did: integer,
dname: string, budget: real, managerid: integer, location: varchar(10)) Works(eid: integer, did:
integer, pcttime: integer)

SQL Queries and Outputs

1. Create Database
CREATE DATABASE emp_info; USE emp_info;

2. Create Tables
CREATE TABLE Emp ( eid INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, age INT,
salary REAL CHECK (salary >= 0), email_id VARCHAR(15) ); CREATE TABLE Dept ( did INT
PRIMARY KEY, dname VARCHAR(50) NOT NULL, budget REAL, managerid INT NOT NULL,
location VARCHAR(20), FOREIGN KEY (managerid) REFERENCES Emp(eid) ); CREATE TABLE
Works ( eid INT, did INT, pcttime INT, PRIMARY KEY (eid, did), FOREIGN KEY (eid)
REFERENCES Emp(eid), FOREIGN KEY (did) REFERENCES Dept(did) );

3. Foreign Key Example


ALTER TABLE Works ADD CONSTRAINT fk_dept FOREIGN KEY (did) REFERENCES Dept(did)
ON DELETE CASCADE ON UPDATE CASCADE;

4. Alter Table Examples


ALTER TABLE Dept ADD location VARCHAR(20); ALTER TABLE Dept MODIFY location
VARCHAR(10); ALTER TABLE Emp ADD dummy_col VARCHAR(20); ALTER TABLE Emp DROP
COLUMN dummy_col;

5. Insert Records into Emp


INSERT INTO Emp VALUES (101, 'John Doe', 32, 15000, '[email protected]'); INSERT INTO Emp
VALUES (102, 'Alice Smith', 29, 20000, '[email protected]'); INSERT INTO Emp VALUES (103,
'Bob Lee', 40, 25000, '[email protected]'); INSERT INTO Emp VALUES (104, 'David Roy', 35,
30000, '[email protected]'); INSERT INTO Emp VALUES (105, 'Eva Brown', 28, 18000,
'[email protected]');

6. Insert Records into Dept


INSERT INTO Dept VALUES (1, 'HR', 500000, 102, 'Delhi'); INSERT INTO Dept VALUES (2, 'IT',
1200000, 103, 'Mumbai'); INSERT INTO Dept VALUES (3, 'Finance', 800000, 104, 'Pune'); INSERT
INTO Dept VALUES (4, 'Marketing', 600000, 105, 'Chennai'); INSERT INTO Dept VALUES (5,
'Toy', 300000, 101, 'Goa');

7. Insert Records into Works


INSERT INTO Works VALUES (101, 1, 50); INSERT INTO Works VALUES (102, 2, 100); INSERT
INTO Works VALUES (103, 2, 70); INSERT INTO Works VALUES (104, 3, 80); INSERT INTO
Works VALUES (105, 5, 60);

8. Insert John Doe Separately


INSERT INTO Emp (eid, ename, age, salary) VALUES (106, 'John Doe', 32, 15000);

9. Update Salaries
UPDATE Emp SET salary = salary * 1.10;

10. Delete Toy Department


DELETE FROM Dept WHERE dname = 'Toy';

Sample Output (Emp Table after Inserts)


eid ename age salary email_id
101 John Doe 32 15000 [email protected]
102 Alice Smith 29 20000 [email protected]
103 Bob Lee 40 25000 [email protected]
104 David Roy 35 30000 [email protected]
105 Eva Brown 28 18000 [email protected]

Conclusion:
In this practical, we successfully created and modified database tables using SQL DDL commands,
enforced integrity constraints, inserted and updated records using DML commands, and
demonstrated deletion operations. This exercise provided practical experience with relational
schema design and manipulation in SQL.

You might also like