Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Constraints Ashima Wadhwa.

Similar presentations


Presentation on theme: "Database Constraints Ashima Wadhwa."— Presentation transcript:

1 Database Constraints Ashima Wadhwa

2 Keys It is important that any entity in an entity set be uniquely identifiable. Practically, we use the values of certain attributes to uniquely identify an entity. For example, when the clerk at a bank keys in a customer’s SSN, the customer’s full information can be brought up.

3 Types of Keys Types of Keys: super key candidate key primary key
Foreign Key Secondary Key Unique Key

4 Keys Superkey: is a set of attributes whose value can uniquely identify an entity in the entity set. A superkey contains one or more attributes. Example: The combination of “SSN” and “Name” is a superkey of the following entity set customer.

5 Notationally, we write, Superkey: (SSN, name) Other superkeys for the entity set customer: Superkey: (SSN) Superkey: (SSN, Street) Superkey: (SSN, City) Superkey: (SSN, Name, Street) Superkey: (SSN, Name, City) Superkey: (SSN, Street, City) Superkey: (SSN, Name, Street, City)

6 (2) Candidate key: is a set of one or more attributes whose value can uniquely identify an entity in the entity set, and any attribute in the candidate key cannot be omitted without destroying the uniqueness property of the candidate key. (It is minimal superkey). Example: (SSN, Name) is NOT a candidate key, because taking out “name” still leaves “SSN” which can uniquely identify an entity. “SSN” is a candidate key of customer. Candidate key could have more than one attributes.

7 (3) Primary Key: is the candidate key that is chosen by the database designer as the unique identifier of an entity. The database designer chooses only one candidate key as the primary key in building the system. Example: “SSN” and “License #” are both candidate keys of Driver entity set. The database designer can choose either one as the primary key. In an E-R diagram, we usually underline the primary key attribute(s).

8 Foreign Keys Foreign key – a (simple or composite) column which refers to the primary key of some table in a database. Foreign and primary keys must be defined on same data type. Bordoloi

9 Foreign Key Directors Movies primary key field parent table
relationship child table Movies foreign key field

10 secondary keys. An entity may have one or more choices for the primary key. Collectively these are known as candidate keys. One is selected as the primary key. Those not selected are known as secondary keys. For example, an employee has an employee number, a National Insurance (NI) number and an address. If the employee number is chosen as the primary key then the NI number and address are secondary keys. 

11 Unique Key The attribute which uniquely identifies all the attribute of table is called unique key. Unique key allows null ( two nulls have different values for system)

12 Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way to guarantee that: rows in a table have valid primary or unique key values rows in a dependent table have valid foreign key values that reference rows in a parent table individual column values are valid

13 Database Constraints Integrity rules:
primary key constraint (to enforce existence integrity) unique constraint (to enforce candidate key integrity) foreign key constraint (to enforce foreign key, or referential integrity) check constraint (to restrict a column's values; a partial enforcement of domain integrity) Not Null Constraint ( to restrict a column value to be blank) You specify one or more of these constraints when you use the Create Table statement to create a base table. You can also add or drop constraints with the Alter Table statement.

14 Primary Key Constraints
A primary key serves as the unique identifier for rows in the table. The syntax of the primary key constraint is CREATE TABLE employee  ( id number(5) PRIMARY KEY,  name char(20),  dept char(10),  age number(2),  salary number(10),  location char(10)  ); CREATE TABLE employee ( id number(5) CONSTRAINT emp_id_pk PRIMARY KEY,  name char(20), dept char(10), age number(2), salary number(10), location char(10) );

15 Unique Constraints This constraint ensures that a column or a group of columns in each row have a distinct value. A column(s) can have a null value but the values cannot be duplicated. CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) UNIQUE  ); CREATE TABLE employee ( id number(5) PRIMARY KEY, name char(20), dept char(10), age number(2), salary number(10), location char(10) CONSTRAINT location UNIQUE  );

16 Foreign Key Constraints
This constraint identifies any column referencing the PRIMARY KEY in another table. It establishes a relationship between two columns in the same table or between different tables. For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. One or more columns can be defined as Foreign key. CREATE TABLE product  ( product_id number(5) CONSTRAINT pd_id_pk PRIMARY KEY,  product_name char(20), supplier_name char(20), unit_price number(10) ); CREATE TABLE order_items ( order_id number(5) CONSTRAINT od_id_pk PRIMARY KEY, product_id number(5) CONSTRAINT pd_id_fk REFERENCES, product(product_id), product_name char(20), supplier_name char(20), unit_price number(10) );

17 Check Constraints This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint can be applied for a single column or a group of columns. CREATE TABLE employee  ( id number(5) PRIMARY KEY,  name char(20),  dept char(10),  age number(2),  gender char(1) CHECK (gender in ('M','F')),  salary number(10),  location char(10)  ); 

18 Not Null This constraint ensures all rows in the table contain a definite value for the column which is specified as not null. Which means a null value is not allowed. CREATE TABLE employee ( id number(5), name char(20) CONSTRAINT nm_nn NOT NULL, dept char(10), age number(2), salary number(10), location char(10)  );

19 Questions??


Download ppt "Database Constraints Ashima Wadhwa."

Similar presentations


Ads by Google