0% found this document useful (0 votes)
16 views35 pages

Wa0000.

The document describes executing various Data Definition Language (DDL) and Data Manipulation Language (DML) commands in a Relational Database Management System (RDBMS). It details creating a database and table, inserting values into tables, updating values in tables based on conditions, and selecting attributes from tables. Various commands like create, alter, drop, truncate, insert, update, select are demonstrated along with their syntax and sample outputs.

Uploaded by

Sidharth
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)
16 views35 pages

Wa0000.

The document describes executing various Data Definition Language (DDL) and Data Manipulation Language (DML) commands in a Relational Database Management System (RDBMS). It details creating a database and table, inserting values into tables, updating values in tables based on conditions, and selecting attributes from tables. Various commands like create, alter, drop, truncate, insert, update, select are demonstrated along with their syntax and sample outputs.

Uploaded by

Sidharth
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/ 35

CS22311 – DATABASE MANAGEMENT SYSTEMS LABORATORY

EX.NO : 1
DATE :

CREATION OF A DATABASE AND WRITING SQL QUERIES TO RETRIEVE


INFORMATION FROM THE DATABASE

AIM:
To execute the various Data Definition Language commands in RDBMS.

DATABASE:

1. Creation of database

Syntax: create database dbname;

Query: create database cs22187;

2. To list the databases

Query: show databases;

Output:
+--------------------+
| Database |
+--------------------+
| information_schema |
| cs22187 |
| test |
+--------------------+
3 rows in set (0.01 sec)

3. Using of database

Syntax: use dbname;

Query: use cs22187;

Output:
Database changed

Roll Number : 2127220501187 Page | 1


4. Deleting database

Query: drop dbname;

TABLES:

5. Creation of table

Syntax: create table tbname(colname attribute_type(size), colname2


attribute_type(size),…);

Query: create table student (rno int(3),name varchar(10));

Output: Query OK, 0 rows affected (0.05 sec)

6. To list tables in database

Query: show tables;

Output:
+-------------------+
| Tables_in_cs22187 |
+-------------------+
| Employee |
| student |
| student1 |
+-------------------+

7. To describe the table

Syntax: desc tbname;

Query: desc student;

Output:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| rno | int(3) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 2


8. Add field to the table
Syntax: alter table tbname add colname attribute_type(size);

Query: alter table student add dept varchar(10);

Output:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| rno | int(3) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| dept | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

9. Add field to a particular position


Syntax: alter table tbname add colname attribute_type(size) after existing_abname;

Query: alter table student add gpa float after name;

Output:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| rno | int(3) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| gpa | float | YES | | NULL | |
| dept | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
10. To rename a table
Syntax: alter table tbname rename new_name;

Query: alter table student rename student2;

Output:
+-------------------+
| Tables_in_cs22187 |
+-------------------+
| Employee |
| student1 |
| student2 |
+-------------------+

Roll Number : 2127220501187 Page | 3


11. To change the attribute name in a table
Syntax: alter table tbname change oldattname newattname attribute_type(size);

Query: alter table student1 change dept department varchar(30);

Output:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| rno | int(3) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| department | varchar(30) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

12. Delete an attribute


Syntax: alter table tbname drop attribute_name;

Query: alter table student drop phno;

Output:
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| s_regno | int(5) | NO | PRI | NULL | |
| s_name | varchar(10) | YES | | NULL | |
| deptname | varchar(10) | YES | | NULL | |
| dob | date | YES | | NULL | |
| address | varchar(20) | YES | | NULL | |
| cgpa | float(5,2) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
13. To delete a table
Syntax: drop tbname;

Query: drop table course;


Output:
+-------------------+
| Tables_in_cs22187 |
+-------------------+
| department |
| student |
+-------------------+
2 rows in set (0.02 sec)

Roll Number : 2127220501187 Page | 4


14. Truncate table
Syntax: truncate table tbname;

Query: truncate table department;

Output:
Empty set (0.00 sec)

CONSTRAINTS:

15. Primary key:


Syntax:
• alter table tbname add constraint primary key(attrib_name);
• create table tbname (attrib_name type(size) primary key);

Query: alter table student1 add constraint primary key(rno);


Output:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| rno | int(3) | NO | PRI | 0 | |
| name | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| department | varchar(30) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
16. Foreign key
Syntax:
• alter table tbname1 add constraint foreign key(attrib_name1) references
tbname2(attrib_name2)
• create table tbname (attribname1 type(size) foreign key reference
tbname2(attribname2));
Query: alter table Employee add constraint foreign key (Empno) references
student1(rno);
Output:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Empno | int(5) | YES | MUL | NULL | |
| Empname | varchar(20) | YES | | NULL | |
| Empsalary | float | YES | | NULL | |
| Empdob | date | YES | | NULL | |
| Empphno | int(10) | YES | | NULL | |
| Empemailid | varchar(20) | YES | | NULL | |
| Empaddress | varchar(25) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+

Roll Number : 2127220501187 Page | 5


17. UNIQUE
Syntax:
• alter table tbname add constraint UNIQUE(attribname);
• create table tbname(attribname type(size) UNIQUE);

Query: alter table student1 add constraint UNIQUE(name);


Output:
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| rno | int(3) | NO | PRI | 0 | |
| name | varchar(20) | YES | UNI | NULL | |
| grade | float | YES | | NULL | |
| department | varchar(30) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

RESULT:
The required data description language commands were executed and the output
was obtained successfully.

Roll Number : 2127220501187 Page | 6


CS22311 – DATABASE MANAGEMENT SYSTEMS LABORATORY
EX.NO : 1.2
DATE :
DATA MANIPULATION LANGUAGE (DML) COMMANDS IN RDBMS

AIM:
To execute and verify the DML commands are the most frequently used SQL
commands and is used to query and manipulate the existing database objects.

1. Insert values in a relation


Syntax: insert into tbname values(a,b,c),(d,e,f);

Query: insert into emp values (1,"Ram",2000,"Accounts"),


(2,"Jaanu",5000,"Accounts"), (3,"Lamar",7000,"Engineering") ,
(4,"Alan",6000,"Hist") , (5,"Wasim",6000,"Accounts");

Output:
Query OK, 5 rows affected (0.00 sec) Records: 5
Duplicates: 0 Warnings: 0

2. To display all the values


Syntax: select * from tbname;

Query: select * from emp;

Output:
| +-------+---------+--------+---------+ |
| | empid | empname | empsal | empdept | |
| +-------+---------+--------+---------+ |
| 1 | Ram | 2000 | Accounts |
| 2 |Jaanu | 5000 | Accounts |
| 3 |Lamar | 7000 |Engineering |
| 4 |Alan | | 6000 | Hist |
| 5 |Wasim | 6000 | Accounts |
| +-------+---------+--------+---------+ |
5 rows in set (0.00 sec)

3. To update the values based on condition


Syntax: update tbname set attribute=value where condition ;

Query: update emp set empname="Sean" where empid=3;

Roll Number : 2127220501187 Page | 7


Output:
select * from emp;
| +-------+---------+--------+---------+ |
| | empid | empname | empsal | empdept | |
| +-------+---------+--------+---------+ |
| 1 | Ram | 2000 | Accounts |
| 2 |Jaanu | 5000 | Accounts |
| 3 |Sean | 7000 |Engineering |
| 4 |Alan | | 6000 | Hist |
| 5 |Wasim | 6000 | Accounts |
| +-------+---------+--------+---------+ |
5 rows in set (0.00 sec)

4. To select a particular attribute from the table and display


Syntax: select attribname from tbname;

Query: select empname from emp;


Output:
+---------+
| empname |
+---------+
| Ram |
| Jaanu |
| Sean |
| Alan |
| Wasim |
+---------+
5 rows in set (0.00 sec)

5. To display more attributes from table


Syntax: select attrib1,attrib2,… from tbname;

Query: select empname,empsal from emp;

Output:
| +-------+---------+-----+ |
| empname | empsal |
| +-------+---------+-----+ |
| Ram | 2000 |
| Jaanu | 5000 |
| Sean | 7000 |
| Alan | | 6000 |
| Wasim | 6000 |
| | +-------+---------+-----+ |
5 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 8


6. To select distinct values from a table
Syntax: select distinct (attribname) from tbname;

Query: select distinct(empdept) from emp;

Output:
+---------+
| empdept |
+---------+
| Accounts |
| Engineering |
| Hist |
+---------+
3 rows in set (0.00 sec)

7. To display records in ascending order


Syntax: select att1 from tbname order by att2;

Query: select empid,empname from emp order by empsal;

Output:
+-------+---------+
| empid | empname |
+-------+---------+
| 1 | Ram |
| 2 |Jaanu |
| 4 |Alan |
| 5 |Wasim |
| 3 |Sean |
+-------+---------+
5 rows in set (0.00 sec)

8. To display records in descending order


Syntax: select att1 from tbname order by att2 desc;
Query: select empid,empname from emp order by empsal desc;
Output:
+-------+---------+
| empid | empname |
+-------+---------+
| 3 | Sean|
| 5 |Wasim |
| 4 |Alan |
| 2 |Jaanu |
| 1 |Ram |
+-------+---------+
5 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 9


9. To select attributes with and/or condition
Syntax: select attribname from tbname where condition1 and/or condition2;

Query: select empname from emp where empsal=6000 and empdept="Hist";

Output:
+---------+
| empname |
+---------+
| Alan |
+---------+
1 row in set (0.00 sec)

Query: select empname from emp where empsal=6000 or empdept="Hist";

Output:
+---------+
| empname |
+---------+
| Alan |
| Wasim |
+---------+
2 rows in set (0.00 sec)

10. Insert particular value


Syntax: insert into tbname (attribname) values (“xxx”);

Query: insert into emp(empname) values("Carl");

Output:
| +-------+---------+--------+---------+ |
| | empid | empname | empsal | empdept | |
| +-------+---------+--------+---------+ |
| 1 | Ram | 2000 | Accounts |
| 2 |Jaanu | 5000 | Accounts |
| 3 |Sean | 7000 |Engineering |
| 4 |Alan | | 6000 | Hist |
| 5 |Wasim | 6000 | Accounts |
| 0 |Carl | NULL | NULL |
| +-------+---------+--------+---------+ |
5 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 10


11. Delete a record
Syntax: delete from tbname where condition;

Query: delete from emp where empname="Carl";

Output:
| +-------+---------+--------+---------+ |
| | empid | empname | empsal | empdept | |
| +-------+---------+--------+---------+ |
| 1 | Ram | 2000 | Accounts |
| 2 |Jaanu | 5000 | Accounts |
| 3 |Sean | 7000 |Engineering |
| 4 |Alan | | 6000 | Hist |
| 5 |Wasim | 6000 | Accounts |
| +-------+---------+--------+---------+ |
5 rows in set (0.00 sec)

12. Delete a constraint


Syntax: alter table tbname drop primary key;

Query: alter table emp drop primary key;

Output:
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| empid | int(2) | NO | |0 | |
| empname | varchar(10) | YES | | NULL | |
| empsal | float | YES | | NULL | |
| empdept | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

RESULT:
The required data manipulation language commands were successfully executed.

Roll Number : 2127220501187 Page | 11


CS22311 – DATABASE MANAGEMENT SYSTEMS LABORATORY
EX.NO : 2
DATE :
PERFORMING INSERTION, DELETION, MODIFYING, ALTERING,
UPDATING AND VIEWING RECORDS BASED ON CONDITIONS

AIM:
To performing insertion, deletion, modifying, altering, updating and viewing records
based on conditions.

1. CREATE TABLE
create table employee(empno int(10),ename varchar(20), job varchar(10),mgr int(10),
hiredate date, sal int(10),deptno int(10));
Query OK, 0 rows affected, 4 warnings (0.03 sec)
desc employee;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int | YES | | NULL | |
| ename | varchar(10) | YES | | NULL | |
| job | varchar(20) | YES | | NULL | |
| mgr | int | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | int | YES | | NULL | |
| deptno | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

2. INSERT VALUES
insert into employee values
(7370,"Allen","Salesman",5002,"1908-02-03",3000,300),
(7521,"Jones","Salesman",5003,"1980-03-13",5000,500),
(7566,"Dennis","Manager",5002,"1985-03-26",75000,200),
(7576,"Raja","Owner",5000,"1982-09-25",9000,900),
(7586,"Benny","COE",5001,"1984-03-30",8000,800),
(7499,"Henry","CEO",5005,"1987-03-30",50000,500);
Query OK, 6 rows affected (0.04 sec)
Records: 6 Duplicates: 0 Warnings: 0

Roll Number : 2127220501187 Page | 12


select* from employee;
+-------+-------+----------+------+------------+-------+--------+
| empno | ename | job | mgr | hiredate | sal | deptno |
+-------+-------+----------+------+------------+-------+--------+
| 7370 | Allen | Salesman | 5002 | 1908-02-03 | 3000 | 300 |
| 7521 | Jones | Salesman | 5003 | 1980-03-13 | 5000 | 500 |
| 7566 | Dennis | Manager | 5002 | 1985-03-26 | 75000 | 200 |
| 7576 | Raja | Owner | 5000 | 1982-09-25 | 9000 | 900 |
| 7586 | Benny | COE | 5001 | 1984-03-30 | 8000 | 800 |
| 7499 | Henry | CEO | 5005 | 1987-03-30 | 50000 | 500 |
+-------+-------+----------+------+------------+-------+--------+
7 rows in set (0.00 sec)

3. BY USING SELECTED COLUMN

select empno,job,ename,sal from employee;


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
6 rows in set (0.00 sec)

select empno,job,ename,sal from employee where sal=5000;


+-------+----------+------+------+
| empno |job | ename | sal |
+-------+----------+------+------+
| 7521 | Salesman | Jones | 5000 |
+-------+----------+------+------+
1 row in set (0.00 sec)

4. BY USING BETWEEN / NOT / IN / NULL / LIKE


select empno,job,ename,sal from employee where sal between 1000 and 30000;
+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
+-------+----------+-------+------+
3 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 13


select empno,job,ename,sal from employee where sal not between 10000 and 30000;
+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
8 rows in set (0.00 sec)

select empno,job,ename,sal from employee where sal in (1000.5,75000);


+-------+---------+-------+-------+
| empno |job | ename | sal |
+-------+---------+-------+-------+
| 7566 | Manager | Dennis | 75000 |
+-------+---------+-------+-------+
1 row in set (0.00 sec)

select empno,job,ename,sal from employee where sal not in (1000.5,75000);


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
4 rows in set (0.00 sec)

select empno,job,ename,sal from employee where sal is null;


empty set (0.00 sec)

select empno,job,ename,sal from employee where sal is not null;


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
5 rows in set (0.00 sec)
Roll Number : 2127220501187 Page | 14
select empno,job,ename,sal from employee where sal like 75000;
+-------+---------+-------+-------+
| empno |job | ename | sal |
+-------+---------+-------+-------+
| 7566 | Manager | Dennis | 75000 |
+-------+---------+-------+-------+
1 row in set (0.00 sec)

select empno,job,ename,sal from employee where job like 's%';


+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
+-------+----------+-------+------+
2 rows in set (0.04 sec)

select empno,job,ename,sal from employee where job like '%r';


+-------+---------+-------+-------+
| empno |job | ename | sal |
+-------+---------+-------+-------+
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
+-------+---------+-------+-------+
2 rows in set (0.00 sec)

select empno,job,ename,sal from employee where job like '%r%';


+-------+---------+-------+-------+
| empno |job | ename | sal |
+-------+---------+-------+-------+
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
+-------+---------+-------+-------+
2 rows in set (0.00 sec)

select empno,job,ename,sal from employee where job like '%a%';


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
+-------+----------+-------+-------+
3 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 15


select empno,job,ename,sal from employee where job like '%le%';
+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
+-------+----------+-------+------+
2 rows in set (0.00 sec)

select empno,job,ename,sal from employee where job like '%e%';


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
6 rows in set (0.00 sec)

select empno,job,ename,sal from employee where job like '%s%a%';


+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
+-------+----------+-------+------+
2 rows in set (0.00 sec)

select empno,job,ename,sal from employee where job like 'o____';


+-------+-------+------+------+
| empno |job | ename | sal |
+-------+-------+------+------+
| 7576 | Owner | Raja | 9000 |
+-------+-------+------+------+
1 row in set (0.00 sec)

select empno,job,ename,sal from employee where job like 'c_o';


+-------+-------+-------+-------+
| empno |job | ename | sal |
+-------+-------+-------+-------+
| 7499 | CEO | Henry | 50000 |
+-------+-------+-------+-------+
1 row in set (0.00 sec)

Roll Number : 2127220501187 Page | 16


select empno,job,ename,sal from employee where job like '_o%';
+-------+-------+------+------+
| empno |job | ename | sal |
+-------+-------+------+------+
| 7586 | COE | Benny | 8000 |
+-------+-------+------+------+
1 row in set (0.00 sec)

5. RELATIONAL OPERATOR

select empno,job,ename,sal from employee where sal=75000;


+-------+---------+-------+-------+
| empno |job | ename | sal |
+-------+---------+-------+-------+
| 7566 | Manager | Dennis | 75000 |
+-------+---------+-------+-------+
1 row in set (0.00 sec)

select empno,job,ename,sal from employee where sal!=55000;


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
6. rows in set (0.00 sec)

select empno,job,ename,sal from employee where sal<>55000;


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
9 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 17


select empno,job,ename,sal from employee where sal<=55000;
+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7576 | Owner | Raja | 9000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
5 rows in set (0.00 sec)

6. AND / OR

select empno,job,ename,sal from employee where job='Salesman' and sal=3000;


+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
+-------+----------+-------+------+
1 row in set (0.00 sec)

select empno,job,ename,sal from employee where job='Salesman' or sal=22000;


+-------+----------+-------+------+
| empno |job | ename | sal |
+-------+----------+-------+------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
+-------+----------+-------+------+
7. rows in set (0.00 sec)

select empno,job,ename,sal from employee where sal=5000 and (job='Salesman' or


job='CEO');
+-------+----------+------+------+
| empno |job | ename | sal |
+-------+----------+------+------+
| 7521 | Salesman | Jones | 5000 |
+-------+----------+------+------+
1 row in set (0.00 sec)

7. ORDER BY

Roll Number : 2127220501187 Page | 18


select empno,job,ename,sal from employee order by ename;
+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7499 | CEO | Henry | 50000 |
| 7586 | COE | Benny | 8000 |
| 7566 | Manager | Dennis | 75000 |
| 7576 | Owner | Raja | 9000 |
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
+-------+----------+-------+-------+
8. rows in set (0.01 sec)

select empno,job,ename,sal from employee order by ename desc;


+-------+----------+-------+-------+
| empno |job | ename | sal |
+-------+----------+-------+-------+
| 7370 | Salesman | Allen | 3000 |
| 7521 | Salesman | Jones | 5000 |
| 7576 | Owner | Raja | 9000 |
| 7566 | Manager | Dennis | 75000 |
| 7586 | COE | Benny | 8000 |
| 7499 | CEO | Henry | 50000 |
+-------+----------+-------+-------+
9. rows in set (0.00 sec)

8. ALTER

a) ADDING COLUMN
alter table employee add tax int;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

desc employee;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| mgr | int | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | int | YES | | NULL | |
| deptno | int | YES | | NULL | |
| TAX | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
10. rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 19


b) REMOVING COLUMN

alter table EMPLOYEE drop column TAX;


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

DESC EMPLOYEE;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| mgr | int | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | int | YES | | NULL | |
| deptno | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

C) RENAMING

alter table EMPLOYEE rename column SAL to SALARY;


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

DESC EMPLOYEE;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| mgr | int | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| SALARY | int | YES | | NULL | |
| deptno | int | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

9. USING UPDATE

update employee set salary = 3000,ename = 'Salesman';


Query OK, 6 rows affected (0.01 sec)
Rows matched: 6 Changed: 6 Warnings: 0

Roll Number : 2127220501187 Page | 20


select * from EMPLOYEE;
+-------+-------+----------+------+------------+--------+--------+
| empno | ename | job | mgr | hiredate | salary | deptno |
+-------+-------+----------+------+------------+--------+--------+
| 7370 | Allen | Salesman | 5002 | 1908-02-03 | 3000 | 300 |
| 7521 | Jones | Salesman | 5003 | 1980-03-13 | 3000 | 500 |
| 7566 | Dennis | Salesman | 5002 | 1985-03-26 | 3000 | 200 |
| 7576 | Raja | Salesman | 5000 | 1982-09-25 | 3000 | 900 |
| 7586 | Benny | Salesman | 5001 | 1984-03-30 | 3000 | 800 |
| 7499 | Henry | Salesman | 5005 | 1987-03-30 | 3000 | 500 |
+-------+-------+----------+------+------------+--------+--------+-------+
6 rows in set (0.00 sec)

RESULT:
Thus the SQL commands for Performing Insertion, Deletion, Modifying, Altering, Updating
and Viewing records based on conditions has been verified and executed successfully.

Roll Number : 2127220501187 Page | 21


CS22311 – DATABASE MANAGEMENT SYSTEMS LABORATORY
EX.NO : 3
DATE :
CREATION OF VIEWS, SYNONYMS, SEQUENCE, INDEXES, SAVE POINT

AIM:
To create views, synonyms, sequences, indexes and save points using DDL, DML and DCL
statements.

TO CREATE THE TABLE ‘FVIEWS’:

create table fviews( name varchar(20),no int(5), sal int(5), dno int(5));
Query OK, 0 rows affected, 3 warnings (0.07 sec)

insert into fviews values('ram',1,19000,11),('shyam',2,19000,12),('prem',3,40000,13);


Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0

select * from fviews;


+-------+------+-------+------+
| name | no | sal | dno |
+-------+------+-------+------+
| ram | 1 | 19000 | 11 |
| shyam | 2 | 19000 | 12 |
| prem | 3 | 40000 | 13 |
+-------+------+-------+------+
2 rows in set (0.00 sec)

TO CREATE THE TABLE ‘DVIEWS’:

create table dviews( dno int(5), dname varchar(20));


Query OK, 0 rows affected, 1 warning (0.05 sec)

insert into dviews values(11,'hello'),(12,'mello');


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

select * from dviews;


+------+-------+
| dno | dname |
+------+-------+
| 11 | hello |
| 12 | mello |
+------+-------+
2 rows in set (0.00 sec)

Roll Number : 2127220501187 Page | 22


CREATING THE VIEW ‘SVIEW’ ON ‘FVIEWS’ TABLE:

create view sview as select name,no,sal,dno from fviews where dno=11;


Query OK, 0 rows affected (0.04 sec)

select * from sview;


+------+------+-------+------+
| name | no | sal | dno |
+------+------+-------+------+
| ram | 1 | 19000 | 11 |
+------+------+-------+------+
1 row in set (0.04 sec)

insert into sview values ('bhai',4,20000,14);


Query OK, 1 row affected (0.04 sec)

select * from sview;


+------+------+-------+------+
| name | no | sal | dno |
+------+------+-------+------+
| ram | 1 | 19000 | 11 |
+------+------+-------+------+
1 row in set (0.00 sec)

select * from fviews;


+-------+------+-------+------+
| name | no | sal | dno |
+-------+------+-------+------+
| ram | 1 | 19000 | 11 |
| shyam | 2 | 19000 | 12 |
| prem | 3 | 40000 | 13 |
| bhai | 4 | 20000 | 14 |
+-------+------+-------+------+
3 rows in set (0.00 sec)

CREATING A VIEW ‘IVIEW’ FOR THE TABLE ‘FVIEWS’:


create view iview as select * from fviews;
Query OK, 0 rows affected (0.04 sec)
select * from iview;
+-------+------+-------+------+
| name | no | sal | dno |
+-------+------+-------+------+
| ram | 1 | 19000 | 11 |
| shyam | 2 | 19000 | 12 |
| prem | 3 | 40000 | 13 |
| bhai | 4 | 20000 | 14 |
Roll Number : 2127220501187 Page | 23
+-------+------+-------+------+
4 rows in set (0.00 sec)
PERFORMING UPDATE OPERATION:

insert into iview values ('arjun',5,30000,15);


Query OK, 1 row affected (0.00 sec)

select * from iview;


+-------+------+-------+------+
| name | no | sal | dno |
+-------+------+-------+------+
| ram | 1 | 19000 | 11 |
| shyam | 2 | 19000 | 12 |
| prem | 3 | 40000 | 13 |
| bhai | 4 | 20000 | 14 |
| arjun | 5 | 30000 | 15 |
+-------+------+-------+------+
5 rows in set (0.00 sec)

select * from fviews;


+-------+------+-------+------+
| name | no | sal | dno |
+-------+------+-------+------+
| ram | 1 | 19000 | 11 |
| shyam | 2 | 19000 | 12 |
| prem | 3 | 40000 | 13 |
| bhai | 4 | 20000 | 14 |
| arjun | 5 | 30000 | 15 |
+-------+------+-------+------+
4 rows in set (0.00 sec)

CREATE A NEW VIEW ‘SSVIEW’ AND DROP THE VIEW:

create view ssview( cusname,id) as select name, no from fviews where dno=12;
Query OK, 0 rows affected (0.04 sec)

select * from ssview;


+---------+------+
| cusname | id |
+---------+------+
| shyam | 2 |
+---------+------+
1 row in set (0.00 sec)

drop view ssview;


Query OK, 0 rows affected (0.04 sec)

Roll Number : 2127220501187 Page | 24


TO CREATE A VIEW ‘COMBO’ USING BOTH THE TABLES ‘FVIEWS’ AND
‘DVIEWS’:

create view combo as select name,no,sal,dviews.dno,dname from fviews,dviews where


fviews.dno=dviews.dno;
Query OK, 0 rows affected (0.04 sec)

select * from combo;


+-------+------+-------+------+-------+
| name | no | sal | dno | dname |
+-------+------+-------+------+-------+
| ram | 1 | 19000 | 11 | hello |
| shyam | 2 | 19000 | 12 | mello |
+-------+------+-------+------+-------+
2 rows in set (0.00 sec)

TO PERFORM MANIPULATIONS ON THIS VIEW:

insert into combo values('radhe',12,1000,13,'x');


insert into combo values('radhe',12,1000,13,'x')
*
ERROR at line 1:
ORA-01779: cannot modify a column which maps to a non key-preserved table
This shows that when a view is created from two different tables no manipulations
can be performed using that view and the above error is displayed.

Create synonym:

create synonym c1 for class;


Synonym created.
insert into c1 values('kalai',20);
1 row created.
select * from class;

NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.

Roll Number : 2127220501187 Page | 25


select * from c1;

NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.

insert into class values('Manu',21);


1 row created.
select * from c1;

NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
Manu 21
9 rows selected.

Drop Synonym:
drop synonym c1;
Synonym dropped.
select * from c1;
ERROR at line 1:
ORA-00942: table or view does not exist

Sequences
Creation of table:

create table class(name varchar(10),id int(10));


Query OK, 0 rows affected, 1 warning (0.07 sec)

Roll Number : 2127220501187 Page | 26


Insert values into table:
insert into class values('radha',101),('krishna',102),('shiv',103),('parvathi',104);
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0

select * from class;


+----------+------+
| name | id |
+----------+------+
| radha | 101 |
| krishna | 102 |
| shiv | 103 |
| parvathi | 104 |
+----------+------+
4 rows in set (0.04 sec)

Create Sequence:
create sequence s_1
2 start with 4
3 increment by 1
4 maxvalue 100
5 cycle;
Sequence created.
insert into class values('divya',s_1.nextval);
1 row created.
select * from class;

NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4

Alter Sequence:
alter sequence s_1 increment by 2;
Sequence altered.
insert into class values('fairoz',s_1.nextval);
1 row created.

Roll Number : 2127220501187 Page | 27


select * from class;

NAME ID
---------- ----------
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7

Drop Sequence:
drop sequence s_1;
Sequence dropped.

Indexes:

create table splr(sname varchar(10),sid int(10),scity varchar(10));


Query OK, 0 rows affected, 1 warning (0.06 sec)

insert into splr


values('apple',01,'chennai'),('dell',02,'delhi'),('hp',03,'trichy'),('asus',04,'chennai');
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0

select * from splr;


+-------+------+---------+
| sname | sid | scity |
+-------+------+---------+
| apple | 1 | chennai |
| dell | 2 | delhi |
| hp | 3 | trichy |
| asus | 4 | chennai |
+-------+------+---------+
4 rows in set (0.00 sec)

create index sp1 on splr(sid);


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

create index sp2 on splr(sid,scity);


Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

Roll Number : 2127220501187 Page | 28


Drop Index:

drop index sp1;


Index dropped.
drop index sp2;
Index dropped.

DCL statements
COMMIT, ROLLBACK and SAVEPOINT:

select * from class;


+----------+------+
| name | id |
+----------+------+
| radha | 101 |
| krishna | 102 |
| shiv | 103 |
| parvathi | 104 |
+----------+------+
4 rows in set (0.00 sec)

insert into class values('gayathri',9);


Query OK, 1 row affected (0.04 sec)

commit;
Query OK, 0 rows affected (0.00 sec)

update class set name='hema' where id='9';


Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

savepoint A;
Query OK, 0 rows affected (0.00 sec)

insert into class values('indu',11);


Query OK, 1 row affected (0.04 sec)

savepoint B;
Query OK, 0 rows affected (0.00 sec)

insert into class values('janani',13);


Query OK, 1 row affected (0.04 sec)

Roll Number : 2127220501187 Page | 29


select * from class;
+----------+------+
| name | id |
+----------+------+
| radha | 101 |
| krishna | 102 |
| shiv | 103 |
| parvathi | 104 |
| hema | 9 |
| indu | 11 |
| janani | 13 |
+----------+------+
7 rows in set (0.00 sec)

rollback to B;
Rollback complete.
select * from class;
+----------+------+
| name | id |
+----------+------+
| radha | 101 |
| krishna | 102 |
| shiv | 103 |
| parvathi | 104 |
| hema | 9 |
| indu | 11 |
+----------+------+
6 rows in set (0.00 sec)

rollback to A;
Rollback complete.
select * from class;
+----------+------+
| name | id |
+----------+------+
| radha | 101 |
| krishna | 102 |
| shiv | 103 |
| parvathi | 104 |
| hema | 9 |
+----------+------+
6 rows in set (0.00 sec)

RESULT:
Thus the Views, Synonyms, and Sequences, indexes and save points has been
executed using DDL, DML and DCL statements.

Roll Number : 2127220501187 Page | 30


CS22311 – DATABASE MANAGEMENT SYSTEMS LABORATORY
EX.NO : 4
DATE :
CREATING AN EMPLOYEE DATABASE TO SET VARIOUS CONSTRAINTS

AIM:
To study the various constraints available in the SQL query language.

DOMAIN INTEGRITY CONSTRAINTS


NOT NULL CONSTRAINT

create table employe(ename varchar(30) not null, eid varchar(20) not null);
Query OK, 0 rows affected (0.06 sec)

insert into employe values ('harsh',1),('joel',2);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

insert into employe values ('kumar',null);


ERROR 1048 (23000): Column 'eid' cannot be null

select * from employe;


+-------+-----+
| ename | eid |
+-------+-----+
| harsh | 1 |
| joel | 2 |
+-------+-----+
2 rows in set (0.00 sec)

CHECK AS A COLUMN CONSTRAINT

create table depts ( dname varchar(30) not null, did int(20) not null check (did<10000));
Query OK, 0 rows affected, 1 warning (0.06 sec)

insert into depts values ('sales',9876),('marketing',4356);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

insert into depts values ('accounts',789645);


ERROR 3819 (HY000): Check constraint 'depts_chk_1' is violated.

Roll Number : 2127220501187 Page | 31


select * from depts;
+-----------+------+
| dname | did |
+-----------+------+
| sales | 9876 |
| marketing | 4356 |
+-----------+------+
2 rows in set (0.00 sec)

CHECK AS A TABLE CONSTRAINT

insert into airports values('shivam', 100,'chennai'), ('harsh', 200,'hyderabad'),


('vrishank',300,'bangalore');
Query OK, 3 row affected (0.04 sec)

insert into airports values( 'bhairav', 103,'mumbai');


ERROR 3819 (HY000): Check constraint 'airports_chk_1' is violated.

select * from airports;


+----------+-----+-----------+
| aname | aid | acity |
+----------+-----+-----------+
| shivam | 100 | chennai |
| harsh | 200 | hyderabad |
| vrishank | 300 | bangalore |
+----------+-----+-----------+
3 rows in set (0.00 sec)

ENTITY INTEGRITY CONSTRAINTS


UNIQUE AS A COLUMN CONSTRAINT

create table book (bname varchar(30) not null, bid int(20) not null unique);
Query OK, 0 rows affected, 1 warning (0.05 sec)

insert into book values ('fairy tales',1000),('bedtime stories',1001);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

insert into book values ('comics',1001);


ERROR 1062 (23000): Duplicate entry '1001' for key 'book.bid'

Roll Number : 2127220501187 Page | 32


select * from book;
+-----------------+------+
| bname | bid |
+-----------------+------+
| fairy tales | 1000 |
| bedtime stories | 1001 |
+-----------------+------+
2 rows in set (0.00 sec)

UNIQUE AS A TABLE CONSTRAINT

create table orders( oname varchar(30) not null , oid int(20) not null , unique(oname,oid));
Query OK, 0 rows affected, 1 warning (0.06 sec)

insert into orders values ('chair', 2005),('table',2006),('chair',2007);


Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0

insert into orders values ('chair', 2005);


ERROR 1062 (23000): Duplicate entry 'chair-2005' for key 'orders.oname'

select * from orders;


+-------+------+
| oname | oid |
+-------+------+
| chair | 2005 |
| chair | 2007 |
| table | 2006 |
+-------+------+
3 rows in set (0.00 sec)

PRIMARY KEY AS A COLUMN CONSTRAINT

create table custo ( cname varchar(30) not null , cid int(20) not null primary key);
Query OK, 0 rows affected, 1 warning (0.05 sec)

insert into custo values ( 'jones', 506),('hayden',508);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

insert into custo values ('ricky',506);


ERROR 1062 (23000): Duplicate entry '506' for key 'custo.PRIMARY'

Roll Number : 2127220501187 Page | 33


select * from custo;
+--------+-----+
| cname | cid |
+--------+-----+
| jones | 506 |
| hayden | 508 |
+--------+-----+
2 rows in set (0.00 sec)

PRIMARY KEY AS A TABLE CONSTRAINT

create table branches( bname varchar(30) not null , bid int(20) not null , primary
key(bname,bid));
Query OK, 0 rows affected, 1 warning (0.06 sec)

insert into branches values ('anna nagar', 1005),('adyar',1006);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

insert into branches values ('anna nagar', 1005);


ERROR 1062 (23000): Duplicate entry 'anna nagar-1005' for key 'branches.PRIMARY'

select * from branches;


+------------+------+
| bname | bid |
+------------+------+
| adyar | 1006 |
| anna nagar | 1005 |
+------------+------+
2 rows in set (0.00 sec)

REFERENTIAL INTEGRITY CONSTRAINTS

create table dept(city varchar(20), dno int(5) primary key);


Query OK, 0 rows affected, 1 warning (0.05 sec)

insert into depts values('chennai', 11),('hyderabad', 22);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

TO CREATE ‘SEMP’ TABLE


create table semp(ename varchar(20), dno int(5) references depts(dno));
Query OK, 0 rows affected, 1 warning (0.05 sec)

insert into semp values('xenon', 11),('yash', 22);


Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0

Roll Number : 2127220501187 Page | 34


select * from semp;
+-------+------+
| ename | dno |
+-------+------+
| xenon | 11 |
| yash | 22 |
+-------+------+
2 rows in set (0.00 sec)

ALTER TABLE
alter table semp add(eddress varchar(20));
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

update semp set eddress='10 gandhi road' where dno=11;


Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

update semp set eddress='12 m.g. road' where dno=22;


Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0

select * from semp;


+-------+------+----------------+
| ename | dno | eddress |
+-------+------+----------------+
| xenon | 11 | 10 gandhi road |
| yash | 22 | 12 m.g. road |
+-------+------+----------------+
2 rows in set (0.00 sec)

select city, ename from depts, semp where depts.dno = semp.dno;


+--------------+------------------+
|CITY ENAME |
+--------------- +--------------------+
|Chennai x |
|hyderabad y |
+--------------- +--------------------+
2 rows in set (0.00 sec)

RESULT
The various constraints were implemented and the tables were created using the
respective constraints.

Roll Number : 2127220501187 Page | 35

You might also like