Not Null Constraints
Not Null Constraints
Date-31-08-2021
INTEGRITY CONSTRAINTS-
DOMAIN INTEGRITY CONSTRAINTS-
It verifies whether the data entered is in proper form and
also they set a range for the input data.
SYNTAX-
create table tablename (columnname1 datatype not null,
columnname2 datatype, columnname3 datatype, ....);
QUERY-
create table job(id int(5) not null,name char(10),job char(10));
OUTPUT-
Check constraints-
PURPOSE-
SYNTAX-
QUERY-
CREATE TABLE student (id INTEGER PRIMARY KEY,name TEXT NOT
NULL, gender TEXT NOT NULL,check(id>1);
OUTPUT-
ENTITY INTEGRITY CONSTRAINTS
Unique constraints-
PURPOSE-
SYNTAX-
SYNTAX-
OUTPUT-
REFERENTIAL INTEGRITY CONSTRAINTS-
PURPOSE-
Syntax-
QUERY-
alter table product_info add(type char(5) not null);
QUERY-
alter table pc add primary key (model_no);
desc pc;
14. Create suitable foreign keys for all the tables of the above
database schema
QUERY-
Alter table laptop
add foreign key(Model_No) references Product_info(Model_no);
Alter table printer
add foreign key(Model_No) references Product_info(Model_No);
Alter table Pc
add foreign key(Model_No) references Product_info(Model_No);
5. Check Constraints:
a. Apply a check constraint on the product_info table such that the only
permitted values for the type column are ‘pc’, ‘lp’ and ‘pr’.
QUERY-
alter table Product_info
Modify column Type Varchar(50) check(Type = PC OR Type = LP OR Type =
PR);
QUERY-
Alter table PC Modify column price Integer check(Price > 0);
Alter table laptop
Modify column price integer check(price > 0);
Alter table Printer
Modify column Price integer check(price > 0);
6. What are the different values for constraint_type in the
user_constraints table and what is the meaning of each of
those values?
In the table’s Product_info, Primary key is used as constraint for
Model_No To ensure that the model number is unique and not null.
Then check constraint is used to ensure that price is not below 0. And
since the product is laptop or Pc or printer, check constraint with OR
operator is used for type. Then a not null constraint is used in type of
Product_info table. And then foreign key is used in table Pc,Laptop
and Printer to link with table Product_info.
QUERY-
create table type_info as select model_no,type from product_info;
desc type_info;
8. Modify the table printer by adding a column printercode of
type varchar2(10) to identify the printer uniquely in the world.
QUERY-
alter table type_info add column printercode char(10);
desc type_info;
QUERY-
alter table type_info add unique(printercode);
desc type_info;
10. Increase the width of the column printercode by 2.
QUERY-
alter table type_info modify column printercode char(12);
desc type_info;
11. Without removing the table definition from the database remove all the
rows from the table ‘Type_info’.