CS 377 Database Systems
Relational Calculus and SQL
Li Xiong Department of Mathematics and Computer Science Emory University
1 Outline Relational Algebra Relational Calculus Tuple Relational Calculus SQL
2 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 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
3 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}
General expression
Range relation Attributes Selection/join conditions
4 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 |
(∃∃∃ 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.
5 Sample Queries in Tuple Relational Calculus
6 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))))}
(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))))}
7 Outline Relational Algebra Relational Calculus Tuple Relational Calculus SQL
8 Requirement analysis
Requirement specification
Conceptual design
Conceptual data model ( ER Model )
Logical design
Representational data model ( Relational Model )
Physical design
Physical data model
Database design implementation Data definition/manipulation (SQL)
9 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 )
Database products containing letters of SQL: MySQL, PostreSQL, Microsoft SQL Server, SQLite
10 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 KEY or UNIQUE attributes, or by using the DISTINCT option in a query
11 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 SELECT FROM
WHERE
Range relation Attributes conditions 12 SQL and relational algebra Execution of a SQL query is based on relational
13 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 SQL Outline
Data definition Query Data update View definition
16 Data Definition
Define a database schema Create new relations (tables) in a schema Alter the structure of existing relations Delete relations
17 CREATE SCHEMA
Define a database schema, which is used to group database tables and other constructs that belong to the same database application 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
18 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 ] [ , 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)
19 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' 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'
20 Specifying Constraints
Attribute constraints not null attribute domain default values Key attributes Referential integrity constraints (foreign keys)
21 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 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));
22 Key Constraints
The Primary Key attribute can be specified by primary key constraint. The UNIQUE constraint can be used to specify candidate keys
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 );
23 Referential Constraints
The Foreign Key attribute is used to reference (i.e., identify) tuples in another relation 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 ) ); CREATE TABLE dependent ( essn CHAR(9) NOT NULL, ... CONSTRAINT Test2ForeignKey FOREIGN KEY (essn) REFRENCES employee(ssn) ); insert into dependent values ('111223333’); ?
24 25 Chicken and Egg Problem?
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 26 Alter Table
Adding attributes Removing attributes Adding constraints Removing constraints
Rename/update attributes? - No
27 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:
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.
28 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 , 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
29 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); 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.
30 Solve the Chicken and Egg Problem
CREATE TABLE employee ( ssn CHAR(9), .... CONSTRAINT EmpPrimKey PRIMARY KEY (ssn), ); CREATE TABLE department ( dnumber INT, .... mgrssn CHAR(9), CONSTRAINT DepPrimKey PRIMARY KEY (dnumber), CONSTRAINT MgrForeignKey FOREIGN KEY (mgrssn) REFRENCES employee(ssn) ); ALTER TABLE employee ADD How can we insert any new tuple CONSTRAINT DepForeignKey FOREIGN KEY dno into the relations without violating REFERENCES department(dnumber); the referential constraints?
31 Chicken and Egg Problem Round 2?
Solution:
ALTER TABLE emp DROP CONSTRAINT DepForeignKey; 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;
32 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 description no longer exists DROP TABLE table_name; DROP TABLE table_name cascade constraints;
33 Creating Company Database and Loading Data Create tables using SQL Load data using SQL Loader (see tutorial)
34 Outline
Data definition Query Data update View definition
35