Professional Documents
Culture Documents
Constraints
Lab Tasks:
1. Create a Table Student, it should have the following fields, choose appropriate DATA
TYPES.
• Std_id
• Std_name
• Std_program
• Std_current_semester Add a new column , cgpa.
QUERY:
CREATE TABLE STUDENT (
std_id INT,
std_name VARCHAR(255),
std_program VARCHAR(255),
std_current_semester INT,
cgpa FLOAT
);
2. Insert at least 8 records, (at least 4 records should have value of Std_program as ‘bscs’)
QUERY:
INSERT INTO STUDENT
VALUES (8, 'Talha', 'BEE', 4, 2.6);
6. Create a table bonus (bonus Id, bonus amount, department no), insert information of
multiple departments with department numbers 2, 4, 10, 15, 20 and 25.
QUERY:
CREATE TABLE BONUS (
BONUS_ID INT,
BONUS_AMOUNT FLOAT,
DEPARTMENT_NO INT
);
11. Add a table-level PRIMARY KEY constraint to the EMP table on the ID column. The
constraint should be named at creation. Name the constraint my_emp_id_pk. Hint: The
constraint is enabled as soon as the ALTER TABLE command executes successfully.
QUERY:
CREATE TABLE EMP (
ID INT,
CONSTRAINT my_emp_id_pk PRIMARY KEY (ID)
);
12. Create a PRIMARY KEY constraint to the DEPT table using the ID column. The
constraint should be named at creation. Name the constraint my_deptid_pk. Hint: The
constraint is enabled as soon as the ALTER TABLE command executes successfully.
QUERY:
CREATE TABLE DEPT (
ID INT,
CONSTRAINT my_deptid_pk PRIMARY KEY (ID)
);
13. Add a column DEPT_ID to the EMP table. Add a foreign key reference on the EMP table
that ensures that the employee is not assigned to a nonexistent department. Name the
constraint my_emp_dept_id_fk.
QUERY:
ALTER TABLE EMP
ADD DEPT_ID INT NOT NULL,
ADD CONSTRAINT my_emp_dept_id_fk FOREIGN KEY (DEPT_ID) REFERENCES
DEPT (DEPT_ID);