Documente Academic
Documente Profesional
Documente Cultură
DDL COMMANDS:
DDL (Data Definition Language) statements are used to create, change the objects of a database.
Typically a database administrator is responsible for using DDL statements or production databases
in a large database system. The commands used are:
Create - It is used to create a table.
Alter - This command is used to add a new column, modify the existing column definition and
to include or drop integrity constraint.
Drop - It will delete the table structure provided the table should be empty.
Truncate - If there is no further use of records stored in a table and the structure has to be
retained, and then the records alone can be deleted.
Desc - This is used to view the structure of the table.
ALTER TABLE:
alter table <table name> add/modify (fieldname-1 datatype, fieldname-2 data type,…..
fieldname-n data type );
alter table drop column column name;
DESCRIBING TABLE:
desc<table name>;
TRUNCATE:
The truncate table statement
● removes all rows from a table
● Release the storage space used by that table
Syntax: truncate table <table name>;
DROP TABLE:
1. All data and structure in the table is deleted
2. Any pending transactions are committed.
3. All indexes are dropped.
Syntax: drop table <table name>; Table dropped.
SAMPLE OUTPUT:
SQL> create table student (sno number (2), regno number (12), name varchar2 (10), age number
(2), marks number (2));
Table created.
SQL>desc student;
Name Null? Type
-------------------- ---------------------- --------------------------
SNO NUMBER (2)
REGNO NUMBER (12)
NAME VARCHAR2 (10)
AGE NUMBER (2)
MARKS NUMBER (2)
SQL>desc student;
Name Null? Type
-------------------- --------------------- --------------------
SNO NUMBER (2)
REGNO NUMBER (12)
NAME VARCHAR2 (10)
AGE NUMBER (2)
MARKS NUMBER (2)
TOTAL NUMBER (3)
Table created.
Table created.
Table altered.
Table altered.
SQL> desc employee;
Table dropped.
ERROR:
Table truncated.
SQL>create table emp (empno number (4), ename varchar2 (10), designation varchar2 (10),
salary number (8,2));
Table created.
7. Display the column name and data type of the table employee.
Table created.
Table created.
10. Create a new table from an existing table without any record.
Table created.
Table altered.
SQL> desc emp;
Name Null? Type
----------------------------------- -------- --------
EMPNO NUMBER(6)
ENAME VARCHAR2(10)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)
12. Alter the table, emp with multiple columns (EMPNO, ENAME.)
SQL>alter table emp modify (empno number (7), ename varchar2(12));
Table altered.
Table altered.
SQL> desc emp;
Name Null? Type
----------------------------------- -------- ---------
EMPNO NUMBER(7)
ENAME VARCHAR2(12)
DESIGNATION VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR2(6)
Table altered.
15. Drop a column from an existing table emp and display it.
Table altered.
SQL> desc emp;
RESULT:
Thus the creation of a database and writing SQL queries to retrieve information from the database
was implemented.
Practice Exercises:
1. Create a table called EMP with the following structure.
Name Type
a. EMPNO NUMBER(6)
b. ENAME VARCHAR2(20)
c. JOB VARCHAR2(10)
d. DEPTNO NUMBER(3)
e. SAL NUMBER(7,2)
2. Allow NULL for all columns except ename and job.
3. Add a column experience to the emp table. Experience numeric null allowed.
4. Create a table using NOT NULL and VARCHAR constraints.
5. Create dept table with the following structure.
a. Name Type
----------- -----------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
AIM:
To Perform Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on
conditions in RDBMS.
DESCRIPTION:
Data Manipulation Language
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
1. Insert
2. Select
3. Update
4. Delete
SYNTAX :
INSERT:
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.
Inserting a single row into a table:
insert into <table name> values(fieldvalue-1,fieldvalue-2,…,fieldvalue-n);
SELECT:
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.
SELECT(att_list) FROM <table name> [WHERE <condition/expression>];
Select command to insert records: Syntax: insert into tablename( select columns from
existing_tablename);
UPDATE:
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.
update<table name> set (fieldname-1 = value, fieldname-2 = value,…,fieldname-n = value)
[WHERE <condition/expression>];
DELETE:
After inserting row in a table we can also delete them if required. The delete command consists of
afrom clause followed by an optional where clause.
delete from <table name> [where <condition/expression>];
SAMPLE OUTPUT:
INSERT, SELECT, UPDATE AND DELETE COMMANDS
SQL> create table person(pidint, lastname varchar2(10),firstnamevarchar(10), address
varchar2(20),age number);
Table created.
DELETE COMMAND
SQL> delete from person where pid=4;
1 row deleted.
SQL> /
Enter value for empno: 103
Enter value for ename: PANNERSELVAM
Enter value for DESIGNATION: ASST. PROF
Enter value for salary: 20000
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&
DESIGNATION','&SALARY')
new 1: INSERT INTO EMP VALUES(103,'PANNERSELVAM','ASST.PROF','20000')
1 row created.
SQL> /
Enter value for empno: 104
Enter value for ename: CHINNI
Enter value for DESIGNATION: HOD, PROF
Enter value for salary: 45000
old 1: INSERT INTO EMP VALUES(&EMPNO,'&ENAME','&
DESIGNATION','&SALARY')
new 1: INSERT INTO EMP VALUES(104,'CHINNI','HOD, PROF','45000')
1 row created.
SQL> SELECT * FROM EMP;
SQL> /
Enter value for eno: 333
Enter value for ename: nithya
Enter value for e_address: bombay
old 1: insert into employee values('&eno','&ename','&e_address')
new 1: insert into employee values('333','nithya','bombay')
1 row created
16. All employees of company ‘TCS’ having salary grater than 5000 are shifted to
TATA.
17. Delete all the rows from the table employee having address ‘nagpur’
RESULT
Thus the Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on
conditions in RDBMS were executed and verified.
Practice Exercise
1. Create the following tables with the mapping given below.
a. stu_details (reg_no, stu_name, address, city)
b. mark_details (reg_no, mark1, mark2, mark3, total)
(i). Display only those rows whose total ranges between 250 and 300.
(ii). Drop the table mark_details.
(iii). Delete the row whose reg_no=161.
(iv). Display all details whose names begins with 'a'.