Points To Remember - DB
Points To Remember - DB
SUCH A WAY THAT SUPPORTS FOR EASY ACCESS,MODIFY, AND MAINTAIN DATA .
3. DROP:
● DROP TABLE TABLENAME
● DROP DATABASE DATABASENAME
Case Study 1:
A University database contains information about its courses and students. The following table
needs to be created:
1. Question: Write the SQL query to create the Courses table. The CourseID should be set as a
primary key, and the CourseName should have a unique constraint.
Answer:
2. Question: Modify the table to ensure that the Fee column cannot have a NULL value.
Answer:
3. Question: The University realized that some courses may have the same name but different
durations. Remove the unique constraint on the CourseName column.
Answer:
4. Question: The CourseDuration column should be changed to store the duration in years, so
the data type needs to be updated to DECIMAL(3, 1) (e.g., 1.5 for 1 year and 6 months). Write
the query for this modification.
Answer:
5. Question: After a policy update, the University wants to rename the Fee column to
TuitionFee. Write the query to rename this column.
Answer:
ALTER TABLE Courses
CHANGE Fee TuitionFee DECIMAL(8, 2);
---
Case Study 2:
A company stores employee details in the Employees table. The table has the following
structure:
1. Question: Create the Employees table, ensuring that no two employees can have the same
FirstName and LastName combination, and the Salary should be greater than or equal to
10000.
Answer:
Answer:
3. Question: Write the query to drop the foreign key constraint on the DepartmentID column.
Answer:
4. Question: The company needs to ensure that all future salaries are entered with a value of at
least 15000. Modify the Salary column to reflect this change.
Answer:
5. Question: Write a query to remove the unique constraint from the combination of FirstName
and LastName in the Employees table.
Answer:
---
Case Study 3:
A music streaming company maintains a Songs table with the following structure:
1. Question: Write the query to create the Songs table and ensure that the combination of
SongName and Artist is unique, while also ensuring that the ReleaseYear is not before 1900.
Answer:
2. Question: Modify the table to add a new column Genre (VARCHAR) that can only contain one
of the following values: 'Pop', 'Rock', 'Jazz', 'Classical', or 'Hip-Hop'.
Answer:
3. Question: The music company decides that they will no longer track the Album of the songs.
Write the SQL query to remove the Album column.
Answer:
ALTER TABLE Songs
DROP COLUMN Album;
Answer:
5. Question: The company wants to delete all rows from the Songs table where the
ReleaseYear is before 2000. However, the table structure should remain intact. Write the query
to do this.
Answer:
---
Case Study 4:
An e-commerce company needs to manage product details in a Products table. The structure of
the table is as follows:
Answer:
2. Question: Add a new column Category (VARCHAR) to the Products table and make sure that
the Category column can only store values: 'Electronics', 'Clothing', 'Furniture'.
Answer:
Answer:
4. Question: The company wants to ensure that for any product in the Products table, if the
Price is greater than 50000, then the AvailableStock must be at least 10. Write the query to
implement this constraint.
Answer: