| |
SQL is a non-procedural language. |
| Features: |
|
It is a non-procedural language. |
|
It is an English-like language. |
|
It can process a single record as well as sets
of records at a time. |
|
It is different from a
third generation language (C & COBOL). All
SQL statements define what is to be done rather
than how it is to be done. |
|
SQL is a data sub-language consisting of three
built-in languages: |
|
o Data Definition Language (DDL).
o Data Manipulation Language (DML).
o Data Control Language (DCL). |
|
It insulates the user from the underlying structure
and algorithm. |
|
SQL has facilities for
defining database views, security, integrity
constraints, transaction controls, etc. |
| DATA
DEFINITION LANGUAGE (DDL) : |
| |
The DDL defines a set of
commands used in the creation and modification
of schema objects such as tables, indexes, views,
etc. These commands provide the ability to create,
alter and drop these objects. These commands
are related to the management and administration
of the databases. |
| CREATE
TABLE : |
| |
CREATE TABLE <TABLE
NAME>(
COLUMN_NAME1 DATA TYPE (COLUMN WIDTH)[CONSTRAINTS],
COLUMN_NAME2 DATA TYPE (COLUMN WIDTH)[CONSTRAINTS],
……………………………………………………………………………….
);
where table name assigns the name of the table,
column name defines the name of the field, data
type specifies the data type for the field and
column width allocates specified size to the
field. |
| Note: |
| |
• Table name should start with an alphabet.
• In table name, blank spaces and single
quotes are not allowed. • Reserve words
of that DBMS can not be used as table name.
• Proper data types and size should be specified.
• Unique column name should be specified. |
| Different column constraints
are : |
| |
• NOT NULL • UNIQUE •
PRIMARY KEY • CHECK • DEFAULT
• REFERENCES |
| Examples: |
| |
CREATE TABLE product(
pno number(4) PRIMARY KEY,
pname char(30) NOT NULL,
qoh number(5) DEFAULT (100),
class char(1) NOT NULL,
rate number(8,2) NOT NULL,
CHECK((class= ‘A’ AND rate < 4500)
OR
(class = ‘C’ AND rate > 4500))
); CREATE TABLE prodtrans(
ppno number(4),
ptype char(1) CHECK(ptype in(‘P’,
‘S’)),
qty number(5),
FOREIGN KEY ppno REFERENCES product(pno)
ON DELETE CASCADE);
|
| On delete Cascade: |
| |
Using this option whenever
a parent row is deleted in a referenced table
then all the corresponding child rows are deleted
from the referencing table. This constraint
is a form of referential integrity constraint. |
| ALTER
TABLE : |
| |
This command is used for modification of existing
structure of the table in the following situation: |
| |
• When a new column is to be added to
the table structure. • When the existing
column definition has to be changed, i.e., changing
the width of the data type or the data type itself.
• When integrity constraints have to be
included or dropped. • When a constraint
has to be enabled or disabled. |
| |
ALTER TABLE <table name> ADD(<column
name> <data type> …);
ALTER TABLE <table name> MODIFY
(<column name> < data type>…);
ALTER TABLE <table name> ADD
CONSTRAINT < constraint name> <constraint
type> (field name);
ALTER TABLE <table name> ENABLE/DISABLE
<constraint name>;
ALTER TABLE <table name> DROP
<constraint name>;
|
| DROP
TABLE : |
| |
To delete the existing object from the database.
DROP TABLE <table name>; |