You are on page 1of 4

Lab – 08

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);

3. Update bscs to BS(COMPUTER SCIENCE) in Student Table.


QUERY:
UPDATE STUDENT
SET STD_PROGRAM = 'BS(Computer Science)'
WHERE STD_PROGRAM = 'BSCS';
4. Rename table to student_bahria
QUERY:
RENAME STUDENT TO STUDENT_BAHRIA;

5. Delete Std_current_semester column from table.


QUERY:
ALTER TABLE STUDENT_BAHRIA
DROP COLUMN STD_CURRENT_SEMESTER;

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
);

7. Delete row when department no is equal to 20,


QUERY:
DELETE FROM BONUS
WHERE DEPARTMENT_NO = 20;

8. Increment bonus when department no is equal to 10 ,


QUERY:
UPDATE BONUS
SET BONUS_AMOUNT = BONUS_AMOUNT + (BONUS_AMOUNT * 0.10)
WHERE DEPARTMENT_NO = 10;

9. Delete row where department is between 10 and 20.


QUERY:
DELETE FROM BONUS
WHERE DEPARTMENT_NO BETWEEN 10 AND 20;

10. Delete rows where department number is greater than 20.


QUERY:
DELETE FROM BONUS
WHERE DEPARTMENT_NO > 20;

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);

You might also like