Experiment 2: AIM: To Execute The Queries in DDL Commands. Theory
Experiment 2: AIM: To Execute The Queries in DDL Commands. Theory
a) Create:
Create command is used to create a new table in database.
Syntax:
Example:
create table student( sname varchar(20),sidint(5),sclassint(1),sphoneint(10));
b) Alter:
The Alter table command is used to add, delete, modify columns, add or drop various constraints on
an existing table. In other words, Alter command is used to change the structure of the table.
Example:
Alter table student add slnamevarchar(30);
Example:
Alter table student drop column section;
Example:
Alter table student modify column sidvarchar(10);
c) Drop:
This statement is used to remove a table definition and all the data, indexes, triggers, constraints and
permission specifications for that table.
Syntax:
DROP TABLE<table_name>
Example:
Drop table student;
d) Truncate:
This command is used to delete complete data from table. However, the structure of table is
retained.
Syntax:
TRUNCATE TABLE <table_name>
PROGRAM:
mysql>>desc employee;
mysql>>desc employee;
mysql>>desc employee;
mysql>>alter table employee modify column salary int(7);
query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>>desc employee;
Theory:
Constraints are the rules that are used to limit the type of data that can go into a table, to maintain
accuracy and integrity of the data inside table. There are two types of constraints:
Column level constraints and Table level constraints depending upon the range up to which their
conditions hold. Constraints are used to make sure that the integrity of data is maintained. The
following are various constraints:
a) Primary Key:
Primary key constraint is used to uniquely identify each record in a database. A primary key must
contain a unique value and it must not contain a null value. Primary key is usually used to index
data inside a table.
Example:
Create table student (sidint(5) primary key, sname varchar(30), sdept varchar(4));
Example:
Alter table employee add primary key (eid);
b) Foreign Key:
Foreign Key is used to establish relation between two tables. It is used to ensure referential
integrity between tables. The foreign key is usually a column that is linked with the primary key of
parent table. And once a column is declared to be as foreign key, it doesn’t allow inclusion of details
which are not present in the primary key of parent table. In this way it ensures referential integrity.
Example:
Alter table employeebonus add foreign key(esuperid) references employee(eid);
Adding foreign key while creating the table:
c) Not Null:
NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint
is applied to a column, you cannot pass a null value to that column. It enforces a column to contain
a proper value.
Syntax:
Create table <table-name>( column_1 datatype(size) NOT NULL, column_2 datatype(size)
…column_n datatype(size));
Example:
Create table student (sidint(5) NOT NULL, sname varchar(30), sdept varchar(3));
d) Unique:
Unique constraint prevents the user from adding duplicate values in the column.
Example:
CREATE TABLE Persons ( IDint NOT NULL, LastName varchar(25) NOT NULL,FirstName
varchar(25),Age int,UNIQUE (ID));
Example:
Alter table persons add unique (ID);
e) Check:
CHECK constraint is used to restrict the value of a column within a range. It performs check on the
values, before storing them into the database. It is like condition checking before saving data into a
column.
Example to apply check constraint while creating the table:
Create table student(sidint(5) NOT NULL Check(sid>0), sname varchar(30) NOT NULL, age
int(2));
PROGRAM:
mysql>>desc department;
mysql>>desc department;
mysql>> alter table dept_locations add foreign key (Dnumber) references department
(Dnumber);
Query OK, 0 rows affected (0.97 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>descdept_locations;
mysql>desc project;