Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.

Similar presentations


Presentation on theme: "1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints."— Presentation transcript:

1 1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints Assign user defined names to constraints The Unique Key constraints –Business Rule constraint Column level constraints Table level constraints NULL value concepts NOT NULL constraints defined at the column level The CHECK constraints Defining different constraints on a table The user_constraints table Defining the integrity constraints via the ALTER TABLE command Dropping the integrity constraints via the ALTER TABLE command Default value concepts

2 2 Data Constraints Business rules which are enforced on data being stored in a table are called Constraints. –Applying data constraints CREATE TABLE, ALTER TABLE – attach constraints to a table. Types of data constraints I/O Constraints, Business Rule constraint. –I/O constraints The PRIMARY KEY constraints Can be defined by CREATE TABLE, ALTER TABLE statement PRIMARY KEY constraints defined at column level ( ) PRIMARY KEY PRIMARY KEY constraints defined at table level PRIMARY KEY (, ) The FOREIGN KEY constraints It represents relationships between tables. FOREIGN KEY constraints defined at the column level ( ) REFERENCES [( )] [ON DELETE CASCADE] FOREIGN KEY constraints defined at the table level FOREIGN KEY ( ) REFERENCES [(, )]

3 3 Assign user defined names to constraints CONSTRAINT eg. “emp_code” varchar(5) CONSTRAINTS p_empCode PRIMARY KEY CONSTRAINTS f_dept FOREIGN KEY dept REFERENCES ….. The Unique Key constraints The Unique column constraints permits multiple entries of NULL into column. UNIQUE constraints defined at the column level ( ) UNIQUE UNIQUE constraints defined at the table level CREATE TABLE tablename (, ) UNIQUE (, )); –Business Rule constraint CHECK Column level constraints Table level constraints NULL value concepts Operands such as =, cannot be used on a null value. IS NULL, IS NOT NULL conditions have to be used. NOT NULL constraints defined at the column level NOT NULL The CHECK constraints It must be specified as a logical expression that evaluates either to TRUE or FALSE. It takes longer to execute.

4 4 CHECK constraints defined at the column level CHECK ( ) CHECK constraints defined at the table level CHECK ( ) The user_constraints table DESCRIBE: column name, data types, size and not null. USER_CONSTRAINTS provides information bound to the names of all the constraints. (Owner, constraint_name, constraint_type, table_name, search_condition) eg. SELECT owner, constraint_name, constraint_type FROM USER_CONSTRAINTS WHERE table_name = “xyz”; Defining the integrity constraints via the ALTER TABLE command Integrity constraints can be defined using the constraints clause in the ALTER TABLE command. eg. ALTER TABLE tablename ADD PRIMARY KEY (column1); ALTER TABLE table1 ADD CONSTRAINT f_key FOREIGN KEY (column1) REFERENCES table2 MODIFY(column2 NOT NULL); Dropping the integrity constraints via the ALTER TABLE command ALTER TABLE table1 DROP PRIMARY KEY; ALTER TABLE table1 DROP CONSTRAINT f_key;

5 5 Default value concepts DEFAULT ;


Download ppt "1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints."

Similar presentations


Ads by Google