0% found this document useful (0 votes)
1 views4 pages

SQL-DDL (1)

The document provides an overview of SQL Data Definition Language (DDL) used for defining database structures such as tables, indexes, and views. It explains the CREATE, DROP, and ALTER commands, detailing their syntax and usage with examples for creating and modifying tables and constraints. Key concepts include defining domains, integrity constraints, and the irreversible nature of dropping database objects.

Uploaded by

gbicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

SQL-DDL (1)

The document provides an overview of SQL Data Definition Language (DDL) used for defining database structures such as tables, indexes, and views. It explains the CREATE, DROP, and ALTER commands, detailing their syntax and usage with examples for creating and modifying tables and constraints. Key concepts include defining domains, integrity constraints, and the irreversible nature of dropping database objects.

Uploaded by

gbicse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

18CS440 / Database Management Systems 2022

Relational model construction using SQL

DDL - Data Definition Language


 The SQL data-definition language allows the specification of
information about relations, including:
o The schema for each relation.
o The domain of values associated with each attribute.
o Integrity constraints
 DDL is a standard for commands that define the different structures
(database objects) like Table, Index, View, Synonym, Sequence in a
database.
 DDL statements create, modify, and remove database objects such as
tables, indexes, and users.
 Common DDL statements are CREATE, ALTER, and DROP.

1. CREATE command:
 The CREATE statement is used to create an object with the description
like the domain (column), domain_type (data type) and integrity
constraints .
 Objects: Table, Index, View, Synonym, Sequence.
 Domain type: char, varchar, date, number, integer, lob, etc.
Dr. B.Subbulakshmi,CSE, TCE Page 1 of 4
18CS440 / Database Management Systems 2022

Example:
1. STUDENT( regno, name, branch )
2. MARK ( regno, test_no, sub1, sub2, sub3, result )

 CREATE TABLE student


(regno char(6) constraint p2 primary key, // column level constraint

name varchar(20) not null,


branch char(4), constraint c1 check (regno='%c%'));

 CREATE TABLE mark


(regno char(6), sub1 number(4,2), sub2 number(4,2),
sub3 number(4,2), result char(4) ,
constraint f2 foreign key(regno) references student2(regno) on delete
cascade / on delete set null); // table level constraint

 SELECT constraint_name, constraint_type, search_condition from


user_constraints where table_name= 'STUDENT';

CONSTRAINT_NAME C SEARCH_CONDITION
--------------------------------------------------------------------------------
SYS_C001362 C "NAME" IS NOT NULL
P2 P

Dr. B.Subbulakshmi, CSE, TCE Page 2 of 4


18CS440 / Database Management Systems 2022

 CREATE TABLE student1


(regno char(6) constraint u1 unique ,
name varchar(20) not null,
branch char(4), constraint c1 check(regno='%c%')); // table level constraint

 SELECT constraint_name,constraint_type,search_condition from


user_constraints where table_name= 'STUDENT1';

CONSTRAINT_NAME C SEARCH_CONDITION
--------------------------------------------------------------------------------
SYS_C001368 C "NAME" IS NOT NULL
F2 R
U1 U

2. DROP command:
 The SQL DROP statement is used to remove an object from the
database.
 If you drop a table, all the rows in the table are deleted and the table
structure is removed from the database.
 Once a table is dropped we cannot get it back.
 Example:
o DROP TABLE student;
o DROP TABLE mark;

Dr. B.Subbulakshmi, CSE, TCE Page 3 of 4


18CS440 / Database Management Systems 2022

3. ALTER command:
 The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
 The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
 ALTER – ADD option
ALTER TABLE table_name ADD (column_1 column-definition,
column_2 column-definition, ... column_n column_definition);
 ALTER – MODIFY option
ALTER TABLE table_name MODIFY (column_1 column_type,
column_2 column_type, ... column_n column_type);
 ALTER – DROP option
ALTER TABLE table_name DROP COLUMN column_name;
(not able to drop multiple columns at a time)
Example:
 ALTER table student DROP primary key cascade;
 CREATE TABLE student1
(regno char(6),name varchar(20),branch char(4));
 ALTER TABLE student1 ADD constraint p1 primary key (regno);
**************

Dr. B.Subbulakshmi CSE, TCE Page 4 of 4

You might also like