Chapter 7
Chapter 7
Contents
Data Definition Language
Data Manipulation Language
Basic SQL Queries
1
SQL Data Definition
• SQL uses the terms table, row, and column for the formal
relational model terms relation, tuple, and attribute,
respectively.
• The main SQL command for data definition is the CREATE
statement, which can be used to create schemas, tables
(relations), and domains.
The CREATE TABLE Command in SQL
The CREATE TABLE command is used to specify a new
relation by giving it a name and specifying its attributes and
initial constraints or their data types (INTEGER, FLOAT,
DECIMAL(i.j), CHAR(n), VARCHAR(n)).
The attributes are specified first, and each attribute is given a
name, a data type to specify its domain of values, and any
attribute constraints, such as NOT NULL. 2
Example for CREATE TABLE
1. CREATE TABLE EMPLOYEE
( Fname VARCHAR(15) NOT NULL,
Minit CHAR,
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR,
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) ); 3
Con’t…
2. CREATE TABLE DEPARTMENT
( Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES
EMPLOYEE(Ssn) );
4
SQL Data Manipulation Language
5
Con’t…
• Basic form of the SQL SELECT statement is called a mapping
or a SELECT-FROM-WHERE block
6
Employee table
7
Department table
8
Con’t…
• Query 0: Retrieve the birthdate and address of the employee
whose name is 'John B. Smith'.
10
B. DELETE Command
• Removes tuples from a relation.
• Includes a WHERE-clause to select the tuples to be deleted
Tuples are deleted from only one table at a time.
11
Con’t…
• Examples:
12
C. UPDATE Command
• Used to modify attribute values of one or more selected tuples.
• A WHERE-clause selects the tuples to be modified.
• An additional SET-clause specifies the attributes to be
modified and their new values.
• Each command modifies tuples in the same relation.
• Example: Change the location and controlling department
number of project number 10 to 'Bellaire' and 5, respectively.
13
Con’t…
• Example: Give all employees in the 'Research' department a
10% raise in salary.
14
THE END!!
15