DM Lab-4pdf

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

2000080025

DEVALLA SATYA SUBHASH

LAB-4
Primary key constraint in database:

1. Create a table as per the following descriptions and create aadhar as primary key
(insert at least two rows of data):
Student (student_id: integer, students_name: string, phone_number: number,
department: string, aadhar_num: not null)

CREATE TABLE student_details


(
student_id numeric(10) not null,
student_name varchar2(50) not null,
department_name varchar2(50),
phone_number numeric(10) not null,
aadhar_number numeric(12) not null,
CONSTRAINT student_de PRIMARY KEY (aadhar_number)
);

insert into student_details values(1,'x','AI','99999999',2000080025);


insert into student_details values(2,'y','DS',999,200008025);
select * from student_details;

2. For the schema, employee(emp_id, emp_name, phone, department,


passport_num)
create a table and assign primary key to emp_id and passport_num. (insert at
least two rows of data)

CREATE TABLE emp_details


(
emp_id numeric(10) ,
emp_name varchar2(50),
department varchar2(50),
phone_number numeric(10),
passport_number numeric(12),
CONSTRAINT emp_de PRIMARY KEY (emp_id,passport_number)
);

insert into emp_details values(3,'z','DS',9999999,2000080025);


insert into emp_details values(4,'w','DS',9999999,200008025);

select * from emp_details;

3. Create a table with schema patient(p_name, p_id, p_address, phone_num)


a. Add p_id as primary key using alter table

CREATE TABLE patient_lab


(
p_name varchar2(30),
p_id number(10),
p_address varchar2(100),
phone_num number(10),
constraint p_pk primary key(p_id)
);
insert into patient_lab values('tejass', 500, 'maa uru', 2000080025);
select * from patient_lab;

4. For table in question number 1, drop the primary key using alter table and add
primary key on student_id

alter table student_details

drop primary key


alter table student_details

add constraint student_pk

primary key(student_id);

5. Demonstrate disable and enable primary key in table in question 2


alter table emp_details

disable constraint emp_de;

alter table emp_details

enable constraint emp_de;

Foreign key constraints in databases


1. Create a table product (p_id, p_name, manufacturer, man_date) with p_id as primary key
Create a table order (order_id, customer_id, address, p_id) with order_id as primary key
Create p_id in order table as foreign key (insert at least two rows of data in the tables)
create table product(
p_id numeric(10) not null,
p_name varchar2(30) not null,
manufacturer varchar(15) not null,
man_date date,
constraint pro_pk primary key(p_id)
);

insert into product values(1, 'booth', 'apollo company',TO_DATE('07-02-2025', 'DD-MM-


YYYY'));
insert into product values(2, 'bottle', 'apollos company',TO_DATE('27-04-2025', 'DD-MM-
YYYY'));

select * from product;


create table "order"(
order_id varchar(10) not null,
customer_id numeric(10) not null,
address varchar(10) not null,
p_id numeric(10) not null,
constraint ord_pk primary key(order_id),
foreign key (p_id) references product(p_id)
);

insert into "order" values(1,2, 'TPG',2);


insert into "order" values(2,1, 'TPG',2);

select * from "order";

You might also like