Practical Assignment 1
-- 1. Create a database named School
CREATE DATABASE School;
-- Output:
-- Query OK, 1 row affected (0.01 sec)
-- 2. Make the database School active
USE School;
-- Output:
-- Database changed
-- 3. Create a table/relation GUARDIAN with the specified attributes
CREATE TABLE GUARDIAN (
GUID CHAR(12) PRIMARY KEY,
GName VARCHAR(20) NOT NULL,
GPhone CHAR(10) UNIQUE,
GAddress VARCHAR(25) NOT NULL
);
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 4. Add a column GIncome of a suitable type to the GUARDIAN table
ALTER TABLE GUARDIAN
ADD GIncome DECIMAL(10, 2);
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 5. Remove the column GIncome from the GUARDIAN table
ALTER TABLE GUARDIAN
DROP COLUMN GIncome;
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 6. Remove the primary key constraint from the GUARDIAN table
ALTER TABLE GUARDIAN
DROP PRIMARY KEY;
-- Output:
-- Query OK, 0 rows affected (0.02 sec)
-- 7. Change the width of the column GName to store a maximum of 30 characters
ALTER TABLE GUARDIAN
MODIFY GName VARCHAR(30) NOT NULL;
-- Output:
-- Query OK, 0 rows affected (0.02 sec)
-- 8. Create a table/relation STUDENT with the specified attributes
CREATE TABLE STUDENT (
RollNumber INT,
SName VARCHAR(20),
SDateofBirth DATE,
GUID CHAR(12),
FOREIGN KEY (GUID) REFERENCES GUARDIAN(GUID)
);
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 9. Add the primary key constraint to the RollNumber attribute in the STUDENT table
ALTER TABLE STUDENT
ADD PRIMARY KEY (RollNumber);
-- Output:
-- Query OK, 0 rows affected (0.02 sec)
-- 10. Modify the constraint in the column SName so that it does not allow NULL values
ALTER TABLE STUDENT
MODIFY SName VARCHAR(20) NOT NULL;
-- Output:
-- Query OK, 0 rows affected (0.02 sec)
-- 11. Set the default value to the SDateofBirth attribute to 15th May 2022
ALTER TABLE STUDENT
ALTER SDateofBirth SET DEFAULT '2022-05-15';
-- Output:
-- Query OK, 0 rows affected (0.02 sec)
-- 12. Create a table/relation ATTENDANCE with the specified attributes
CREATE TABLE ATTENDANCE (
AttendanceDate DATE,
RollNumber INT,
AttendanceStatus CHAR(1),
PRIMARY KEY (AttendanceDate, RollNumber),
FOREIGN KEY (RollNumber) REFERENCES STUDENT(RollNumber)
);
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 13. Remove the ATTENDANCE relation
DROP TABLE ATTENDANCE;
-- Output:
-- Query OK, 0 rows affected (0.01 sec)
-- 14. Remove the School database
DROP DATABASE School;
-- Output:
-- Query OK, 0 rows affected (0.01 sec)