You are on page 1of 7

IT248-DBMS 18IT072

PRACTICAL -3
Aim: To apply the concept of integrity/data constraints while creating or altering
table.

Theoretical Description:
A SQL Primary Key is a field in a table which remarkably distinguishes each
line/record in a database table. Primary keys must contain unique values. A
primary key segment can’t have NULL qualities.
A foreign Key is a key used to connect two tables together.
A table can have just a single primary key, which may comprise of single or
different fields.

When applied to a relation declaration, the clause check(P) specifies a predicate P


that must be satisfied by every tuple in a relation. A common use of the check
clause is to ensure that attribute values satisfy specified conditions, in effect
creating a powerful type system.

Query-1: Create Table Salespeople where Snum number(4) P.K, Sname


varchar2(20)NOT NULL, City varchar2(15), Comm number(5,2).

SQL Statement:
Create Table Salespeople(Snum number(4) Primary key, Sname varchar2(20) NOT
NULL, City varchar2(15), Comm number(5,2));
Output:

Table created.

Query-2: Create Table customer where Cnum number (4) P.K, Cname
varchar2(20)NOT NULL, City varchar2(20), Rating number(3) DEFAULT 10, Snum
number(4) F.K.(where snum refer salespeople table).

SQL Statement:
Create Table customer(Cnum number(4) Primary key,
Cname varchar2(20) NOT NULL,
City varchar2(20),
Rating number(3) DEFAULT 10,
Snum number(4) references Salespeople);

CSPIT Department of Information Technology Page 1


IT248-DBMS 18IT072

Output:
Table created.

Query-3: Create table Order where Order_no number(4) P.K, Amount number(5),
Odate varchar2(10), Cnum number(4) F.K, (where cnum refer customer table). Snum
number(4) F.K (where snum refer salespeople table).

SQL Statement:
Create table Order1(Order_no number(4) Primary key,
Amount number(5),
Odate varchar2(10),
Cnum number(4) references customer,
Snum number(4) references salespeople);
Output:
Table created.

Query-4: Table: Sales_order

SQL Statement:
Create table Sales_order(Order_no Varchar2(6) Primary key,Order_date Date,
Client_no Varchar2(6) references client_master,
Dely_addr Varchar2(25),
Salesman_no Varchar2(6) references Salesman_master,
Dely_type char check(Dely_type in ('P','F')),
Dely_date date,
Order_status Varchar2(15) check(Order_status in('In
Process','Fulfilled','Backorder','Cancelled')));

CSPIT Department of Information Technology Page 2


IT248-DBMS 18IT072

Output:
Table created.

Query-5: Table: salesman_master

SQL Statement:
Create table Salesman_master(Salesman_no Varchar2(6) Primary key,
Salesman_name Varchar2(20) Not null,
Address Varchar2(30) Not null,
City Varchar2(20),
Pincode Varchar2(8),
State Varchar2(20),
Sal_amt Number(8,2) Not null check(sal_amt!=0),
Ytd_sales Number(6,2) Not null check(Ytd_sales!=0),
Tgt_sales Number(6,2) Not null);

Output:
Table created.

CSPIT Department of Information Technology Page 3


IT248-DBMS 18IT072

Query-6: Table: Client_master

SQL Statement:
Create table Client_master(Client_no Varchar2(6) Primary key,
Name Varchar2(20) Not null,
Address Varchar2(30),
City Varchar2(15),
Pincode Number(8),
State Varchar2(15),
Bal_due Number(10,2));
Output:

Table created.

Query-7: Insert the following records in salespeople.

SQL Statement:
insert into salespeople values(1001,'Peel','London',0.12);
insert into salespeople values(1002,'Serres','San Jose',0.13);
insert into salespeople values(1004,'Motika','London',0.11);
insert into salespeople values(1007,'Rifkin','Barcelona',0.15);
insert into salespeople values(1003,'Axelord','New York',0.10);

CSPIT Department of Information Technology Page 4


IT248-DBMS 18IT072

Output:
1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

Query-8: Insert the following records in Customer.

SQL Statement:
insert into customer values(2001,'Hoffman','London',100,1001);
insert into customer values(2002,'Giovanne','Rome',200,1003);
insert into customer values(2003,'Liu','San Jose',300,1002);
insert into customer values(2004,'Grass','Berlin',100,1002);
insert into customer values(2006,'Clemens','London',300,1004);
insert into customer values(2007,'Pereira','Rome',100,1004);
Output:
1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

CSPIT Department of Information Technology Page 5


IT248-DBMS 18IT072

Query-9: Insert the following records in Order.

SQL Statement:
insert into order1 values(3001, 18.96, '10-mar-94', 2002, 1002);
insert into order1 values(3003, 767.19 ,'10-mar-94',2001, 1001);
insert into order1 values(3002, 1900.10,'10-mar-94', 2007, 1003);
insert into order1 values(3005, 5160.45,'10-mar-94', 2003, 1002);
insert into order1 values(3006, 1098.16,'10-mar-94', 2004, 1002);
insert into order1 values(3009, 1713.23,'10-apr-94', 2002, 1003);
insert into order1 values(3007, 75.75,'10-apr-94', 2004, 1002);
insert into order1 values(3008, 4723.95,'10-may-94', 2006, 1001);
insert into order1 values(3010, 1309.95,'10-jun-94', 2004, 1002);
insert into order1 values(3011, 9891.00,'10-jun-94', 2006, 1001);
Output:
1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

CSPIT Department of Information Technology Page 6


IT248-DBMS 18IT072

Question-1: Why check clause used in sql?


Answer: The CHECK clause is used to limit the value range that can be placed in a
column.

Question-2: Difference between Primary Key and Foreign Key.


Answer:
Primary Key Foreign Key
Primary key uniquely identifies a Foreign key is a field in the table that is
record in the table. primary key in another table.
Primary Key can't accept null values Foreign key can accept multiple null
value
By default, Primary key is clustered Foreign key do not automatically create
index and data in the database table is an index, clustered or non-clustered.
physically organized in the sequence of You can manually create an index on
clustered index. foreign key.
We can have only one Primary key in a We can have more than one foreign
table. key in a table.

CSPIT Department of Information Technology Page 7

You might also like