Query : CREATE Table student_details(Roll_No Integer NOT NULL, Name varchar(10) NOT NULL, Marks Integer); |
NOTE : Once a table is created, NOT NULL constraints cannot be added after that. It needs to be added while creating the tables. |
For example : Consider a student_details table. We can add the UNIQUE constraint to the Roll_No column as all the values would be unique. This can be done by executing below query.
Query : CREATE Table student_details(Roll_No Integer NOT NULL UNIQUE, Name char(20) NOT NULL, Marks Integer); |
For example : Consider a student_details table. We can only add a single PRIMARY KEY constraint to Roll_No. Also, Admission_No will be a unique value can be treated as either a UNIQUE value or as PRIMARY KEY. Both cannot be treated as PRIMARY KEY together.
Query : CREATE Table student_details(Roll_No Integer PRIMARY KEY NOT NULL, Admission_No varchar(10) UNIQUE NOT NULL, Name char(20) NOT NULL, Marks Integer); |
For example : Consider two tables student_details and student_relation. FOREIGN KEY constraint can be added as :
Query : CREATE Table student_details(Roll_No Integer PRIMARY KEY NOT NULL, Name char(10) NOT NULL, Address varchar(20), Age Integer); |
Query : CREATE Table student_relation(Roll_No Integer UNIQUE, Marks Integer); |
For example : We can add a “CHECK” condition against the “Marks” column in student_details table of “Only values of Marks <= 100 will be accepted” while inserting the values in the table. Below is the query.
Query : CREATE Table student_details(Roll_No Integer PRIMARY KEY NOT NULL, Name Char(10) NOT NULL, Marks Integer CHECK(Marks<=100)); |
For example : Consider the table student_details in which we can take the maximum marks column as DEFAULT constraint. It can be done by executing below query.
Query : CREATE Table student_details(Roll_No Integer PRIMARY KEY NOT NULL, Name char(10), Max_Marks Integer DEFAULT 100, Marks_Scored Integer); |