Creating and Modfying the Tables in Databases
In this lab, you will work on creating tables (relations) and adding/deleting records to/from the tables. You will learn also how to modify a database by altering the tables (adding/deleting attributes) or even deleting the whole table. When writing the SQL
queries, it is good to keep a copy of the queries in a plain text document so that you can use them later (if needed).
Note: we will use table and relation exchangeable unless something else is specified.
Creating table
During the first weeks of the course, we will use the University database from the Database System Concepts book. Over the first two weeks of the course, it would be good to create all the tables and enter a few records in the student, instructor and department relations.
- Write the
SQL
query that creates one or more tables. For now, you don’t need to specify the primary/foreign keys or any additional constraints. Just the data type of each attribute.
Modifying the tables
- Write an
SQL
query to add an attribute major in the student table.
ALTER TABLE STUDENT ADD COLUMN MAJOR;
- If you have inserted a few records in the student table before, display the records of the table and check the values in the major attribute.
SELECT * FROM STUDENT;
We can see the output as follows:
- Before performing the next tasks, make sure that you have the query for creating the tables that you will work on and you have the queries for inserting the records again.
- Delete all the records from the table. Make sure that the table itself is not deleted.
DELETE FROM STUDENT;
This query will delete the data but not the schema.
- Delete the table and its content. Comment on the differences between the two queries.
DROP TABLE student;
This query will delete the data and the schema.