Chapter 1 - Advanced SQL
Chapter 1 - Advanced SQL
?!
CREATE TABLE DEPARTMENT (
Dnumber int primary key, Order
Dname varchar(30) not null,
Mgr_ssn char(10) foreign key references EMPLOYEE (Ssn),
Mgr_start_date smalldatetime,
)
CREATE TABLE command in SQL
EMPLOYEE (Ssn, Fname, Minit, Lname, Bdate, Address, Sex, Salary, Super_ssn, Dno)
PROJECT (Pnumber, Pname, Plocation, Dnum)
WORKS_ON (Essn, Pno, Hours)
CREATE TABLE WORKS_ON (
Primay key has
Essn
Pno
char(10),
int, ?! some attributes
Hours int not null,
primary key (Essn, Pno)
foreign key (Essn) references EMPLOYEE (Ssn),
foreign key (Pno) references PROJECT (Pnumber),
)
CREATE TABLE command in SQL
Specifying Constraints in SQL
▪ NOT NULL
o NULL is not permitted for a particular attribute
▪ Default value
o DEFAULT <value>
▪ CHECK clause
o Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21);
o Score float Check (Score>=0 and Score<=10)
Specifying Constraints in SQL
▪ PRIMARY KEY clause
o Specifies one or more attributes that make up the primary key of a
relation
o Dnumber INT PRIMARY KEY;
▪ UNIQUE clause
o Specifies alternate (secondary) keys
o Dname VARCHAR(15) UNIQUE;
Specifying Constraints in SQL
▪ FOREIGN KEY clause
o Default operation: reject update on violation
o Attach referential triggered action clause
✓ Options include SET NULL, CASCADE, and SET DEFAULT
✓ Action taken by the DBMS for SET NULL or SET DEFAULT is the same
for both ON DELETE and ON UPDATE
✓ CASCADE option suitable for “relationship” relations
INSERT, DELETE, and UPDATE Statements in SQL
▪ Three commands used to modify the database: INSERT, DELETE,
UPDATE
INSERT Command in SQL
▪ Specify the relation name and a list of values for the tuple
INSERT INTO <Table Name> [(<List of attribute>)] VALUES (<List of value>)
INSERT INTO <Table Name> <Select query>
DELETE Command in SQL
▪ Removes tuples from a relation
o Includes a WHERE clause to select the tuples to be deleted
DELETE FROM <Table Name> [WHERE <Criteria>]