Upload
phungnga
View
220
Download
2
Embed Size (px)
Citation preview
CS 377
Database Systems
1
Relational Calculus and SQL
Li Xiong
Department of Mathematics and Computer Science
Emory University
Relational Calculus
� A relational calculus is a declarative language for specifying database queries� tuple relational calculus
� domain relational calculus
� Relational calculus vs. relational algebra� Relational calculus is nonprocedural. A calculus
3
� Relational calculus is nonprocedural. A calculus expression specifies what is to be retrieved rather than how to retrieve it
� Relational algebra is procedural. A relational algebra expression contains a sequence of operations to specify a retrieval request.
� Relational calculus and relational algebra are logically
equivalent
Tuple Relational Calculus� A simple tuple relational calculus query is of the form
{t | COND(t)}
where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t)
� Example:
To find the first and last names of all employees whose salary is above $50,000
{t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000}
4
{t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000}
� General expression
� Range relation
� Attributes
� Selection/join conditions
The Existential and Universal Quantifiers
� Two special quantifiers can appear in formulas
� universal quantifier (∀∀∀∀)
� existential quantifier (∃∃∃∃).
� Informally, a tuple variable t is bound if it is quantified, meaning that it
appears in an (∀∀∀∀ t) or (∃∃∃∃ t) clause; otherwise, it is free.
� The only free tuple variables in a tuple relational calculus expression should be those that appear to the left of |
5
be those that appear to the left of |
� (∃∃∃∃ t)(F) is true if F evaluates to true for some (at least one) tuple t; otherwise false.
� (∀∀∀∀ t)(F) is true if F evaluates to true for every tuple (in the universe) t; otherwise false.
Sample Queries in Tuple
Relational Calculus(For all projects, they are either not controlled by department 5 or e is working on)
Q3: {e.lname, e.fname | Employee(e)
AND ((∀∀∀∀x) (NOT (Project(x))
OR NOT (x.dnum = 5)
OR ((∃∃∃∃w) (Works_on(w)
AND w.essn =e.ssn
AND x.pnumber = w.pno))))}
7
AND x.pnumber = w.pno))))}
(There is no project controlled by dept 5 that e is not working on)
Q3A: {e.lame, e.fname | Employee(e)
AND (NOT (∃∃∃∃x) (Project (x) AND (x.dnum = 5)
AND (NOT (∃∃∃∃w) (Works_on(w)
AND w.essn = e.ssn
AND x.pnumber = w.pno))))}
Requirement analysis
Requirement specification
Conceptual design
Conceptual data model (ER Model)
Logical design
9
Logical design
Representational data model (Relational Model)
Physical design
Physical data model
Database design implementation Data definition/manipulation (SQL)
SQL Introduction� SQL (Structured Query Language) – create,
retrieve, update and delete data from relational
database systems
� A data manipulation language (DML) and a data
definition language (DDL)
10
definition language (DDL)
Database products containing letters of SQL:
MySQL, PostreSQL, Microsoft SQL Server, SQLite
SQL vs. Relational Model
� Important distinction between SQL and the formal relational
model
� An SQL relation (table) is a multi-set (bag) of tuples; it is not a set
of tuples
� SQL relations can be constrained to be sets by specifying PRIMARY
11
SQL relations can be constrained to be sets by specifying PRIMARY KEY or UNIQUE attributes, or by using the DISTINCT option in a query
SQL and Relational Calculus
� The language SQL is inspired by tuple relational
calculus.
� SQL is a declarative language to a great extent
� Basic SQL block structure
12
SELECT <list of attributes>
FROM <list of relations>
WHERE <conditions>
� General relational calculus expression
� Range relation
� Attributes
� conditions
Query plan in relational algebra� A query plan is a relational algebra expression represented as a query tree
14
Using SQL
� Stand-alone (CLI or GUI): SQL*Plus (see tutorial)
� Embedded in a host language (C, C++, Java, etc.)
15
Data Definition
� Define a database schema
� Create new relations (tables) in a schema
� Alter the structure of existing relations
� Delete relations
17
� Delete relations
CREATE SCHEMA
� Define a database schema, which is used to group
database tables and other constructs that belong to
the same database application
� Syntax
18
� Syntax
CREATE SCHEMA schema_name AUTHORIZATION
db_user;
� Typically executed by DBA who will grant
authorities to some database user who then owns
the database schema
CREATE TABLE� Specifies a new base relation by giving it a name, and specifying each of
its attributes and their data types
� Syntax:
CREATE TABLE relation_name
( attr_name1 type1 [attr_constraint1] ,
....
attr_namen typen [attr_constraintn]
19
attr_namen typen [attr_constraintn]
[ , integrity constrains ]
);
� Example:CREATE TABLE DEPARTMENT( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,MGRSSN CHAR(9),MGRSTARTDATE CHAR(9) );
� Create a new table from existing table
CREATE TABLE AS QUERY (later)
Data Types� Numeric Types:
� INTEGER or INT - integer
� SMALLINT - short integer
� DECIMAL(i,j) or DEC(i,j) or NUMERIC(i,j) - fixed point numbers with i decimal digits and j digits after the decimal point. E.g.: DEC(8,3): xxxxx.yyy
� FLOAT or REAL - single precision floating point numbers (with roundoff errors)
� DOUBLE PRECISION - double precision floating point numbers (still with roundoff errors)
� Character string� CHARACTER(n) or CHAR(n) - fixed length character strings
� VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n) - To denote string: 'John'
20
VARCHAR(n) or CHAR VARYING(n) or CHARACTER VARYING(n) - To denote string: 'John'
� Bit string� BIT(n) - fixed length bit string
� BIT VARYING(n) - variable length bit string - To denote bit string: B'10101'
� BOOLEAN - boolean data NOTE: Due to the NULL value, SQL uses a three value logic
� DATE - Calendar date NOTE: DATE values are specified as: DATE'YYYY-MM-DD‘, must be preceded by the keyword DATE
� TIME - Time of day NOTE: TIME values are specified as: TIME'HH:MM:SS‘, Must be preceded by the keyword TIME
� TIMESTAMP - DATA + TIME NOTE: TIMESTAMP values are specified as: TIMESTAMP'YYYY-MM-DD HH:MM:SS'
Specifying Constraints
� Attribute constraints� not null
� attribute domain
� default values
� Key attributes
21
Key attributes
� Referential integrity constraints (foreign keys)
Attribute Constraints
� NOT NULL: attribute cannot be assigned a NULL value
� Example: CREATE TABLE test
( ssn CHAR(9) NOT NULL,
fname CHAR(30), .... );
Insert into test(fname, lname) values (‘John’, ‘Smith’); ?
� DEFAULT: specify a default value of an attribute
22
� DEFAULT: specify a default value of an attribute
� Example: CREATE TABLE test
( ssn CHAR(9) NOT NULL,
salary DECIMAL(6,2) DEFAULT 50000, .... );
insert into test(ssn) values ('111223333'); ?
� If no DEFAULT value is specified, the default value is NULL
� CHECK: check if the value of an attribute is within a specific range
� Example: CREATE TABLE test
( ssn CHAR(9) NOT NULL,
dno INTEGER CHECK (dno > 0 AND dno < 21));
Key Constraints
� The Primary Key attribute can be specified by primary
key constraint.
� The UNIQUE constraint can be used to specify candidate
keys
23
CREATE TABLE DEPT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP );
Referential Constraints
� The Foreign Key attribute is used to reference (i.e., identify) tuples in anotherrelation and as such, the referenced tuples must exist to maintain integrity
� Each key constraint may be (and better be) identified by a constraint name
� Example: CREATE TABLE employee
( ssn CHAR(9) NOT NULL,
....
CONSTRAINT Test1PrimKey PRIMARY KEY(ssn)
24
CONSTRAINT Test1PrimKey PRIMARY KEY(ssn)
);
CREATE TABLE dependent
( essn CHAR(9) NOT NULL,
...
CONSTRAINT Test2ForeignKey FOREIGN KEY (essn)
REFRENCES employee(ssn)
);
� insert into dependent values ('111223333’); ?
Chicken and Egg Problem?
26
� Cannot define a referential integrity constraint when
the referenced attribute does not exist
� Solution: using the ALTER command to add the
referential constraints relations are created
Alter Table
� Adding attributes
� Removing attributes
� Adding constraints
� Removing constraints
27
� Removing constraints
� Rename/update attributes? - No
ALTER TABLE – Add Attributes
� Used to add an attribute to one of the base relations
� The new attribute will have NULLs in all the tuples of the relation right
after the command is executed; hence, the NOT NULL constraint is not
allowed for such an attribute
� Example:
28
ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);
� The database users must still enter a value for the new attribute JOB for
each EMPLOYEE tuple. This can be done using the UPDATE
command.
ALTER TABLE – Remove Attributes
� ALTER TABLE table_name DROP [COLUMN] attr_name{RESTRICTED|CASCADE};
� RESTRICTED: only the attribute table_name.attr_name is dropped -however, if the attribute table_name.attr_name is a part of a foreign key in some other relation, it cannot be dropped.
� E.g. You cannot drop employee.ssn because it is used as a foreign key in dependent department
29
E.g. You cannot drop employee.ssn because it is used as a foreign key in dependent, department, etc
� CASCADE: the attribute table_name.attr_name is dropped and if the attribute table_name.attr_name is a part of a foreign key in some other relation, that attribute will also be dropped, and so on
ALTER Table – Add/Remove
Constraints� Adding a new constraint
ALTER TABLE table_name ADD CONSTRAINT constraint_name
constraint_def;
� Example: ALTER TABLE employee ADD CONSTRAINT
EmpPrimKey ssn PRIMARY KEY(ssn);
30
� NOTE: If there are some tuples in the relation, and the constraint is
violated by some tuples, the new constraint is NOT recorded.
� Removing an existing constraint
ALTER TABLE table_name DROP CONSTRAINT constraint_name ;
� Example: ALTER TABLE employee DROP CONSTRAINT
EmpPrimKey;
� NOTE: you can only drop a constraint if you have given it a name at
the time of definition.
Solve the Chicken and Egg ProblemCREATE TABLE employee
( ssn CHAR(9),
....
CONSTRAINT EmpPrimKey
PRIMARY KEY (ssn),
);
CREATE TABLE department
( dnumber INT,
....
31
....
mgrssn CHAR(9),
CONSTRAINT DepPrimKey
PRIMARY KEY (dnumber),
CONSTRAINT MgrForeignKey
FOREIGN KEY (mgrssn)
REFRENCES employee(ssn)
);
ALTER TABLE employee ADD
CONSTRAINT DepForeignKey
FOREIGN KEY dno
REFERENCES department(dnumber);
How can we insert any new tuple
into the relations without violating
the referential constraints?
Chicken and Egg Problem Round 2?
� Solution:
ALTER TABLE emp
DROP CONSTRAINT DepForeignKey;
ALTER TABLE emp
32
ALTER TABLE emp
ADD CONSTRAINT DepForeignKey
FOREIGN KEY (dno)
REFERENCES dept(dnumber)
INITIALLY DEFERRED DEFERRABLE;
INSERT INTO emp VALUES ('444444444', 12);
INSERT INTO dept VALUES (12, '444444444');
COMMIT;
DROP TABLE
� Used to remove a relation (base table) and its
definition
� The relation can no longer be used in queries,
updates, or any other commands since its
33
updates, or any other commands since its
description no longer exists
� DROP TABLE table_name;
� DROP TABLE table_name cascade constraints;
Creating Company Database and
Loading Data� Create tables using SQL
� Load data using SQL Loader (see tutorial)
34