data de nition, modi cation, and constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · t....

90
Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints Data Definition, Modification, and Constraints T. M. Murali September 9, 2009 T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Upload: others

Post on 20-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Definition, Modification, and Constraints

T. M. Murali

September 9, 2009

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 2: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 3: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 4: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and

UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 5: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 6: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 7: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 8: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 9: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Data Types in SQL

I Character strings:I CHAR(n): fixed-length string of n characters, with padding, if necessary.I VARCHAR(n): string of length of upto n characters.

I Bit strings:I BIT(n): bit string of length n.I BIT VARYING(n): bit string of length upto n.

I BOOLEAN: possible values are TRUE, FALSE, and UNKNOWN (readChapter 6.1.6 and 6.1.7).

I integers: INTEGER (INT), SHORTINT.

I floats: FLOAT (or REAL), DOUBLE PRECISION.

I fixed point numbers: DECIMAL(n, d): a number with n digits, withthe decimal point d positions from the right.

I dates and times: DATE and TIME (read Chapter 6.1.5).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 10: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Creating and Deleting Tables

I A table is a relation that is physically stored in a database.

I A table is persistent; it exists indefinitely unless dropped or altered insome way.

I Creating a table: CREATE TABLE followed by the name of the relationand a parathentised list of attribute names and their types.

CREATE TABLE Students(PID VARCHAR(8), Name CHAR(20),Address VARCHAR(255));

I Deleting a table: DROP TABLE followed by the name of the table.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 11: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Creating and Deleting Tables

I A table is a relation that is physically stored in a database.

I A table is persistent; it exists indefinitely unless dropped or altered insome way.

I Creating a table: CREATE TABLE followed by the name of the relationand a parathentised list of attribute names and their types.

CREATE TABLE Students(PID VARCHAR(8), Name CHAR(20),Address VARCHAR(255));

I Deleting a table: DROP TABLE followed by the name of the table.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 12: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Creating and Deleting Tables

I A table is a relation that is physically stored in a database.

I A table is persistent; it exists indefinitely unless dropped or altered insome way.

I Creating a table: CREATE TABLE followed by the name of the relationand a parathentised list of attribute names and their types.

CREATE TABLE Students(PID VARCHAR(8), Name CHAR(20),Address VARCHAR(255));

I Deleting a table: DROP TABLE followed by the name of the table.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 13: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Table Schemas

I ALTER TABLE followed by the name of the relation followed byI ADD followed by a column name and its data type.

I Add date of birth to Students:ALTER TABLE Students ADD Dob DATE;

I DROP followed by a column name.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 14: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Table Schemas

I ALTER TABLE followed by the name of the relation followed byI ADD followed by a column name and its data type.I Add date of birth to Students:

ALTER TABLE Students ADD Dob DATE;I DROP followed by a column name.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 15: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Table Schemas

I ALTER TABLE followed by the name of the relation followed byI ADD followed by a column name and its data type.I Add date of birth to Students:

ALTER TABLE Students ADD Dob DATE;

I DROP followed by a column name.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 16: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Table Schemas

I ALTER TABLE followed by the name of the relation followed byI ADD followed by a column name and its data type.I Add date of birth to Students:

ALTER TABLE Students ADD Dob DATE;I DROP followed by a column name.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 17: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Null and Default Values

I SQL allows NULL for unknown attribute values. (Read Chapter 6.1.6,especially for how SQL treats comparisons using NULL).

I NULL not allowed in certain cases.

I We can specify a default value for an attribute using the DEFAULTkeyword.

ALTER TABLE Students ADD Gender char(1) DEFAULT ’?’;

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 18: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Null and Default Values

I SQL allows NULL for unknown attribute values. (Read Chapter 6.1.6,especially for how SQL treats comparisons using NULL).

I NULL not allowed in certain cases.

I We can specify a default value for an attribute using the DEFAULTkeyword.

ALTER TABLE Students ADD Gender char(1) DEFAULT ’?’;

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 19: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Inserting Data into a Table

I INSERT INTO R(A1,A2, . . .An) VALUES (v1, v2, . . . , vn).I (A1,A2, . . . ,An) can be a subset of R’s schema.I Remaining attributes get default values.I Can omit names of attributes if we provide values for all attributes and

list values in standard order.

I Insertion: Instead of VALUES, use a SELECT statement.I Insert into the Professors table all professors who are mentioned in

Teach but are not in Professors.

INSERT INTO Professors(PID)SELECT ProfessorPIDFROM TeachWHERE ProfessorPID NOT IN

(SELECT PIDFROM Professors);

I Subquery in WHERE clause is evaluated completely before any tuples areinserted.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 20: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Inserting Data into a Table

I INSERT INTO R(A1,A2, . . .An) VALUES (v1, v2, . . . , vn).I (A1,A2, . . . ,An) can be a subset of R’s schema.I Remaining attributes get default values.I Can omit names of attributes if we provide values for all attributes and

list values in standard order.

I Insertion: Instead of VALUES, use a SELECT statement.

I Insert into the Professors table all professors who are mentioned inTeach but are not in Professors.

INSERT INTO Professors(PID)SELECT ProfessorPIDFROM TeachWHERE ProfessorPID NOT IN

(SELECT PIDFROM Professors);

I Subquery in WHERE clause is evaluated completely before any tuples areinserted.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 21: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Inserting Data into a Table

I INSERT INTO R(A1,A2, . . .An) VALUES (v1, v2, . . . , vn).I (A1,A2, . . . ,An) can be a subset of R’s schema.I Remaining attributes get default values.I Can omit names of attributes if we provide values for all attributes and

list values in standard order.

I Insertion: Instead of VALUES, use a SELECT statement.I Insert into the Professors table all professors who are mentioned in

Teach but are not in Professors.

INSERT INTO Professors(PID)SELECT ProfessorPIDFROM TeachWHERE ProfessorPID NOT IN

(SELECT PIDFROM Professors);

I Subquery in WHERE clause is evaluated completely before any tuples areinserted.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 22: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Inserting Data into a Table

I INSERT INTO R(A1,A2, . . .An) VALUES (v1, v2, . . . , vn).I (A1,A2, . . . ,An) can be a subset of R’s schema.I Remaining attributes get default values.I Can omit names of attributes if we provide values for all attributes and

list values in standard order.

I Insertion: Instead of VALUES, use a SELECT statement.I Insert into the Professors table all professors who are mentioned in

Teach but are not in Professors.

INSERT INTO Professors(PID)SELECT ProfessorPIDFROM TeachWHERE ProfessorPID NOT IN

(SELECT PIDFROM Professors);

I Subquery in WHERE clause is evaluated completely before any tuples areinserted.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 23: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Deleting Data from a Table

I DELETE FROM R WHERE C .

I Every tuple satisfying the condition C is deleted from R.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 24: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Updating Data in a Table

I An update in SQL is a change to attributes in one or more of thetuples existing in the database.

I Example: change the name of a student so that every male studenthas ’Mr.’ added to the name and every female student has ’Ms.’added to the name.

I UPDATE StudentsSET Name = ’Ms. ’ || NameWHERE Gender = ’F’;

I UPDATE StudentsSET Name = ’Mr. ’ || NameWHERE Gender = ’M’;

I Each assignment is an attribute, the equal sign, and an expression.

I Can set multiple attributes in the SET clause, separated by commas.

I The WHERE clause can involve a subquery.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 25: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Updating Data in a Table

I An update in SQL is a change to attributes in one or more of thetuples existing in the database.

I Example: change the name of a student so that every male studenthas ’Mr.’ added to the name and every female student has ’Ms.’added to the name.

I UPDATE StudentsSET Name = ’Ms. ’ || NameWHERE Gender = ’F’;

I UPDATE StudentsSET Name = ’Mr. ’ || NameWHERE Gender = ’M’;

I Each assignment is an attribute, the equal sign, and an expression.

I Can set multiple attributes in the SET clause, separated by commas.

I The WHERE clause can involve a subquery.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 26: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Updating Data in a Table

I An update in SQL is a change to attributes in one or more of thetuples existing in the database.

I Example: change the name of a student so that every male studenthas ’Mr.’ added to the name and every female student has ’Ms.’added to the name.

I UPDATE StudentsSET Name = ’Ms. ’ || NameWHERE Gender = ’F’;

I UPDATE StudentsSET Name = ’Mr. ’ || NameWHERE Gender = ’M’;

I Each assignment is an attribute, the equal sign, and an expression.

I Can set multiple attributes in the SET clause, separated by commas.

I The WHERE clause can involve a subquery.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 27: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Bulk Loading Data

I Different RDBMs have different syntax.

I PostgreSQL: Use the \copy ’filename’ INTO TABLE tablename; atthe psql prompt.

I File format:I Tab-delimited with columns in the same order as the attributes.I Use \N to indicate null values.

I Do not make assumptions about how the RDBMS will behave!

I Check to make sure your data is not corrupted.

I Do not delete the original files that contain the raw data.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 28: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Bulk Loading Data

I Different RDBMs have different syntax.

I PostgreSQL: Use the \copy ’filename’ INTO TABLE tablename; atthe psql prompt.

I File format:I Tab-delimited with columns in the same order as the attributes.I Use \N to indicate null values.

I Do not make assumptions about how the RDBMS will behave!

I Check to make sure your data is not corrupted.

I Do not delete the original files that contain the raw data.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 29: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Saving Data

I Use the pg_dump programme:

pg_dump -t table database

I Use man pg_dump for more information.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 30: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Specific Guidelines for the Project

I Login to the machine postdb.cs.vt.edu and use the psqlcommand-line interface to access the database.

I We are using v8.2.9 of the PostgreSQL server and client:Documentation is athttp://www.postgresql.org/docs/8.2/static/index.html andathttp://www.postgresql.org/docs/8.2/static/app-psql.html

I Shahriar will create an account and a database for each student.I Shahriar will create a database for each project.

I The name of the database is the name of your project.I Only the members of each project will be able to access the database

for the project.I You can create as many tables within a database as you want.

I We will maintain a webpage on how you can access the database.

I We will provide help for PostgreSQL on Linux.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 31: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

General Guidelines for the Project

I The database schema is not something that should change often.

I Think long and hard about your schema.I DROP may be better than ALTER TABLE.

I Do not delete the files containing raw data.

I Read documentation for the RDBMS you are using.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 32: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

General Guidelines for the Project

I The database schema is not something that should change often.I Think long and hard about your schema.

I DROP may be better than ALTER TABLE.

I Do not delete the files containing raw data.

I Read documentation for the RDBMS you are using.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 33: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

General Guidelines for the Project

I The database schema is not something that should change often.I Think long and hard about your schema.I DROP may be better than ALTER TABLE.

I Do not delete the files containing raw data.

I Read documentation for the RDBMS you are using.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 34: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Maintaining Integrity of Data

I Data is dirty.

I How does an application ensure that a database modification doesnot corrupt the tables?

I Two approaches:I Application programmes check that database modifications are

consistent.I Use the features provided by SQL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 35: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Maintaining Integrity of Data

I Data is dirty.

I How does an application ensure that a database modification doesnot corrupt the tables?

I Two approaches:I Application programmes check that database modifications are

consistent.

I Use the features provided by SQL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 36: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Maintaining Integrity of Data

I Data is dirty.

I How does an application ensure that a database modification doesnot corrupt the tables?

I Two approaches:I Application programmes check that database modifications are

consistent.I Use the features provided by SQL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 37: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Integrity Checking in SQL

I PRIMARY KEY and UNIQUE constraints.

I FOREIGN KEY constraints.

I Constraints on attributes and tuples.

I Triggers (schema-level constraints).

I How do we express these constraints?

I How do we check these constraints?

I What do we do when a constraint is violated?

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 38: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Integrity Checking in SQL

I PRIMARY KEY and UNIQUE constraints.

I FOREIGN KEY constraints.

I Constraints on attributes and tuples.

I Triggers (schema-level constraints).

I How do we express these constraints?

I How do we check these constraints?

I What do we do when a constraint is violated?

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 39: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Keys in SQL

I A set of attributes S is a key for a relation R if every pair of tuples inevery instance of R must disagree on at least one attribute in S .

I Select one key to be the PRIMARY KEY; declare other keys usingUNIQUE.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 40: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Primary Keys in SQL

I Modify the schema of Students to declare PID to be the key.

CREATE TABLE Students(PID VARCHAR(8) PRIMARY KEY,Name CHAR(20), Address VARCHAR(255));

I What about Courses, which has two attributes in its key?

CREATE TABLE Courses(Number integer, DeptName:VARCHAR(8), CourseName VARCHAR(255), ClassroomVARCHAR(30), Enrollment integer, PRIMARY KEY (Number,DeptName));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 41: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Primary Keys in SQL

I Modify the schema of Students to declare PID to be the key.

CREATE TABLE Students(PID VARCHAR(8) PRIMARY KEY,Name CHAR(20), Address VARCHAR(255));

I What about Courses, which has two attributes in its key?

CREATE TABLE Courses(Number integer, DeptName:VARCHAR(8), CourseName VARCHAR(255), ClassroomVARCHAR(30), Enrollment integer, PRIMARY KEY (Number,DeptName));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 42: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Primary Keys in SQL

I Modify the schema of Students to declare PID to be the key.

CREATE TABLE Students(PID VARCHAR(8) PRIMARY KEY,Name CHAR(20), Address VARCHAR(255));

I What about Courses, which has two attributes in its key?

CREATE TABLE Courses(Number integer, DeptName:VARCHAR(8), CourseName VARCHAR(255), ClassroomVARCHAR(30), Enrollment integer, PRIMARY KEY (Number,DeptName));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 43: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Primary Keys in SQL

I Modify the schema of Students to declare PID to be the key.

CREATE TABLE Students(PID VARCHAR(8) PRIMARY KEY,Name CHAR(20), Address VARCHAR(255));

I What about Courses, which has two attributes in its key?

CREATE TABLE Courses(Number integer, DeptName:VARCHAR(8), CourseName VARCHAR(255), ClassroomVARCHAR(30), Enrollment integer

, PRIMARY KEY (Number,DeptName));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 44: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Primary Keys in SQL

I Modify the schema of Students to declare PID to be the key.

CREATE TABLE Students(PID VARCHAR(8) PRIMARY KEY,Name CHAR(20), Address VARCHAR(255));

I What about Courses, which has two attributes in its key?

CREATE TABLE Courses(Number integer, DeptName:VARCHAR(8), CourseName VARCHAR(255), ClassroomVARCHAR(30), Enrollment integer, PRIMARY KEY (Number,DeptName));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 45: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Effect of Declaring PRIMARY KEYs

1. Two tuples in a relation cannot agree on all the attributes in the key.DBMS will reject any action that inserts or updates a tuple inviolation of this rule.

2. A tuple cannot have a NULL value in a key attribute.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 46: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Other Keys in SQL

I If a relation has other keys, declare them using the UNIQUE keyword.

I Use UNIQUE in exactly the same places as PRIMARY KEY.

I There are two differences between PRIMARY KEY and UNIQUE:I A table may have only one PRIMARY KEY but more than one set of

attributes declared UNIQUE.I A tuple may have NULL values in UNIQUE attributes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 47: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Other Keys in SQL

I If a relation has other keys, declare them using the UNIQUE keyword.

I Use UNIQUE in exactly the same places as PRIMARY KEY.I There are two differences between PRIMARY KEY and UNIQUE:

I A table may have only one PRIMARY KEY but more than one set ofattributes declared UNIQUE.

I A tuple may have NULL values in UNIQUE attributes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 48: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Keys in Relational Algebra

I Two ways of expressing constraints in relational algebra:

1. If R is an expression in relational algebra, then R = ∅ is a constraintthat means “there are no tuples in the result of R”.

2. If R and S are expressions in relational algebra, then R ⊆ S is aconstraint that says that “Every tuple in the result of R must be in theresult of S”.

I Express the fact that PID is a key of Students in relational algebra:

σ(S1.PID=S2.PID)AND(S1.Name<>S2.Name)(ρS1(Students)× ρS2(Students)) = ∅

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 49: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Keys in Relational Algebra

I Two ways of expressing constraints in relational algebra:

1. If R is an expression in relational algebra, then R = ∅ is a constraintthat means “there are no tuples in the result of R”.

2. If R and S are expressions in relational algebra, then R ⊆ S is aconstraint that says that “Every tuple in the result of R must be in theresult of S”.

I Express the fact that PID is a key of Students in relational algebra:

σ(S1.PID=S2.PID)AND(S1.Name<>S2.Name)(ρS1(Students)× ρS2(Students)) = ∅

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 50: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Enforcing Key Constraints

I Upon which actions should an RDBMS enforce a key constraint?

Only tuple update and insertion.

I RDMBS searches the tuples in the table to find if any tuple existsthat agrees with the new tuple on all attributes in the primary key.

I To speed this process, an RDBMS automatically creates an efficientsearch index on the primary key.

I User can instruct the RDBMS to create an index on one or moreattributes (See Chapters 8.3 and 8.4).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 51: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Enforcing Key Constraints

I Upon which actions should an RDBMS enforce a key constraint?Only tuple update and insertion.

I RDMBS searches the tuples in the table to find if any tuple existsthat agrees with the new tuple on all attributes in the primary key.

I To speed this process, an RDBMS automatically creates an efficientsearch index on the primary key.

I User can instruct the RDBMS to create an index on one or moreattributes (See Chapters 8.3 and 8.4).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 52: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Enforcing Key Constraints

I Upon which actions should an RDBMS enforce a key constraint?Only tuple update and insertion.

I RDMBS searches the tuples in the table to find if any tuple existsthat agrees with the new tuple on all attributes in the primary key.

I To speed this process, an RDBMS automatically creates an efficientsearch index on the primary key.

I User can instruct the RDBMS to create an index on one or moreattributes (See Chapters 8.3 and 8.4).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 53: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints

I Referential integrity constraint: in the relation Teach (that “connects”Courses and Professors), if Teach relates a course to a professor,then an tuple corresponding to this professor must exist inProfessors.

I How do we express such constraints in Relational Algebra?

I Consider the Teach(ProfessorPID, Number, DeptName) relation.We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

RA πProfessorPID(Teach) ⊆ πPID(Professors).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 54: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints

I Referential integrity constraint: in the relation Teach (that “connects”Courses and Professors), if Teach relates a course to a professor,then an tuple corresponding to this professor must exist inProfessors.

I How do we express such constraints in Relational Algebra?

I Consider the Teach(ProfessorPID, Number, DeptName) relation.

We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

RA πProfessorPID(Teach) ⊆ πPID(Professors).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 55: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints

I Referential integrity constraint: in the relation Teach (that “connects”Courses and Professors), if Teach relates a course to a professor,then an tuple corresponding to this professor must exist inProfessors.

I How do we express such constraints in Relational Algebra?

I Consider the Teach(ProfessorPID, Number, DeptName) relation.We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

RA πProfessorPID(Teach) ⊆ πPID(Professors).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 56: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints

I Referential integrity constraint: in the relation Teach (that “connects”Courses and Professors), if Teach relates a course to a professor,then an tuple corresponding to this professor must exist inProfessors.

I How do we express such constraints in Relational Algebra?

I Consider the Teach(ProfessorPID, Number, DeptName) relation.We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

RA πProfessorPID(Teach) ⊆ πPID(Professors).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 57: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints

I Referential integrity constraint: in the relation Teach (that “connects”Courses and Professors), if Teach relates a course to a professor,then an tuple corresponding to this professor must exist inProfessors.

I How do we express such constraints in Relational Algebra?

I Consider the Teach(ProfessorPID, Number, DeptName) relation.We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

RA πProfessorPID(Teach) ⊆ πPID(Professors).

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 58: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints in SQL

I We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

I In Teach, declare ProfessorPID to be a foreign key.

I CREATE TABLE Teach(ProfessorPID VARCHAR(8) REFERENCESProfessor(PID), Name VARCHAR(30) ...);

I CREATE TABLE Teach(ProfessorPID VARCHAR(8), NameVARCHAR(30) ..., FOREIGN KEY ProfessorPID REFERENCESProfessor(PID));

I If the foreign key has multiple attributes, use the second type ofdeclaration.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 59: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints in SQL

I We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

I In Teach, declare ProfessorPID to be a foreign key.

I CREATE TABLE Teach(ProfessorPID VARCHAR(8) REFERENCESProfessor(PID), Name VARCHAR(30) ...);

I CREATE TABLE Teach(ProfessorPID VARCHAR(8), NameVARCHAR(30) ..., FOREIGN KEY ProfessorPID REFERENCESProfessor(PID));

I If the foreign key has multiple attributes, use the second type ofdeclaration.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 60: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints in SQL

I We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

I In Teach, declare ProfessorPID to be a foreign key.

I CREATE TABLE Teach(ProfessorPID VARCHAR(8) REFERENCESProfessor(PID), Name VARCHAR(30) ...);

I CREATE TABLE Teach(ProfessorPID VARCHAR(8), NameVARCHAR(30) ..., FOREIGN KEY ProfessorPID REFERENCESProfessor(PID));

I If the foreign key has multiple attributes, use the second type ofdeclaration.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 61: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints in SQL

I We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

I In Teach, declare ProfessorPID to be a foreign key.

I CREATE TABLE Teach(ProfessorPID VARCHAR(8) REFERENCESProfessor(PID), Name VARCHAR(30) ...);

I CREATE TABLE Teach(ProfessorPID VARCHAR(8), NameVARCHAR(30) ..., FOREIGN KEY ProfessorPID REFERENCESProfessor(PID));

I If the foreign key has multiple attributes, use the second type ofdeclaration.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 62: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Foreign Key Constraints in SQL

I We want to require that every non-NULL value of ProfessorPID inTeach must be a valid ProfessorPID in Professors.

I In Teach, declare ProfessorPID to be a foreign key.

I CREATE TABLE Teach(ProfessorPID VARCHAR(8) REFERENCESProfessor(PID), Name VARCHAR(30) ...);

I CREATE TABLE Teach(ProfessorPID VARCHAR(8), NameVARCHAR(30) ..., FOREIGN KEY ProfessorPID REFERENCESProfessor(PID));

I If the foreign key has multiple attributes, use the second type ofdeclaration.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 63: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Requirements for FOREIGN KEYs

I If a relation R declares that some of its attributes refer to foreign keysin another relation S , then these attributes must be declared UNIQUEor PRIMARY KEY in S .

I For every tuple in R, the values of the foreign key in that tuple mustappear as values of the referenced attributes in some tuple in S .

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 64: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Enforcing Referential Integrity

I Three policies for maintaining referential integrity.

I Default policy: reject violating modifications.

I Cascade policy: mimic changes to the referenced attributes at theforeign key.

I Set-NULL policy: set appropriate attributes to NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 65: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Enforcing Referential Integrity

I Three policies for maintaining referential integrity.

I Default policy: reject violating modifications.

I Cascade policy: mimic changes to the referenced attributes at theforeign key.

I Set-NULL policy: set appropriate attributes to NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 66: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Default Policy for Enforcing Referential Integrity

I Reject violating modifications. There are four cases.

1. Insert a new Teach tuple whose ProfessorPID is not NULL and isnot the PID of any tuple in Professors.

2. Update the ProfessorPID attribute in a tuple in Teach to a valuethat is not the PID value of any tuple in Professors.

3. Delete a tuple in Professors whose PID value is the ProfessorPIDvalue for one or more tuples in Teach.

4. Update the PID value of a tuple in Professors when the old PIDvalue is the value of ProfessorPID in one or more tuples of Teach.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 67: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Default Policy for Enforcing Referential Integrity

I Reject violating modifications. There are four cases.

1. Insert a new Teach tuple whose ProfessorPID is not NULL and isnot the PID of any tuple in Professors.

2. Update the ProfessorPID attribute in a tuple in Teach to a valuethat is not the PID value of any tuple in Professors.

3. Delete a tuple in Professors whose PID value is the ProfessorPIDvalue for one or more tuples in Teach.

4. Update the PID value of a tuple in Professors when the old PIDvalue is the value of ProfessorPID in one or more tuples of Teach.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 68: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Default Policy for Enforcing Referential Integrity

I Reject violating modifications. There are four cases.

1. Insert a new Teach tuple whose ProfessorPID is not NULL and isnot the PID of any tuple in Professors.

2. Update the ProfessorPID attribute in a tuple in Teach to a valuethat is not the PID value of any tuple in Professors.

3. Delete a tuple in Professors whose PID value is the ProfessorPIDvalue for one or more tuples in Teach.

4. Update the PID value of a tuple in Professors when the old PIDvalue is the value of ProfessorPID in one or more tuples of Teach.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 69: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Default Policy for Enforcing Referential Integrity

I Reject violating modifications. There are four cases.

1. Insert a new Teach tuple whose ProfessorPID is not NULL and isnot the PID of any tuple in Professors.

2. Update the ProfessorPID attribute in a tuple in Teach to a valuethat is not the PID value of any tuple in Professors.

3. Delete a tuple in Professors whose PID value is the ProfessorPIDvalue for one or more tuples in Teach.

4. Update the PID value of a tuple in Professors when the old PIDvalue is the value of ProfessorPID in one or more tuples of Teach.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 70: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Default Policy for Enforcing Referential Integrity

I Reject violating modifications. There are four cases.

1. Insert a new Teach tuple whose ProfessorPID is not NULL and isnot the PID of any tuple in Professors.

2. Update the ProfessorPID attribute in a tuple in Teach to a valuethat is not the PID value of any tuple in Professors.

3. Delete a tuple in Professors whose PID value is the ProfessorPIDvalue for one or more tuples in Teach.

4. Update the PID value of a tuple in Professors when the old PIDvalue is the value of ProfessorPID in one or more tuples of Teach.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 71: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Cascade Policy for Enforcing Referential Integrity

I Only applies to deletions of or updates to tuples in the referencedrelation (e.g., Professors).

I If we delete a tuple in Professors, delete all tuples in Teach thatrefer to that tuple.

I If we update the PID value of a tuple in Professors from p1 to p2,update all values of ProfessorPID in Teach that are p1 to p2.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 72: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Cascade Policy for Enforcing Referential Integrity

I Only applies to deletions of or updates to tuples in the referencedrelation (e.g., Professors).

I If we delete a tuple in Professors, delete all tuples in Teach thatrefer to that tuple.

I If we update the PID value of a tuple in Professors from p1 to p2,update all values of ProfessorPID in Teach that are p1 to p2.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 73: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Set-NULL Policy for Enforcing Referential Integrity

I Also applies only to deletions of or updates to tuples in the referencedrelation (e.g., Professors).

I If we delete a tuple in Professors, set the ProfessorPID attributesof all tuples in Teach that refer to the deleted tuple to NULL.

I If we update the PID value of a tuple in Professors from p1 to p2,set all values of ProfessorPID in Teach that are p1 to NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 74: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Set-NULL Policy for Enforcing Referential Integrity

I Also applies only to deletions of or updates to tuples in the referencedrelation (e.g., Professors).

I If we delete a tuple in Professors, set the ProfessorPID attributesof all tuples in Teach that refer to the deleted tuple to NULL.

I If we update the PID value of a tuple in Professors from p1 to p2,set all values of ProfessorPID in Teach that are p1 to NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 75: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Specifying Referential Integrity Policies in SQL

I SQL allows the database designer to specify the policy for deletes andupdates independently.

I Optionally follow the declaration of the foreign key with ON DELETEand/or ON UPDATE followed by the policy: SET NULL or CASCADE.

I Constraints can be circular, e.g., if there is a one-one mappingbetween two relations.

I In this case, SQL allows us to defer the checking of constraints.(Read Chapter 7.1.3).

I For your project, you do not have to consider deferring constraints.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 76: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Specifying Referential Integrity Policies in SQL

I SQL allows the database designer to specify the policy for deletes andupdates independently.

I Optionally follow the declaration of the foreign key with ON DELETEand/or ON UPDATE followed by the policy: SET NULL or CASCADE.

I Constraints can be circular, e.g., if there is a one-one mappingbetween two relations.

I In this case, SQL allows us to defer the checking of constraints.(Read Chapter 7.1.3).

I For your project, you do not have to consider deferring constraints.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 77: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Specifying Referential Integrity Policies in SQL

I SQL allows the database designer to specify the policy for deletes andupdates independently.

I Optionally follow the declaration of the foreign key with ON DELETEand/or ON UPDATE followed by the policy: SET NULL or CASCADE.

I Constraints can be circular, e.g., if there is a one-one mappingbetween two relations.

I In this case, SQL allows us to defer the checking of constraints.(Read Chapter 7.1.3).

I For your project, you do not have to consider deferring constraints.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 78: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Constraining Attributes and Tuples

I SQL also allows us to specify constraints on attributes in a relationand on tuples in a relation.

I Disallow courses with a maximum enrollment greater than 100.I A chairperson of a department must teach at most one course every

semester.

I How do we express such constraints in SQL?

I How can we change our minds about constraints?

I A simple constraint: NOT NULLI Declare an attribute to be NOT NULL after its type in a CREATE TABLE

statement.I Effect is to disallow tuples in which this attribute is NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 79: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Constraining Attributes and Tuples

I SQL also allows us to specify constraints on attributes in a relationand on tuples in a relation.

I Disallow courses with a maximum enrollment greater than 100.I A chairperson of a department must teach at most one course every

semester.

I How do we express such constraints in SQL?

I How can we change our minds about constraints?I A simple constraint: NOT NULL

I Declare an attribute to be NOT NULL after its type in a CREATE TABLEstatement.

I Effect is to disallow tuples in which this attribute is NULL.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 80: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Attribute-Based CHECK Constraints

I Disallow courses with a maximum enrollment greater than 100.

I This constraint only affects the value of a single attribute in eachtuple.

I Follow the declaration of the Enrollment attribute with the CHECKkeyword and a condition.

CREATE TABLE Courses(...Enrollment INT CHECK (Enrollment <= 100) ...);

I The condition can be any condition that can appear in a WHEREclause.

I CHECK statement may use a subquery to mention other attributes ofthe same or other relations.

I An attribute-based CHECK constraint is checked only when the valueof that attribute changes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 81: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Attribute-Based CHECK Constraints

I Disallow courses with a maximum enrollment greater than 100.

I This constraint only affects the value of a single attribute in eachtuple.

I Follow the declaration of the Enrollment attribute with the CHECKkeyword and a condition.

CREATE TABLE Courses(...Enrollment INT CHECK (Enrollment <= 100) ...);

I The condition can be any condition that can appear in a WHEREclause.

I CHECK statement may use a subquery to mention other attributes ofthe same or other relations.

I An attribute-based CHECK constraint is checked only when the valueof that attribute changes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 82: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Attribute-Based CHECK Constraints

I Disallow courses with a maximum enrollment greater than 100.

I This constraint only affects the value of a single attribute in eachtuple.

I Follow the declaration of the Enrollment attribute with the CHECKkeyword and a condition.

CREATE TABLE Courses(...Enrollment INT CHECK (Enrollment <= 100) ...);

I The condition can be any condition that can appear in a WHEREclause.

I CHECK statement may use a subquery to mention other attributes ofthe same or other relations.

I An attribute-based CHECK constraint is checked only when the valueof that attribute changes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 83: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Attribute-Based CHECK Constraints

I Disallow courses with a maximum enrollment greater than 100.

I This constraint only affects the value of a single attribute in eachtuple.

I Follow the declaration of the Enrollment attribute with the CHECKkeyword and a condition.

CREATE TABLE Courses(...Enrollment INT CHECK (Enrollment <= 100) ...);

I The condition can be any condition that can appear in a WHEREclause.

I CHECK statement may use a subquery to mention other attributes ofthe same or other relations.

I An attribute-based CHECK constraint is checked only when the valueof that attribute changes.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 84: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Tuple-Based CHECK Constraints

I A tuple-based CHECK constraint in a relation is checked whenever atuple is inserted into or updated in that relation.

I Designer may add these constraints after the list of attributes in aCREATE TABLE statement.

I A chairperson of a department must teach at most one course in anysemester.

CREATE TABLE Teach(...CHECK ProfessorPID NOT IN((SELECT ProfessorPID FROM Teach)INTERSECT(SELECT ChairmanPID FROM Departments)));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 85: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Tuple-Based CHECK Constraints

I A tuple-based CHECK constraint in a relation is checked whenever atuple is inserted into or updated in that relation.

I Designer may add these constraints after the list of attributes in aCREATE TABLE statement.

I A chairperson of a department must teach at most one course in anysemester.

CREATE TABLE Teach(...CHECK ProfessorPID NOT IN((SELECT ProfessorPID FROM Teach)INTERSECT(SELECT ChairmanPID FROM Departments)));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 86: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Tuple-Based CHECK Constraints

I A tuple-based CHECK constraint in a relation is checked whenever atuple is inserted into or updated in that relation.

I Designer may add these constraints after the list of attributes in aCREATE TABLE statement.

I A chairperson of a department must teach at most one course in anysemester.

CREATE TABLE Teach(...CHECK ProfessorPID NOT IN((SELECT ProfessorPID FROM Teach)INTERSECT(SELECT ChairmanPID FROM Departments)));

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 87: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Constraints

I SQL allows constraints to be named. You should name yourconstraints!

I Use CONSTRAINT followed by the name of the constraint in front ofPRIMARY KEY, UNIQUE, or CHECK.

I Can use constraint names in ALTER TABLE statements to deleteconstraints: say DROP CONSTRAINT followed by the name of theconstraint.

I Can add constraints in an ALTER TABLE statement using ADDCONSTRAINT followed by an optional name followed by the (required)CHECK statement.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 88: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Constraints

I SQL allows constraints to be named. You should name yourconstraints!

I Use CONSTRAINT followed by the name of the constraint in front ofPRIMARY KEY, UNIQUE, or CHECK.

I Can use constraint names in ALTER TABLE statements to deleteconstraints: say DROP CONSTRAINT followed by the name of theconstraint.

I Can add constraints in an ALTER TABLE statement using ADDCONSTRAINT followed by an optional name followed by the (required)CHECK statement.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 89: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Constraints

I SQL allows constraints to be named. You should name yourconstraints!

I Use CONSTRAINT followed by the name of the constraint in front ofPRIMARY KEY, UNIQUE, or CHECK.

I Can use constraint names in ALTER TABLE statements to deleteconstraints: say DROP CONSTRAINT followed by the name of theconstraint.

I Can add constraints in an ALTER TABLE statement using ADDCONSTRAINT followed by an optional name followed by the (required)CHECK statement.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints

Page 90: Data De nition, Modi cation, and Constraintscourses.cs.vt.edu/.../lectures/...constraints.pdf · T. M. Murali September 9, 2009 CS4604: Data De nition, Modi cation, and Constraints

Data Definition Data Modification Project Constraints Keys and Foreign Keys Attribute and Tuple Constraints

Modifying Constraints

I SQL allows constraints to be named. You should name yourconstraints!

I Use CONSTRAINT followed by the name of the constraint in front ofPRIMARY KEY, UNIQUE, or CHECK.

I Can use constraint names in ALTER TABLE statements to deleteconstraints: say DROP CONSTRAINT followed by the name of theconstraint.

I Can add constraints in an ALTER TABLE statement using ADDCONSTRAINT followed by an optional name followed by the (required)CHECK statement.

T. M. Murali September 9, 2009 CS4604: Data Definition, Modification, and Constraints