Integrity Constraints ===================== => are also represented as "constraints". Why we need to use constraints? -------------------------------- => While defining the database objects by following business rules constraints can be used. => The advantage with the constraints is: avoiding of duplication of the data Ex: create table Books( sno int, book_id number, name_book varchar2(50), author varchar(50) ); insert into Books values(1,102938,'Java Fullstack Guide','James Gosling'); insert into Books values(1, 112131, 'Java Fullstack Preparation Guide','Ashok'); select * from Books; How to define the constraints? ------------------------------- => The constraints can be defined in two ways: 1) Column-level 2) Table-level => If the constraint can be defined within the column definition are called as "column-level constraints". Syntax: ------- create table ( col1 datatype costraint_type, col2 datatype ); => If the constraint can be defined after all the definitions of columns of the table, then that constraint is called as "Table-level constraint". Syntax: ------- create table ( col1 datatype, col2 datatype, Constraint constraint_type(column_name) ); Here: "constraint" is a class. to define the "table-level constraints" Note: ----- 1) For column level constraints, the constraint-name can be assigned by the oracle database automatically. 2) Whereas for the table level constraints the constraint-name can be assigned by the programmer at the time of the definition. Types of Constraints: ---------------------- => The total constraints in Oracle are: 1) unique constraint 2) not null constraint 3) check constraint 4) default constraint 5) primary key constraint 6) foreign key constraint Unique Constraints: =================== => to define the table data as "unique" we can use "unique constraint". => Disadvantage: not stop to enter null values. Column-level Unique Constraint: ------------------------------- Syntax-1: -------- create table table-name( col1 datatype unique, col2 datatype ); create table clients1( sno number(6) unique, client_id int, first_name varchar(50), last_name varchar(50), unique(client_id) ); insert into clients1 values(1,10203040,'Sanjy','Sahu'); insert into clients1 values(2,11213141,'Ashok','Guptha'); Table-level Unique constraint: ------------------------------ Syntax-1: -------- create table table-name( col1 datatype, col2 datatype, unique(col2) ); Syntax-2: --------- create table table-name( col1 datatype, col2 datatype, col3 datatype, ..... constraint unique(column-name); ); create table products1( sno number(6), product_id int, product_name varchar(50), price decimal(10,2), constraint pid unique(product_id) ); How to apply the unique constraints for the existing table: ------------------------------------------------------------ => using "alter" command we can define the constraint for the existing table. Syntax: alter table add constraint unique(column-name); create table customers( sno number(6), customer_id int, customer_name varchar(50), location varchar(30), purchage_cost decimal(10,2) ); insert into customers values(1,101122,'Gautahm','Hyderabad',1999); select * from customers; alter table customers add constraint custid unique(customer_id); Assignment: ----------- 1) Write a SQL query to disable the constraint which we have created? 2) Write a SQL query to enable the constraint which we have created and disabled? 3) Write a SQL query to add unique constraint for more than one column at a time within the table?