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>;
Ex:rename emp to emp1;
Syntax:
Truncate table <tablename>;
7. Destroying tables.
Syntax:
Drop table <tablename>;
CREATION OF TABLE:
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
Description
The Oracle ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The Oracle
ALTER TABLE statement is also used to rename a table.
Example
Let's look at an example that shows how to add a column in an Oracle table using the ALTER TABLE
statement.
For example:
ALTER TABLE customers
ADD customer_name varchar2(45);
This Oracle ALTER TABLE example will add a column called customer_name to the customers table that is a
data type of varchar2(45).
In a more complicated example, you could use the ALTER TABLE statement to add a new column that also has
a default value:
ALTER TABLE customers
ADD city varchar2(40) DEFAULT 'Seattle';
In this example, the column called city has been added to the customers table with a data type of varchar2(40)
and a default value of 'Seattle'.
Add multiple columns in table
Syntax
To ADD MULTIPLE COLUMNS to an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
ADD (column_1 column_definition,
column_2 column_definition,
...
column_n column_definition);
Example
Let's look at an example that shows how to add multiple columns in an Oracle table using the ALTER TABLE
statement.
For example:
ALTER TABLE customers
ADD (customer_name varchar2(45),
city varchar2(40) DEFAULT 'Seattle');
This Oracle ALTER TABLE example will add two columns, customer_name as a varchar2(45) field and city as
a varchar2(40) field with a default value of 'Seattle' to the customers table.
Example
Let's look at an example that shows how to modify a column in an Oracle table using the ALTER TABLE
statement.
For example:
ALTER TABLE customers
MODIFY customer_name varchar2(100) NOT NULL;
This Oracle ALTER TABLE example will modify the column called customer_name to be a data type of
varchar2(100) and force the column to not allow null values.
In a more complicated example, you could use the ALTER TABLE statement to add a default value as well as
modify the column definition:
ALTER TABLE customers
MODIFY city varchar2(75) DEFAULT 'Seattle' NOT NULL;
In this example, the ALTER TABLE statement would modify the column called city to be a data type of
varchar2(75), the default value would be set to 'Seattle' and the column would be set to not allow null values.
Modify Multiple columns in table
Syntax
To MODIFY MULTIPLE COLUMNS in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
...
column_n column_type);
Example
Let's look at an example that shows how to modify multiple columns in an Oracle table using the ALTER
TABLE statement.
For example:
ALTER TABLE customers
MODIFY (customer_name varchar2(100) NOT NULL,
city varchar2(75) DEFAULT 'Seattle' NOT NULL);
This Oracle ALTER TABLE example will modify both the customer_name and city columns. The
customer_name column will be set to a varchar2(100) data type and not allow null values. The city column will
be set to a varchar2(75) data type, its default value will be set to 'Seattle', and the column will not allow null
values.
Example
Let's look at an example that shows how to drop a column in an Oracle table using the ALTER TABLE
statement.
For example:
ALTER TABLE customers
DROP COLUMN customer_name;
This Oracle ALTER TABLE example will drop the column called customer_name from the table called
customers.
Example
Let's look at an example that shows how to rename a column in an Oracle table using the ALTER TABLE
statement.
For example:
ALTER TABLE customers
RENAME COLUMN customer_name TO cname;
This Oracle ALTER TABLE example will rename the column called customer_name to cname.
Rename table
Syntax
To RENAME A TABLE, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example
Let's look at an example that shows how to rename a table in Oracle using the ALTER TABLE statement.
For example:
ALTER TABLE customers
RENAME TO contacts;
This Oracle ALTER TABLE example will rename the customers table to contacts.
The following Oracle ALTER TABLE statement would rename the departments table to depts:
ALTER TABLE departments
RENAME TO depts;
The following Oracle ALTER TABLE statement would add a bonus column to the employees table:
ALTER TABLE employees
ADD bonus number(6);
The following Oracle ALTER TABLE statement would add the contact_name and last_contacted columns to
the customers table:
ALTER TABLE customers
ADD (contact_name varchar2(50),
last_contacted date);
The following Oracle ALTER TABLE statement would change the datatype for the employee_name column to
varchar2(75):
ALTER TABLE employees
MODIFY employee_name varchar2(75);
The following Oracle ALTER TABLE statement would modify the customer_name and state columns
accordingly in the customers table:
ALTER TABLE customers
MODIFY (customer_name varchar2(50) NOT NULL,
state varchar2(2));
The following Oracle ALTER TABLE statement would drop the salary column from the employees table:
ALTER TABLE employees
DROP COLUMN salary;
The following Oracle ALTER TABLE statement would rename the department_name column to dept_name in
the departments table:
ALTER TABLE departments
RENAME COLUMN department_name TO dept_name;