Ex no: Date: / / Dr. N.G.
P Institute of Technology Reg no:710721205043
Data Definition Commands, Data Manipulation Commands for inserting,
deleting, updating and retrieving Tables
Aim:
To create database and perform DML queries to retrieve information from the
database.
Different types of commands in SQL:
• DDL commands: - To create a database object.
• DML commands: - To manipulate data of a database objects.
Data Definition Language (DDL):
To specify the database schema.
DDL Commands:
• Create
• Alter
• Rename
• Drop
Creation of Table:
Creates a table with specified attributes and its data type along with
constraints.
Syntax:
CREATE TABLE TABLE_NAME(A1 D1, A2 D2, . . . AN DN)
Here Ai is the attribute of the table,
Di is the data type of the attribute
View the table structure:
Syntax:
desc r;
Code:
CREATE TABLE student (s_id int,s_name varchar(30),s_dept varchar(40),s_street varchar(20),s_city
varchar(30),primary key(s_id));
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
Desc student;
Retrieval of Information by Select Command:
Syntax-
(i)Select A1,A2.. ,An from r1
(ii) Select * from r1;
(iii) Select A1,A2.. ,An from r1,r2,..,rn;
(iv) Select A1,A2.. ,An from r1,r2,..,rn where condition;
Code:
SELECT * FROM student;
SELECT s_id,s_name from student where s_name=’sahana’;
Altering the Table Schema:
Add or delete or change the attribute and data type.It also can add or drop
the integrity constraints.
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
Syntax:
1. Alter table r add (Ai Di);
2. Alter table r drop(Ai);
3. Alter table r modify(Ai Di);
4. Alter table r add primary key(Ai);
5. Alter table r enable primary key;
6. Alter table r disable primary key;
Code:
Alter table student modify (s_name varchar2(40));
ALTER TABLE student DROP(s_city);
Rename:
Change the table name
Syntax:
Rename <tablename> to <newtablename>;
Code:
RENAME student to learner;
Dropping the Table:
Deletes all information and structure about that table(relation)
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
Syntax:
Drop table r;
Code:
Drop table learner;
Data Manipulation Language (DML):
To express database queries and updates.
List of DML Commands:
1. Insertion of information
2. Retrieval of information
3. Deleting information
4. Modifying information
Two types of DML:
1. Procedural DMLs – requires a user to specify what data are needed and how
to get
those data.
2. Declarative DMLs ( nonprocedural language) - requires a user to specify
what data are
needed without specifying how to get those data.
DML Component of SQL language is nonprocedural language.
Query is a statement requesting the retrieval of information.
Insertion of information can be done in two ways -
Syntax-
Insert into r values(V1, V2,...,Vn); // where Vi – Attribute Values
r - relation name (Table name)
Insert into r values(‘&A1’,’&A2’,....,’&An’); // where Ai – Attribute
Example :
SQL> insert into student values(1,'sanjana','IT','singanallur','cbe');
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
1 row created.
SQL> insert into student values(2,'punitha','IT','kalapatti','cbe');
1 row created.
SQL> insert into student values(3,'sahana','IT','ukkadam','cbe');
1 row created.
SQL> insert into student values(4,'varshini','BME','kovilpalayam','cbe');
1 row created.
SELECT * from learner;
UPDATE:
The UPDATE command updates some or all data values in a database. It can
update one or more records in a table. The UPDATE command specifies the
rows to be changed using the WHERE clause and the new data using the SET
keyword.
Syntax:
UPDATE table_name SET column-name = value, column-name = value,...WHERE condition;
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
Example:
UPDATE learner SET S_dept='CSE' where Sname='punitha';
DELETE:
The DELETE command permanently removes one or more records from the
table. It removes the entire row, not individual fields of the row, so no field
argument is needed.
Syntax:
DELETE FROM table-name WHERE condition;
Example:
DELETE FROM learner WHERE s_id=3;
RESULT:
Thus the DDL, DML queries are executed successfully and the output is
verified
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
CONSTRAINTS – PRIMARY KEY, UNIQUE, CHECK, DEFAULT and NOT NULL
AIM:
To perform certain constraints(PRIMARY KEY, UNIQUE KEY,
CHECK, DEFAULT and NOT NULL) to the table.
PRIMARY KEY:
The PRIMARY KEY constraint uniquely identifies each record in a database
table. Each table should have a primary key, and each table can have only one
primary key.
Syntax:
CREATE TABLE table name
(ATTRIBUTE d_type PRIMARY KEY);
To add a PRIMARY KEY constraint, use the following SYNTAX:
ALTER TABLE table_name ADD PRIMARY KEY (ATTRIBUTE);
To drop a PRIMARY KEY constraint, use the following SYNTAX:
ALTER TABLE table_name DROP PRIMARY KEY
UNIQUE:
The UNIQUE constraint uniquely identifies each record in a database table. The
UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.
Syntax:
CREATE TABLE table_name(
ATTRINUTE d_type UNIQUE);
To create a UNIQUE constraint when the table is already created, use the
following Syntax:
ALTER TABLE TABLE_NAME ADD UNIQUE (ATTRUBITE);
CONSTRAINTS – PRIMARY KEY, UNIQUE, CHECK, DEFAULT and NOT NULL
NOT NULL:
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. The
following SQL query column to not accept NULL values:
CREATE TABLE table_name(ATTRIBUTE d_type NOT NULL);
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
DEFAULT:
The DEFAULT constraint is used to insert a default value into a column. The
following SQL creates a DEFAULT constraint on the table is created:
CREATE TABLE Persons(ATTRIBUTE d_type DEFAULT value);
To create a DEFAULT constraint on the column when the table is already
created use the following SQL Query:
ALTER TABLE table_name add (ATTRIBUTE d_type DEFAULT value);
To drop a DEFAULT constraint, use the following SQL:
ALTER TABLE table-name ALTER ATTRIBUTE DROP DEFAULT;
CHECK:
The CHECK constraint is used to limit the value range that can be placed in a
column. If you define a CHECK constraint on a single column it allows only
certain values for this column. The following SQL creates a CHECK constraint
on the table is created.
CREATE TABLE TABLE_NAME(ATTRIBUTE d_type CHECK condition);
To add a CHECK constraint, use the following SQL:
ALTER TABLE table_name ADD CHECK (condition);
Example:
CREATE TABLE emp (id int,name varchar(50),dob varchar(50),phno int);
desc emp;
Now, let’s add on the table we created previously.
SQL> ALTER TABLE emp ADD PRIMARY KEY (id);
Table altered.
SQL> ALTER TABLE emp ADD UNIQUE (phno);
Table altered.
SQL> ALTER TABLE emp add (regno varchar(1000) DEFAULT 101);
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
Table altered.
SQL> ALTER TABLE emp ADD CHECK (id>0);
Table altered.
The values that are updated ealier.
UNIQUE:
insert into emp values (4,'arun','20-1-2003',8934710834,710721205021);
PRIMARY KEY/NOT NULL:
INSERT into emp values (6,'tarun','17-10-2003',9934568834,710721205022);
CHECK:
INSERT into
Since the default value is 100 the price is automatically added
UPDATE emp SET id = 5 where name='arun';
DELETE:
DELETE FROM emp WHERE id=5;
RESULT:
Thus, the constraints(PRIMARY KEY, UNIQUE KEY, CHECK, DEFAULT and NOT NULL) are add to the
table successfully and checked.
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
FOREIGN KEY CONSTRAINTS
AIM:
To create a set of tables, add foreign keys and incorporate referential
integrity.
FOREIGN KEY:
The FOREIGN KEY constraint is used to prevent actions that would destroy
link between tables. The FOREIGN KEY constraint also prevents that invalid
data is inserted into the foreign key column, because it has to be one of the
values contained in the table it points to A FOREIGN KEY in one table points
to a PRIMARY KEY in another table.
SYNTAX:
CREATE TABLE (TABLENAME)( COLUMNNAME DATATYPE, PRIMARY KEY (COLUMNNAME), FOREIGN
KEY (COLUMNNAME) REFERENCES (1ST TABLENAME) (COLUMNNAME));
EXAMPLE:
CREATE:
SQL> create table dept (d_no int primary key,d_name varchar(15),course_code int,course_title
varchar(15));
SQL> create table student1(id int,name varchar(15),d_id int,cgpa int,Foreign key(id) References
Dept(d_no));
Ex no: Date: / / Dr. N.G.P Institute of Technology Reg no:710721205043
INSERT:
Parent key Error:
SQL> insert into dept values(12,'CSE','34','chemistry');
1 row created.
SQL> insert into dept values(13,'IT','35','dpco');
1 row created.
SQL> insert into dept values(14,'BME','36','OS');
1 row created.
ALTER:
SQL> ALTER TABLE student1 modify(name varchar(25));
Table altered.
RESULT:
Thus the table is created, foreign key constraint and incorporate referential integrityqueries are
added and checked.