0% found this document useful (0 votes)
325 views6 pages

Implementation of DDL Commands

The document discusses implementation of DDL and DML commands in SQL. It provides the syntax and examples of various DDL commands like CREATE TABLE, ALTER TABLE, DROP TABLE etc. It also discusses DML commands like INSERT, SELECT, UPDATE, DELETE and provides examples to implement each command. It includes questions and solutions to create tables, insert/update/select/delete records and alter table structures using the various DDL and DML commands.

Uploaded by

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

Implementation of DDL Commands

The document discusses implementation of DDL and DML commands in SQL. It provides the syntax and examples of various DDL commands like CREATE TABLE, ALTER TABLE, DROP TABLE etc. It also discusses DML commands like INSERT, SELECT, UPDATE, DELETE and provides examples to implement each command. It includes questions and solutions to create tables, insert/update/select/delete records and alter table structures using the various DDL and DML commands.

Uploaded by

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

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>));
Ex:create table emp(empno number(4) primary key, ename char(10));
2. Modifying the structure of tables.
a) Add new columns
Syntax:
Alter table <tablename> add(<new col><datatype(size),<new col>datatype(size));
Ex:alter table emp add(sal number(7,2));
3. Dropping a column from a table.
Syntax:
Alter table <tablename> drop column <col>;
Ex:alter table emp drop column sal;
4. Modifying existing columns.
Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));
Ex:alter table emp modify(ename varchar2(15));
5. Renaming the tables
Syntax:
Rename <oldtable> to <new table>;
Ex:rename emp to emp1;
6. truncating the tables.
Syntax:
Truncate table <tablename>;
Ex:trunc table emp1;
7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:drop table emp;
CREATION OF TABLE:
SYNTAX:
create table<tablename>(column1 datatype,column2 datatype...);
EXAMPLE:
SQL>create table std(sno number(5),sname varchar(20),age number(5),sdob date,sm1
number(4,2),sm2 number(4,2),sm3 number(4,4));
Table created.
SQL>insert into std values(101,‟AAA‟,16,‟03-jul-88‟,80,90,98);
1 row created.
SQL>insert into std values(102,‟BBB‟,18,‟04-aug-89‟,88,98,90);
1 row 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:
SQL>create table student(id number(5),name varchar(10),game varchar(20));
Table created.
SQL>insert into student values(1,‟mercy‟,‟cricket‟);
1 row created.
SYNTAX:
alter table<tablename>add(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student add(age number(4));
SQL>insert into student values(2,‟sharmi‟,‟tennis‟,19);
OUTPUT:
ALTER: select * from student;
ID NAME GAME

ADD: select * from student;


ID NAME GAME AGE
1 Mercy cricket
2 Sharmi Tennis 19
ALTER TABLE WITH MODIFY:
SYNTAX:
Alter table<tablename>modify(col1 datatype,col2 datatype..);
EXAMPLE:
SQL>alter table student modify(id number(6),game varchar(25));
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:
SQL>drop table student;
SQL>Table dropped.
TRUNCATE TABLE
SYNTAX: TRUNCATE TABLE <TABLE NAME>;
Example: Truncate table stud;
DESC
Example: desc emp;
Name Null? Type
--------------------------------- --------
EmpNo NOT NULL number(5)
EName VarChar(15)
Job NOT NULL Char(10)
DeptNo NOT NULL number(3)
PHONE_NO number (10)
Queries:
Q1. Create a table called EMP with the following structure.
Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.
Solution:
1. Understand create table syntax.
2. Use the create table syntax to create the said tables.
3. Create primary key constraint for each table as understand from logical table structure. Ans:
SQL> create table emp(empno number(6),ename varchar2(20)not null,job varchar2(10) not null,
deptno number(3),sal number(7,2));
Table created.
Q2: Add a column experience to the emp table.
experience numeric null allowed.
Solution:
1. Learn alter table syntax.
2. Define the new column and its data type.
3. Use the alter table syntax.
Ans: SQL> alter table emp add(experience number(2));
Table altered.
Q3: Modify the column width of the job field of emp table.
Solution:
1. Use the alter table syntax.
2. Modify the column width and its data type.
Ans: SQL> alter table emp modify(job varchar2(12));
Table altered.
SQL> alter table emp modify(job varchar(13));
Table altered.
Q4: Create dept table with the following structure.
Name Type
------------ ---------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primarykey
Solution:
1. Understand create table syntax.
2. Decide the name of the table.
3. Decide the name of each column and its data type.
4. Use the create table syntax to create the said tables.
5. Create primary key constraint for each table as understand from logical table structure. Ans:
SQL> create table dept(deptno number(2) primary key,dname varchar2(10),loc varchar2(10));

Q5: create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno > 100.
Solution:
1. Learn alter table syntax.
2. Define the new constraint [columns name type]
3. Use the alter table syntax for adding constraints.
Ans:
SQL> create table emp1(ename varchar2(10),empno number(6) constraint check(empno>100));
Table created.
Q6: drop a column experience to the emp table.
Solution:
1. Learn alter table syntax. Use the alter table syntax to drop the column.

Ans:
SQL> alter table emp drop column experience; Table altered.
Q7: Truncate the emp table and drop the dept table
Solution:
1. Learn drop, truncate table syntax.
Ans: SQL> truncate table emp; Table truncated.

IMPLEMENTATION OF DML 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 12000
SQL> 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 10000
Q9: 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

You might also like