You are on page 1of 67

Creating Databases & Tables

,
Constraints


Relational Query Languages
• A major strength of the relational model: supports
simple, powerful querying of data.
• Two sublanguages:
• DDL – Data Definition Language
– define and modify schema (at all 3 levels)
• DML – Data Manipulation Language
– Queries can be written intuitively.
• The DBMS is responsible for efficient evaluation.
– The key: precise semantics for relational queries.
– Allows the optimizer to extensively re-order operations,
and still ensure that the answer does not change.
– Internal cost model drives use of indexes and choice of
access paths and physical operators.


The SQL Query Language
• The most widely used relational query language.
• Originally IBM, then ANSI in 1986
• Current standard is SQL-2003
• Introduced XML features, window functions,
sequences, auto-generated IDs.
• Not fully supported yet
• SQL-1999 Introduced “Object-Relational” concepts.
Also not fully suppored yet.
• SQL92 is a basic subset
• Most systems support a medium
• PostgreSQL has some “unique” aspects (as do
most systems).

Introduction to SQL
• A standard language used in most DBMS.
– Well, not as standardized as one might hope
• it keeps involving and growing
• Vendors have the tendency to add “unique” features.
• Pronounced as “S-Q-L” or “Sequel.”
• Both as a DDL and DML language.
– DDL (Data Definition Language): define the schema
of the database.
– DML (Data Manipulation Language): provides
commands to manipulate the database (query,
insert, update, delete).
SQL Continued
• Based on relational algebra, but not entirely
identical.
– Relations Tables
– Tuples Rows
– Attributes Columns
• Unlike a relation, a table is not a set. Duplicates are
not automatically removed.
– This is for practical reasons. Duplicate eliminations are
inefficient in implementation.
• Like a relation, the order of rows in a table is
irrelevant.
SQL
• It has 9 commands common to all RDBMS
– CREATE, DROP, ALTER for Tables.
– INSERT, UPDATE, DELETE for records.
– GRANT, REVOKE for permission.
– SELECT for query.
• SQL was initiated by IBM and is now controlled by ANSI (American
National Standard Institute). Also pronounced as SEQUEL.
• It is command language for communication with ORACLE/MS-SQL
Server.
• When an SQL statement is entered it is stored in the part of memory
called SQL Buffer and remains there until a new SQL statement is
entered.

An Overview of
SQL
• SQL stands for Structured Query Language.
• It is the most commonly used relational
database language today.
• SQL works with a variety of different fourth-
generation (4GL) programming languages, such
as Visual Basic.
SQL is used for:
• Data Manipulation
• Data Definition
• Data Administration
• All are expressed as an SQL statement or
command.
SQL
Requirements
• SQL Must be embedded in a programming
language, or used with a 4GL like VB
• SQL is a free form language so there is no limit
to the the number of words per line or fixed
line break.
• Syntax statements, words or phrases are always
in lower case; keywords are in uppercase.
Not all versions are case sensitive!
SQL is a Relational Database
• Represent all info in database as tables
• Keep logical representation of data independent from its physical storage
characteristics
• Use one high-level language for structuring, querying, and changing info
in the database
• Support the main relational operations
• Support alternate ways of looking at data in tables
• Provide a method for differentiating between unknown values and nulls
(zero or blank)
• Support Mechanisms for integrity, authorization, transactions, and
recovery
A Fully Relational Database Management System must:
Features of SQL
• It can be used by a range of users including those
with little or no programming experience.
• It is a non-procedural language.
• It reduces the amount of time required for creating
and maintaining system.
• It is English like language
Rules of SQL
• SQL statements starts with a verb (SQL action word) eg.
SELECT.
• Each verb is followed by number of clauses eg. FROM,
WHERE, HAVING.
• A space separates clause eg. DROP TABLE.
• Comma (,)separates parameters without clause.
• A semicolon (;) is used to end SQL statement.
• Characters and date literals must be enclosed in single
quotes.
• Numeric values can be represented by simple values 0.39,
-34, 01991. Scientific notations 2E5 = 2 * 10
5


Types of Tables
• User Tables: contain information that is
the database management system
• System Tables: contain the database
description, kept up to date by DBMS
itself

There are two types of tables which make up a relational database in SQL
Relation Table
Tuple Row
Attribute Column
Using SQL
SQL statements can be embedded into a program
(cgi or perl script, Visual Basic, MS Access)
OR
SQL statements can be entered directly at the
command prompt of the SQL software being
used (such as mySQL)
SQL Delimiters
Addition + Exponential **
Subtraction – Concatenation ||
Multiplication * Comment -- , /*…*/
Division /
Relational =,>,<
Expression or List ()
Terminator ;
Item Separator ,
Character String delimiter ‘
Quote Identifier “
Components of SQL
• DDL (Data Definition Language)
– CREATE, ALTER, DROP, TRUNCATE, COMMENT.
• DML (Data Manipulation Language)
– INSERT, UPDATE, DELETE, CALL, LOCK TABLE
• DCL (Data Control Language)
– COMMIT- Save the work.
– SAVE POINT – Identify a point in a transaction to which you can
later on rollback.
– ROLLBACK – restore database to the original since the last
COMMIT.
– SET TRANSACTION – Change transaction option like what rollback
segment to use.
– GRANT/ REVOKE – Grant or take back permission to or from the
users.

• DQL (Data Query Language)
– SELECT – retrieve data from the database
Data types
• CHAR (size) – store characters maximum upto 255.
• VARCHAR (size) / VARCHAR2(size) – store alphanumeric
data maximum upto 2000 / 4000.
• DATE – To represent Date and Time DD-MM-YY.
• NUMBER(P,S) – Precision (P) maximum length of data ,
Scale (S) determines number of places to the right of
decimal.
• LONG – Variable length character strings containing upto
2 GB.
• CLOB/ BLOB – Character Large Objects/ Binary Large
objects to store images, pictures, sounds, video, txt files
etc.
Sub Languages of SQL – DDL
(Data Definition Language)
Command

Description

CREATE

Creates a new table, a view of a
table, or other object in
database
ALTER

Modifies an existing database
object, such as a table.
DROP

Deletes an entire table, a view
of a table or other object in the
database.
Sub Languages of SQL – DML
(Data Manipulation Language)
Command

Description

INSERT

Creates a record
UPDATE Modifies records

DELETE

Deletes records
Sub Languages of SQL – DCL
(Data Control Language)
Command

Description

GRANT

Gives a privilege to user
REVOKE

Takes back privileges
granted from user
Sub Languages of SQL – DQL
(Data Query Language)
Command

Description

SELECT

Retrieves certain
records from one or
more tables
Domain Types
• char(n): fixed length char string
• varchar(n): variable length char string
• int or integer
• smallint
• numeric(p,d): fixed-point number of given precision
• real, double precision
• float(n): floats with a given precision
• date: containing year,month, date
• time: in hours, minutes and seconds
• Null value is part of each domain
• Define new domains:
– create domain person-name char(20)2
Schema Definition
Create table r (
A1 D1 [not null] [default V1]
A2 D2 [not null] [default V2]

An Dn [not null] [default Vn]
<integrity constraint 1>
<integrity constraint 2>

<integrity constraint k>
)
Integrity constraints 1 … k could be:
– primary key
– candidate key
– foreign key
– check(predicate) specifies predicate that must be satisfied by
each tuple
SQL as DDL
create table Employee
(
name char[15] not null
age smallint
sex (M,F) default M
ss# integer not null
spouse_ss# integer
dept# integer
)

Primary Key (ssno)
Unique(spouse_ss#)
check (age >=20 and age <=70)
Foreign key (dept#) references Dept(dept#)
on delete cascade
on update cascade
Don’t allow null values
Default value is Male
if dept# updated/deleted in Dept
table, cascade update/delete to
employee-
if null/default instead of cascade,
null/default value taken by dangling
tuples in Employee
SQL as DDL
• Attributes in primary key required to be not null
• Attributes in candidate key can take null values unless specified
otherwise
• Unique constraint violated if two tuples have exactly same values for
attribues in unique clause provided values are not null
• Null also permitted for attributes in foreign key.
• Foreign key constraint automatically satisfied if even one attribute in
foreign key is null.
• Predicate in check clause can be very complex and can include SQL
queries inside them
• Other DDL Statements:
– drop table r
– alter table r add A D
– alter table r drop A
Design
• SQL represents all information in the form
of tables
• Supports three relational operations:
selection, projection, and join. These are
for specifying exactly what data you want
to display or use
• SQL is used for data manipulation,
definition and administration
S
Q
L

Rows
describe the
Occurrence of
an Entity
Table Design
Name Address
Jane Doe 123 Main Street
John Smith 456 Second Street
Mary Poe 789 Third Ave
Columns describe one
characteristic of the entity
Basic DDL Commands in SQL
• CREATE: to define new tables (to define
relation schemas)
• DROP: to delete table definitions (to delete
relation schemas)
• ALTER: to change the definitions of existing
tables (to change relation schema)
• Other features as DDL
– Specify referential integrity constraints (FKs)
– Specify user-defined attributes constraints
Data Definition Language
• The schema for each relation, including
attribute types.
• Integrity constraints
• Authorization information for each
relation.
• Non-standard SQL extensions also allow
specification of
– The set of indices to be maintained for each
relations.
– The physical storage structure of each relation
on disk.
Allows the specification of:
SQL Commands Are Sequential
• Commands are executed in the order they are
encountered.
• DDL commands are not like C/Java
declarations.
• DDL and DML commands can be mixed
– For example, you can define a table, fill it up with
contents, and delete a columns.
– That is, table definitions (relation schema) can be
changed during the lifespan of a database.
• The ability of doing so does imply it is a good practice.
• It is best the schema/design of a database is well
thought through before its use.
MySQL
• Open source and free
• Generally not as powerful as Oracle
– Our projects will not need advanced and/or
proprietary features of Oracle.
• Still, it is an industrial strength package.
– Users include Amazon, NASA, Google, Yahoo …
• A commercial edition is also available (MySQL
Enterprise) --- You are paying for the services.
Comparison Operators
Defining Search Expressions
• Character strings
– Must be enclosed in single quotes
– Case sensitive
• Dates
– Use to_date function with date string and format model
• Intervals
– Use to_yminterval and to_dsinterval with interval string format model
Slide 35
The
COMPANY
Database
Using SQL
To begin, you must first CREATE a database using
the following SQL statement:
CREATE DATABASE database_name
Depending on the version of SQL being used
the following statement is needed to begin
using the database:
USE database_name
Using SQL
• To create a table in the current database,
use the CREATE TABLE keyword
CREATE TABLE authors
(auth_id int(9) not null,
auth_name char(40) not null)
auth_id auth_name
(9 digit int) (40 char string)
CREATE
• Syntax – CREATE TABLE <Table name>
(<columnName1> <DataType>(<size>),
(<columnName2> <DataType>(<size>));
• Example:
create table bank_sys ( branch_no
varchar2(10), name varchar2(20));

Output- Table Created

Create the COMPANY Database
• To create
create datatbase COMPANY;
• To use (or switch to) the database
use COMPANY;

• Subsequent commands will operate on the
COMPANY database by default.

Create Table Construct
• An SQL relation is defined using the create table
command:
create table r (A
1
D
1
, A
2
D
2
, ..., A
n
D
n
,
(integrity-constraint
1
),
...,
(integrity-constraint
k
))
– r is the name of the relation
– each A
i
is an attribute name in the schema of relation r
– D
i
is the data type of attribute A
i

• Example:
create table branch
(branch_name char(15),
branch_city char(30),
assets integer)
Domain Types in SQL
• char(n). Fixed length character string, with user-specified
length n.
• varchar(n). Variable length character strings, with user-
specified maximum length n.
• int. Integer (a finite subset of the integers that is machine-
dependent).
• smallint. Small integer (a machine-dependent subset of the
integer domain type).
• numeric(p,d). Fixed point number, with user-specified
precision of p digits, with n digits to the right of decimal point.
• real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision.
• float(n). Floating point number, with user-specified precision
of at least n digits.


Integrity Constraints on Tables
• not null
• primary key (A
1
, ..., A
n
)
Example: Declare branch_name as the primary key for branch
.
create table branch
(branch_name char(15),
branch_city char(30) not null,
assets integer,
primary key (branch_name))
primary key declaration on an attribute automatically ensures
not null in SQL-92 onwards, needs to be explicitly stated in
SQL-89
CREATE TABLE
CREATE TABLE DEPARTMENT (
Dname VARCHAR(10) NOT NULL,
Dnumber INTEGER Default 0,
Mgr_ssn CHAR(9),
Mgr_Startdate CHAR(9),
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn)
REFERENCES EMPLOYEE (Ssn));

• The “UNIQUE” clause specifies secondary keys.
• EMPLOYE)has to be created first for the FK Mgr_ssn
to refer to it.
• How could we have defined the Dno FK in EMPLOYEE?
Additional Data Types
• DATE:
– Made up of year-month-day in the format yyyy-mm-dd
• TIME:
– Made up of hour:minute:second in the format hh:mm:ss
• TIMESTAMP:
– Has both DATE and TIME components
• Decimal (i,j):
– i: total number of digits
– j: the number of digits after the decimal point

• Others: Boolean, Float, Double Precision
Adding the Dno FK(Foreign Key)
to EMPLOYEE
• If “create table EMPLOYEE” is issued first, we
cannot specify Dno as a FK in that create command.
• An ALTER command must be used to change the schema
of EMPLOYEE, after the “create table
DEPARTMENT,” to add a FK.

alter table EMPLOYEE
add constraint
foreign key (Dno)
references DEPARTMENT (Dnumber);
The Check Clause
• Used to specify user-defined constraints
• Assume that dept. numbers are from 0 to 99.
create table DEPARTMENT (

Dnumber INTEGER Default 0
check (Dnumber>=0 AND Dumber<=99),
…);
• “Check” can also be a clause of the entire table.
create table DEPARTMENT (

Dept_create_date date,
Mgr_start_date date,
check (Dept_create_date <= Mgr_start_date)
);
Using SQL
ALTER TABLE authors
ADD birth_date datetime null
auth_id auth_name auth_city auth_state
123456789 Jane Doe Dearborn MI
000000001 John Smith Taylor MI
To change a table in a database use ALTER TABLE. ADD adds a
characteristic.
ADD puts a new column in the table called birth_date
birth_date
.
.
Type Initializer
Using SQL
ALTER TABLE authors
DROP birth_date
auth_id auth_name auth_city auth_state
123456789 Jane Doe Dearborn MI
000000001 John Smith Taylor MI
To delete a column or row, use the keyword DROP
DROP removed the birth_date characteristic from the table
auth_state
.
.
ALTER Table
• Syntax – ALTER Table <Tablename> ADD
(<newcolumnname> <Datatype> (<size>),
<newcolumn2> <Datatype2>(size));
• Example:
ALTER TABLE Bank_sys add (address
varchar(40));
• Syntax ALTER TABLE <Tablename> DROP COLUMN
<Columnname>;
• Example:
ALTER TABLE Bank_sys DROP COLUMN name;

• Syntax – ALTER TABLE <Tablename>
MODIFY ( <Columnname> <Newdatatype>
(<size>));
• Example:
ALTER TABLE Bank_sys Modify
(Branch_no varchar(15));

Add Columns to Existing
Tables
• To add spouse SSN (S_ssn) to EMPLOYEE
alter table EMPLOYEE add column S_ssn
char(9);
– The new attribute will have NULLs in all the tuples of the
relation right after the command is executed

• Alternatively, we can set a default value.
alter table EMPLOYEE add column S_ssn char(9)
default “000000000”;
Referential Integrity Options
• Causes of referential integrity violation for a foreign
key FK (consider the Mgr_ssn of DEPARTMENT).
– On Delete: when deleting the foreign tuple
• What to do when deleting the manager tuple in EMPLOYEE ?
– On Update: when updating the foreign tuple
• What to do when updating/changing the SSN of the manager tuple
in EMPLOYEE is changed ?
• Actions when the above two causes occur.
– Set Null: the Mgr_ssn is set to null.
– Set Default: the Mgr_ssn is set to the default value.
– Cascade: the Mgr_ssn is updated accordingly
• If the manager is deleted, the department is also deleted.
The Mgr_ssn Example
CREATE TABLE DEPARTMENT (

Mgr_ssn CHAR(9),

FOREIGN KEY (Mgr_ssn)
REFERENCES EMPLOYEE (Ssn)
ON DELETE ???
ON UPDATE ???
);

Another Example
Create table EMP(

ESSN CHAR(9),
DNO INTEGER DEFAULT 1,
SUPERSSN CHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT
ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES
EMP ON DELETE SET NULL
ON UPDATE CASCADE);
Miscellaneous Commands
• show databases;
– Show all the databases on the server
• show tables;
– Show all the tables of the present database
• show columns from table EMPLOYEE;
• drop table t_name;
– Delete the entire table t_name
• drop database db_name;
– Delete the entire database db_name
• load data infile f_name into table t_name;
– To be discussed with the next homework.
More on SQL

Constraints
Triggers
Integrity Constraints (Review)
• Constraint describes conditions that every legal instance of a
relation must satisfy.
– Inserts/deletes/updates that violate ICs are disallowed.
– Can be used to :
• ensure application semantics (e.g., sid is a key), or
• prevent inconsistencies (e.g., sname has to be a string, age must be < 200)

• Types of IC’s:
– Fundamental: Domain constraints, primary key constraints, foreign key
constraints
– General constraints : Check Constraints, Table Constraints and Assertions.
Check or Table Constraints

• Can use queries to express constraint.

CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY (sid),
CHECK ( rating >= 1
AND rating <= 10 ))

Explicit Domain Constraints
CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating values-of-ratings,
age REAL,
PRIMARY KEY (sid))
CREATE DOMAIN values-of-ratings INTEGER
DEFAULT 1
CHECK ( VALUE >= 1 AND VALUE <= 10)

More Powerful Table Constraints


CREATE TABLE Reserves
( sname CHAR(10),
bid INTEGER,
day DATE,
PRIMARY KEY (bid,day),
CONSTRAINT noInterlakeRes
CHECK (`Interlake’ <>
( SELECT B.bname
FROM Boats B
WHERE B.bid= bid)))
 Constraint that Interlake boats cannot be reserved:
 If condition evaluates to FALSE, update is rejected.

Table Constraints


 Associated with one table
 Only needs to hold TRUE when table is non-empty.
Table Constraints with Complex
CHECK
CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY (sid),
CHECK
( (SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B)
< 100 )

• Symmetric constraint, yet associated with Sailors.
• If Sailors is empty, the number of Boats tuples can be anything!
Number of boats plus number of sailors is < 100
Assertions
( Constraints over Multiple Relations)
CREATE TABLE Sailors
( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY (sid),
CHECK
( (SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )


• ASSERTION
not
associated
with either
table.
CREATE ASSERTION smallClub
CHECK
( (SELECT COUNT (S.sid) FROM Sailors S)
+ (SELECT COUNT (B.bid) FROM Boats B) < 100 )

Number of boats
plus number of
sailors is < 100
Using SQL
DROP DATABASE authors
auth_id auth_name auth_city auth_state
123456789 Jane Doe Dearborn MI
000000001 John Smith Taylor MI
The DROP statement is also used to delete an entire database.
DROP removed the database and returned the memory to system
Summary
• SQL allows specification of rich
integrity constraints and their
efficient maintenance


Drop and Alter Table
Constructs
• The drop table command deletes all information about the dropped relation
from the database.
• The alter table command is used to add attributes to an existing relation:
alter table r add A D
where A is the name of the attribute to be added to relation r and D is the
domain of A.
– All tuples in the relation are assigned null as the value for the new attribute.
• The alter table command can also be used to drop attributes of a relation:
alter table r drop A
where A is the name of an attribute of relation r
– Dropping of attributes not supported by many databases
Conclusion
• SQL is a versatile language that can
integrate with numerous 4GL languages
and applications
• SQL simplifies data manipulation by
reducing the amount of code required.
• More reliable than creating a database
using files with linked-list implementation