Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab2 - Integrity constraints.doc
Скачиваний:
29
Добавлен:
20.02.2016
Размер:
217 Кб
Скачать
☆
    1. Keywords and Parameters

      table_ constraint  

      The table_constraint syntax is part of the table definition. An integrity constraint defined with this syntax can impose rules on any columns in the table.  

      The table_constraint syntax can appear in a CREATE TABLE or ALTER TABLE statement. This syntax can define any type of integrity constraint except a NOT NULL constraint.  

      column_ constraint  

      The column_constraint syntax is part of a column definition. Usually, an integrity constraint defined with this syntax can impose rules only on the column in which it is defined.

      • The column_constraint syntax that appears in a CREATE TABLE or ALTER TABLE ADD statement can define any type of integrity constraint.

      • Column_constraint syntax that appears in an ALTER TABLE MODIFY column_options statement can only define or remove a NOT NULL constraint.

      CONSTRAINT  

      identifies the integrity constraint by the name constraint.  If you do not specify NULL or NOT NULL in a column definition, NULL is the default.

      UNIQUE  

      designates a column or combination of columns as a unique key. To satisfy a UNIQUE constraint, no two rows in the table can have the same value for the unique key. However, the unique key made up of a single column can contain nulls.

      A composite unique key is made up of a combination of columns. To define a composite unique key, you must use table_constraint syntax rather than column_constraint syntax. Any row that contains nulls in all key columns automatically satisfies the constraint. However, two rows that contain nulls for one or more key columns and the same combination of values for the other key columns violate the constraint.

      You cannot designate the same column or combination of columns as both a unique key and a primary key.

      PRIMARY KEY  

      designates a column or combination of columns as the table's primary key. A composite primary key is made up of a combination of columns. To define a composite primary key, you must use the table_constraint syntax rather than the column_constraint syntax.

      A table can have only one primary key.

      No primary key value can appear in more than one row in the table.

      No column that is part of the primary key can contain a null.

      You cannot designate the same column or combination of columns as both a primary key and a unique key

      NULL

      NOT NULL  

       

       

      determines whether a column can contain nulls. You must specify NULL and NOT NULL with column_constraint syntax, not with table_constraint syntax.  

      NULL  

      specifies that a column can contain null values. The NULL keyword does not actually define an integrity constraint. If you do not specify either NOT NULL or NULL, the column can contain nulls by default.  

      NOT NULL  

      specifies that a column cannot contain null values. To satisfy this constraint, every row in the table must contain a value for the column.  

      Referential integrity constraints

       

       

       

       

       

       

       

       

      Referential integrity constraints designate a column or combination of columns as the foreign key and establish a relationship between that foreign key and a specified primary or unique key, called the referenced key. The table containing the foreign key is called the child table, and the table containing the referenced key is called the parent table. The foreign key and the referenced key can be in the same table. In this case, the parent and child tables are the same.  

      From the table level, specify referential integrity using the foreign_key_clause with the table_constraint syntax. This syntax allows you to specify a composite foreign key, which is made up of a combination of columns.

      From the column level, use the REFERENCES clause of the column_constraint syntax to specify a referential integrity constraint in which the foreign key is made up of a single column.  

      You can designate the same column or combination of columns as both a foreign key and a primary or unique key.

      You can define multiple foreign keys in a table. Also, a single column can be part of more than one foreign key.  

      Restrictions:

      • The referenced UNIQUE or PRIMARY key constraint on the parent table must already be defined.

      • The child and parent tables must be on the same database.

      • You cannot define a referential integrity constraint in a CREATE TABLE statement that contains an AS subquery clause. Instead, you must create the table without the constraint and then add it later with an ALTER TABLE statement.

      foreign_key_clause

      designates a column or combination of columns as the foreign key from the table level. You must use this syntax to define a composite foreign key.

      To satisfy a referential integrity constraint involving composite keys, either the values of the foreign key columns must match the values of the referenced key columns in a row in the parent table, or the value of at least one of the columns of the foreign key must be null.  

      A composite foreign key must refer to a composite unique key or a composite primary key.

      REFERENCES  

      designates the current column or attribute as the foreign key and identifies the parent table and the column or combination of columns that make up the referenced key. If you identify only the parent table and omit the column names, the foreign key automatically references the primary key of the parent table. The corresponding columns of the referenced key and the foreign key must match in number and datatypes.  

      ON DELETE

       

      determines how Oracle automatically maintains referential integrity if you remove a referenced primary or unique key value. If you omit this clause, Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table.  

      • CASCADE specifies that Oracle removes dependent foreign key values.

      • SET NULL specifies that Oracle converts dependent foreign key values to NULL.

      CHECK  

       

      specifies a condition that each row in the table must satisfy. To satisfy the constraint, each row in the table must make the condition either TRUE or unknown (due to a null). When Oracle evaluates a CHECK constraint condition for a particular row, any column names in the condition refer to the column values in that row.

      If you create multiple CHECK constraints for a column, design them carefully so their purposes do not conflict. Oracle does not verify that CHECK conditions are not mutually exclusive.  

      The condition of a CHECK constraint can refer to any column in the table, but it cannot refer to columns of other tables.

  1. Lab tasks

Create the following tables with integrity constraints.

NOTE. Our database has so cold recursive integrity constraint definitions/ It means the following: The TEACHER table references to the DEPARTMENT table and DEPARTMENT table references to the FACULTY table/ Ay the same time TEACHER and DEPARTMENT tables reference to the TEACHER table. That is why these tables are defined in such a way:

1) First of all the FACULTY and DEPARTMENT tables are defined. And at the same time the columns DeanFK HeadFK (it foreign keys))are defined without TEREFENCES clause

2) The table TEACHER is defined with all references keys

3) At last with the help of ALTER TABLE statement the REFERENCES close is defined for columns DeanFK and HeadFK

NOTE. In terms of ER-modelling language our database has the following structure

Table name

Column name

Column

data type

Length

(preci­sion)

Scale

Column constraint

FACULTY

FacPK

integer

PRIMARY KEY

Name

varchar2

50

UNIQUE, cannot be NULL

DeanFK

integer

Dean of the faculty. It is a foreign key references to the TchPK column in the TEACHER table. If the dean is deleted from TEACHER table than the reference is set to NULL

Building

char

5

Building of the faculty. Takes the values: ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’10’

Fund

number

9

2

fund cannot be less than 100000

table constraint

DEPARTMENT

DepPK

integer

PRIMARY KEY

FacFK

integer

FOREIGN KEY referenced to FacPK of the FACULTY. It is not possible to delete faculty if it has at least one department.

Name

varchar2

50

Department name. Cannot be NULL

HeadFK

integer

Head of the department. It is a foreign key references to the TchPK column in the TEACHER table. If the head is deleted from TEACHER table then the reference is set to NULL

Building

char

5

Building of the department. Takes the values: ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’10’

Fund

number

8

2

Fund take the values in the interval 20000.00- 100000

table constraints

pair of columns (FacFK,name)is UNIQUE (that is the faculty cannot have different departments with identical names)

TEACHER

TchPK

integer

PRIMARY KEY

DepFR

integer

FOREIGN KEY referenced to DepNo of DEPARTMENT. If referenced department is deleted the key set to NULL.

Name

varchar2

50

Name of the teacher, cannot be NULL

Post

varchar2

20

Post of the teacher. Take the values from the list: assistant, counselor, assistant professor, professor

Tel

char

7

Hiredate

date

Hire date. should be more than 01.01.1950; cannot be NULL

Salary

number

6

2

Must be more than 1000, cannot be NULL

Commission

number

6

2

cannot be negative. DEFAULT value is 0

ChiefFK

Integer

Chief of the teacher. It is the foreign key that references to the primary key TchPK of the TEACHER table. If a teacher is deleted all references to it is set to NULL

table constraint

a) Commission should be at least twice less than salary

b) Salary + Commission is in interval 1000-3000

c) ChiefFK cannot be equal to TchPK (that is teacher cannot be chief of of himself)

SGROUP

GrpPK

integer

PRIMARY KEY

DepFK

integer

FOREIGN KEY referenced to DepPK of DEPARTMENT. If referenced department is deleted the key set to NULL.

Course

number

1

take the values from the list: 1, 2, 3, 4, 5, 6

Num

number

3

Group numbe. Takes the values in the interval 0-700

Quantity

number

2

Number of students in the group/ Take the value in the interval 1 - 50

Curator

integer

FOREIGN KEY referenced to TchPK of TEACHER. If referenced department is deleted the key set to NULL.

Rating

number

3

take the values in the interval 0 – 100. DEFAULT value is 0.

table constraints

Pair of values (DepFK, Num) is UNIQUE (that is any department cannot have groups with identical numbers)

Pair of foreign keys (DepFK, Curator) is UNIQUE. That is any teacher may be curator in only one group in any department.

SUBJECT

SbjPK

integer

PRIMARY KEY

Name

varchar2

50

cannot be NULL

table constraint

ROOM

RomPK

integer

PRIMARY KEY

Num

number

4

Room number, cannot be NULL

Seats

number

3

Number seats in the room, is in interval 1 - 300

Floor

number

2

Floor of the room, is in interval 1 – 16

Building

char

5

Building of the room/ cannot be NULL. Take values from the following list: ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’10’

table constraint

pair of values (Num, Building) is UNIQUE

LECTURE

TchFK

integer

FOREIGN KEY references to TchPK of TEACHER. If references teacher is deleted the key is set to NULL.

GrpFK

integer

FOREIGN KEY references to GrpPK of SGROUP. If referenced group is deleted the corresponding lecture is deleted too (cascade deletion).

SbjFK

integer

FOREIGN KEY references to SbjNo of SUBJECT. It is not possible to delete subject if there exists at least one lecture that is referenced to this subject.

RomNo

integer

FOREIGN KEY referenced to RomNo of ROOM. If referenced room is deleted the key set to NULL.

Type

varchar2

25

Type of the lecture. Take the values from the list: lecture, lab, seminar, practice. Cannot be NULL

Day

char

3

Day of the week. Take the values from the list: sun, mon, tue, wed, thu, fri, sat.

Cannot be NULL

Week

number

1

Week of the lesson Take the values 1 or 2. Cannot be NULL

Lesson

number

1

Take the values in the interval 1 – 8.

Cannot be NULL

table constraint

Values of columns (GrpFK, Day, Week, Lesson) is UNIQUE. That is one group cannot has two or more different lectures in the same lesson, day and week.

Values of columns (TchFK, Day, Week, Lesson) is UNIQUE. That is the same condition is true for any teacher

Соседние файлы в папке English