DDL DML Excercise
DDL DML Excercise
DDL DML Excercise
NO:1
IMPLEMENTATION OF DDL COMMANDS
AIM:
To create a DDL to perform creation of table, alter, modify and drop column.
DDL COMMANDS
1. The Create Table Command: - it defines each column of the table uniquely. Each
column has minimum of three attributes, a name , data type and size.
Syntax:
Create table <table name> (<col1> <datatype>(<size>),<col2> <datatype><size>));
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
Syntax:
Alter table <tablename> drop column <col>;
Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));
Syntax:
Rename <oldtable> to <new table>;
Syntax:
Truncate table <tablename>;
7. Destroying tables.
Syntax:
Drop table <tablename>;
SYNTAX:
create table<tablename>(column1 datatype,column2 datatype...);
EXAMPLE:
Table created.
OUTPUT:
Select * from std;
SNO SNAME AGE SDOB SM1 SM2 SM3
101 AAA 16 03-jul-88 80 90 98
102 BBB 18 04-aug-89 88 98 90
ALTER TABLE WITH ADD:
SYNTAX:
alter table<tablename>add(col1 datatype,col2 datatype..);
EXAMPLE:
OUTPUT:
ALTER: select * from student;
ID NAME GAME
1 Mercy Cricket
SYNTAX:
Alter table<tablename>modify(col1 datatype,col2 datatype..);
EXAMPLE:
OUTPUT:
MODIFY
desc student;
NAME NULL? TYPE
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)
DROP:
SYNTAX: drop table<tablename>;
EXAMPLE:
TRUNCATE TABLE
SYNTAX: TRUNCATE TABLE <TABLE NAME>;
CONSTRAINTS:
Create table tablename (column_name1 data_ type constraints, column_name2
data_ type constraints ...)
Example:
Create table Emp ( EmpNo number(5), EName VarChar(15), Job Char(10)
constraint un unique, DeptNo number(3) CONSTRAINT FKey2 REFERENCES
DEPT(DeptNo));
Create table stud (sname varchar2(20) not null, rollno number(10) not null,dob date
not null);
DOMAIN INTEGRITY
Example:
CHECK CONSTRAINT
Example: Create table student (regno number (6), mark number (3) constraint b
check (mark >=0 and mark <=100)); Alter table student add constraint b2 check
(length(regno<=4));
ENTITY INTEGRITY
Queries:
EX.NO:2
IMPLEMENTATION OF DML AND DCL COMMANDS
AIM;
To study the various DML commands and implement them on the database.
DML COMMANDS
DML commands are the most frequently used SQL commands and is used to query
and manipulate the existing database objects. Some of the commands are Insert,
Select,
Update, Delete.
Insert Command This is used to add one or more rows to a table. The values are
separated by
commas and the data types char and date are enclosed in apostrophes. The values
must be
entered in the same order as they are defined.
Select Commands It is used to retrieve information from the table. It is generally
referred to
as querying the table. We can either display all columns in a table or only specify
column
from the table.
Update Command It is used to alter the column values in a table. A single column
may be
updated or more than one column could be updated.
Delete command After inserting row in a table we can also delete them if required.
The delete
command consists of a from clause followed by an optional where clause.
Q1: Insert a single record into dept table.
Ans: SQL> insert into dept values (1,'IT','Tholudur');
1 row created.
Q2: Insert more than a record into emp table using a single insert command.
Ans: SQL> insert into emp values(&empno,'&ename','&job',&deptno,&sal);
Enter value for empno: 1
Enter value for ename: Mathi
Enter value for job: AP
Enter value for deptno: 1
Enter value for sal: 10000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)new 1: insert
into emp values(1,'Mathi','AP',1,10000)
1 row created.
SQL> / Enter value for empno: 2
Enter value for ename: Arjun
Enter value for job: ASP
Enter value for deptno: 2
Enter value for sal: 12000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
new 1: insert into emp values(2,'Arjun','ASP',2,12000)
1 row created.
SQL> / Enter value for empno: 3
Enter value for ename: Gugan
Enter value for job: ASP
Enter value for deptno: 1
Enter value for sal: 12000
old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)
new 1: insert into emp values(3,'Gugan','ASP',1,12000)
1 row created.
Q3: Update the emp table to set the salary of all employees to Rs15000/- who are
working as
ASP
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 1 12000SQL> update emp set sal=15000 where job='ASP'; 2 rows
updated.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
Q4: Create a pseudo table employee with the same structure as the table emp and
insert rows
into the table using select clauses.
Ans: SQL> create table employee as select * from emp;
Table created.
SQL> desc employee;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(6)
ENAME NOT NULL VARCHAR2(20)
JOB NOT NULL VARCHAR2(13)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Q5: select employee name, job from the emp table
Ans: SQL> select ename, job from emp;
ENAME JOB
-------------------- -------------
Mathi AP
Arjun ASP
Gugan ASP
Karthik Prof
Akalya AP
suresh lect
6 rows selected.Q6: Delete only those who are working as lecturer
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
6 suresh lect 1 8000
6 rows selected.
SQL> delete from emp where job='lect';
1 row deleted.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
-
--------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
Q7: List the records in the emp table orderby salary in ascending order.
Ans: SQL> select * from emp order by sal;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
Q8: List the records in the emp table orderby salary in descending order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
4 Karthik Prof 2 30000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
1 Mathi AP 1 10000
5 Akalya AP 1 10000Q9: Display only those employees whose deptno is 30.
Solution: Use SELECT FROM WHERE syntax.
Ans: SQL> select * from emp where deptno=1;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
3 Gugan ASP 1 15000
5 Akalya AP 1 10000
Q10: Display deptno from the table employee avoiding the duplicated values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
----------
1
2