Académique Documents
Professionnel Documents
Culture Documents
domain constraints
entity integrity referential integrity enterprise constraints
Views
2010-01-29
CUSTOMERS (cid, name, city, discount, contact) PRODUCTS (pid, name, city, quantity, price) ORDERS (ordno, month, cid, pid, qty)
2010-01-29
2010-01-29
CREATE TABLE
Basic syntax conforms to the ISO standard in all DBMS; subclauses defining integrity constraints may vary Example: To create the customers table, specify the primary key , and apply not null constraints
CREATE TABLE customers ( cid CHAR(4) NOT NULL, name VARCHAR (20) NOT NULL, city VARCHAR (10), discount REAL, contact VARCHAR(24),
PRIMARY KEY (cid) );
2010-01-29 CO532 Database Systems 5
Integrity constraints
Rules formulated by the database designer May be specified as
Column constraints, as part of column specification Table constraints, after all columns specified Separate constraint statements
Can cover:
mandatory data column value constraints entity integrity referential integrity
enterprise constraints
Some constraints may have to be specified using database procedures and rules
2010-01-29 CO532 Database Systems 6
Constraints
Mandatory data
Specified using the NOT NULL clause in a column specification Primary keys are, by definition, NOT NULL
CHECK constraint
used to constrain the domain (data type) values of columns
2010-01-29
Constraints
DEFAULT constraint
Specified using the DEFAULT clause in a column specification Example: To create a default value on customer discount discount REAL DEFAULT 0.0
UNIQUE constraint
used to ensure unique value in each row for non-primary keys
Entity Integrity
Primary key: a unique identifier for each row Entity integrity rule no primary key (or part thereof) should have a null value
CREATE TABLE products ( pid CHAR(3) name VARCHAR(10) city VARCHAR(10) quantity INTEGER, price REAL, PRIMARY KEY (pid) );
PRIMARY KEY (ordno), FOREIGN KEY (cid) REFERENCES customers (cid) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY (pid) REFERENCES products (pid) );
By specifying NOT NULL for the foreign keys, we enforce a mandatory relationship. CASCADE
Whenever rows in the master (referenced) table are deleted (resp. updated), the respective rowsDatabase Systems a 2010-01-29 CO532 of the child (referencing) table with 11 matching foreign key column will get deleted (resp. updated) as well.
Enterprise Constraints
these are application dependent constraints that can be implemented in different ways:
using CHECK, DEFAULT, NOT NULL, UNIQUE clauses in the CREATE TABLE statement
2010-01-29
12
VIEWS (1)
tables created with CREATE TABLE statements are stored in the database and referred to as base tables
a view is a virtual table a view is a logical window on data from the base tables
the SQL statements required to generate a view are stored as a database object
no data is retrieved when a view is created, only when it is used in a query statement views reduce query complexity increase security by restricting access to parts of the database reflect changes to the base tables
2010-01-29
13
VIEWS (2)
To create a view of the agents in New York.
CREATE VIEW nycust AS SELECT * FROM customers WHERE city = New York;
When this view is used in a query, the system replaces references to the view by the definition of the view. SELECT * FROM nycust WHERE discount > 5.0;
2010-01-29
14
Summary, things to do
Lectures 2-4 covered the basics of SQL Read one of:
Connolly (4th edition):
chapter 5: SQL Data manipulation chapter 6: SQL SQL Data definition
2010-01-29
15