DDL DML Complete
DDL DML Complete
Outline
• Introduction
• Data Definition Language
• CREATE TABLE
• ALTER TABLE
• DROP TABLE
• Data Manipulation Language
• SELECT Statement
• INSERT Statement
• DELETE Statement
• UPDATE Statement
• Assertions
• Views
• The user has just to specify “what” results he/she would like,
leaving the “how” to the DBMS.
• Where:
• [schema.]: schema name followed by a dot.
• name: table name.
• column-1 to column-n: column names.
• TYPE: data type.
• [DEFAULT value]: optional default value.
• [constraints]: optional constraints, can be specified at column
level or at table level.
Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views 7
Some SQL Data Types (From Oracle)
• CHAR(n): a fixed-length string n characters long
(default 1, max 255 character).
• VARCHAR(n): a variable string up to n character long (max 2000).
• INT: an integer number.
• DECIMAL(n,d): a number n digits long including d decimal
places.
• DATE: a valid date YYYY-MM-DD.
• This makes it easier to change the data type for a domain that is
used by numerous attributes in a schema, and improves schema
readability.
• Examples:
• CREATE DOMAIN SSN_TYPE AS CHAR(9);
• CREATE DOMAIN D_NUM AS INT CHECK
(D_NUM>0 AND D_NUM<21);
• Syntax:
DROP SCHEMA name [CASCADE or RESTRICT]
• Where:
• name: is the name of the schema.
• CASCADE: removes the schema and all its tables, domains, and
other elements.
• RESTRICT: removes the schema only if it has no elements.
• Syntax:
DROP TABLE name [CASCADE CONSTRAINTS]
• Where:
• name: is the name of the table to be dropped.
• CASCADE CONSTRAINTS: drops all referential integrity
constraints which refer to keys in the dropped table.
• Where:
• table: the name of the table to update.
• attribute-list: a list of attributes in the specified table.
• value-list: the list of values to be inserted for the row.
• Example:
INSERT INTO PROJECT (PNAME, PNUMBER, DNUM)
VALUES ('ProductZ', 1, 2)
• Example:
INSERT INTO PROJECT (PNAME, PNUMBER, DNUM)
VALUES ('&PN', &P_NO, &D_NO)
Each time it is run, the user will be
prompted for a new set of values
• Example:
Update EMPLOYEE
SET (Salary)
=
( SELECT Salary
FROM EMPLOYEE
WHERE Ssn='987654321')
WHERE Fname = 'John' AND Lname='Smith';
Chapter 8: SQL-99: Schema Definition, Constraints, Queries, and Views 68
Basic Queries in SQL
• SQL has one basic statement for retrieving data from a database,
the SELECT statement.
• Query: for each employee, retrieve the employee’s first and last
name and the first and last name of his or her immediate supervisor.
SELECT E.Fname, E.Lname, S.Fname, S.Lname
FROM EMPLOYEE E, EMPLOYEE S
WHERE E.Super_ssn=S.Ssn;
• Query: make a list of all project numbers for projects that involve
an employee whose last name is ‘Smith’, either as a worker or as
a manager of the department that controls the project.
SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Union
Lname='Smith' compatible
UNION
SELECT DISTINCT Pnumber
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber=Pno AND Essn=Ssn AND Lname='Smith';