W01 11 CreateAlterDrop
W01 11 CreateAlterDrop
Topics
Essential syntax to use
DDL – data definition language
Data types supported by SQL standard
How to define tables and modify its
structure
How to define integrity constraints
in SQL
1
Data Definition Language (DDL)
Tables and other database objects exist in an
environment
Each environment contains >=1 catalogs,
and each catalog consists of set of schemas
Schema is named collection of related
database objects with the same owner
Objects in a schema can be tables, views,
security, domains, assertions, collations,
translations, and character sets
DROP TABLE
DROP TABLE TableName
[ RESTRICT | CASCADE [CONSTRAINTS] ]
e.g. DROP TABLE
PropertyForRent;
Removes named table and all rows within it
CREATE TABLE
CREATE TABLE PropertyForRent
(propNo VARCHAR2(5) NOT NULL UNIQUE
, ...
,rooms NUMBER(2,0) DEFAULT 4 NOT NULL
,rent NUMBER(6,2) DEFAULT 600 NOT NULL
CHECK(rent BETWEEN 0 AND 9999.99)
,ownerNo VARCHAR2(5) NOT NULL
,staffNo VARCHAR2(5) NOT NULL
,branchNo CHAR(4) NOT NULL
,PRIMARY KEY (propNo)
,FOREIGN KEY (staffNo) REFERENCES Staff (staffNo)
ON DELETE SET NULL ON UPDATE CASCADE
, ...
,CONSTRAINT chkrms CHECK(rooms BETWEEN 1 AND 15)
...);
Keith Tang COMP2714 6
2
SQL Data Types
CHAR(9)
VARCHAR(30) VARCHAR2(30)
DECIMAL(10,2) NUMBER(10,2)
DATE (Oracle variant)
Keith Tang COMP2714 8
3
CREATE TABLE
Creates a table with one or more columns of the
specified data type
With NOT NULL, system rejects any attempt to
insert a null in the column
Can specify a DEFAULT value for the column
Primary keys should always be specified as
NOT NULL
FOREIGN KEY clause specifies FK along with the
referential actions
CREATE TABLE
CREATE TABLE PropertyForRent
(propNo VARCHAR2(5) NOT NULL UNIQUE
, ...
,rooms NUMBER(4,0) DEFAULT 4 NOT NULL
,rent NUMBER(6,2) DEFAULT 600 NOT NULL
CHECK(rent BETWEEN 0 AND 9999.99)
,ownerNo VARCHAR2(5) NOT NULL
,staffNo VARCHAR2(5) NOT NULL
,branchNo CHAR(4) NOT NULL
,PRIMARY KEY (propNo)
,FOREIGN KEY (staffNo) REFERENCES Staff (staffNo)
ON DELETE SET NULL ON UPDATE CASCADE
, ...
,CONSTRAINT chkrms CHECK(rooms BETWEEN 1 AND 15)
...);
Keith Tang COMP2714 12
4
Integrity Constraints
Consider 5 types of integrity constraints:
Required data: NOT NULL
position VARCHAR2(10) NOT NULL
Domain constraints
Entity integrity – Primary Key
Referential integrity – Foreign Key
Enterprise constraints
CHECK constraints
Triggers
CONSTRAINT keyword – only if naming
the constraint
Keith Tang COMP2714 13
Entity Integrity
Primary key of a table must contain a unique,
non-null value for each row.
ISO standard supports PRIMARY KEY clause in
CREATE and ALTER TABLE statements:
PRIMARY KEY(staffNo)
PRIMARY KEY(clientNo, propertyNo)
Can only have one PRIMARY KEY clause per
table; can still ensure uniqueness for alternate
keys using UNIQUE:
UNIQUE(telNo)
Referential Integrity
FK is column or set of columns that relates
each row in child table containing FK to row of
parent table with matching PK or unique key
Referential integrity means that, if FK contains
a value, that value must reference an existing
row in parent table
Definition of FKs with FOREIGN KEY clause in
CREATE and ALTER TABLE:
CONSTRAINT fk_branchNo
FOREIGN KEY(branchNo) REFERENCES Branch
FOREIGN KEY(orderNo, prodNo) REFERENCES
OrderDetail (orderNo, productNo)
Keith Tang COMP2714 15
5
Referential Integrity
Any INSERT/UPDATE that attempts to create FK
value in child table without matching PK or
unique key value in parent will be rejected
Action taken that attempts to update/delete a
PK or unique key value in parent table with
matching rows in child is dependent on
referential action specified using
ON UPDATE and ON DELETE subclauses:
- CASCADE - NO ACTION
- SET DEFAULT - SET NULL
Note: Oracle’s limited support in this
Keith Tang COMP2714 16
Referential Integrity
NO ACTION: Reject delete from parent - Default
CASCADE: Delete row from parent and delete
matching rows in child, and so on in cascading
manner
SET NULL: Delete row from parent and set FK
column(s) in child to NULL; only valid if FK columns
are defined as NULL allowed
SET DEFAULT: Delete row from parent and set each
component of FK in child to specified default; only
valid if DEFAULT value is specified for each FK
column involved
CHECK Constraints
[CONSTRAINT constraint_name]
CHECK (condition);
Minimum or maximum values
CONSTRAINT chk_rent
CHECK(rent <= 9999.99)
Specific list of values
CONSTRAINT chk_branchNo
CHECK(branchNo IN ('BR09','BR22','BR17'))
Range of values
CONSTRAINT chk_rent
CHECK(rent BETWEEN 0 AND 9999.99)
6
ALTER TABLE
Add a new column to a table
Drop a column from a table
Add a new table constraint
Drop a table constraint
Set a default for a column
Drop a default for a column
ALTER TABLE
ALTER TABLE MyTable
ADD column_b VARCHAR(20) NULL;
ALTER TABLE MyTable
DROP COLUMN column_b;
ALTER TABLE MyTable
ADD
column_b VARCHAR(20) NULL
CONSTRAINT exb_unique UNIQUE;
ALTER TABLE MyTable WITH NOCHECK
ADD
CONSTRAINT exa_check
CHECK (column_a > 1);
Keith Tang COMP2714 21
7
ALTER TABLE
ALTER TABLE MyTable
/* Add a PRIMARY KEY identify column */
ADD column_b NUMBER(5) NOT NULL
CONSTRAINT column_b_pk PRIMARY KEY,
-- Add a column referencing another
-- column in the same table
column_c NUMBER(5) NULL
CONSTRAINT column_c_fk
REFERENCES MyTable(column_b);
Use multiple ALTER TABLE statements for
better readability and maintainability
Do NOT interleave comments inside an SQL
statement – reduced readability!
Keith Tang COMP2714 22