SQL can be split into different language types:
Here is a breakdown in the diffferences between the two:
| DDL | DML | |
|---|---|---|
| purpose | Defines or changes the structure of a database | Retrieves or changes the data stored in the structures |
| Common SQL statements | CREATE / ALTER / DROP | SELECT / INSERT / UPDATE / DELETE |
We can use DDL to CREATE a new table in a database:
During the design of the database, we can use DROP to delete a table or column, or ALTER to change the structure of a table eg:
Now that we have created some data structures (tables) in the database, we can manipulate the data using DML
You are a database administrator.
Open your database software eg XAMPP and create a new database called SchoolBase
Now continue to the challenges.
Using Data Definition Language, create the following 2 database tables:
| Field | Info |
|---|---|
| studentID | INT(3), PRIMARY KEY |
| firstName | VARCHAR(16) |
| lastName | VARCHAR(16) |
| yearGroup | INT(2) |
| VARCHAR(36) |
| Field | Info |
|---|---|
| clubID | INT(3), PRIMARY KEY |
| clubName | VARCHAR(24) |
| Day | VARCHAR(9) |
| Field | Info |
|---|---|
| signupID | INT AUTO_INCREMENT PRIMARY KEY |
| clubID | INT(4) |
| studentID | INT(4) |
Run the following SQL statements:
School admin is contacting you, the database administrator, to handle the following requests.
Use your skills and knowledge of DDL and DML to handle the requests.
The school has changed the name of the Robotics club to RoboClub
Alex has left the Football club. Delete the appropriate record from the SignUp table. Kudos if you can do this using joins!
The school wants to record the location that each club is help in.
Add a new field/column to the Club table called Location. Choose an appropriate datatype.
The following club locations have been confirmed:
| Club | Location |
|---|---|
| RoboClub | Rm138 |
| Football | Field |
| Chess | Library |
Update the Club table to reflect these changes.
Select the last name and first name of every student in the RoboClub. Order by last name.
This activity was intended to introduce the difference between DDL and DML in SQL.
Hopefully, it also gave you a chance to review how to select specific data from a database.
This would have included using JOINS.
If you had wanted to restrict access to the database, you could have created some views instead.
The view would provide a layer of security between the user of the database and the database itself.
There are 2 types of view:
ACIDpessimistic locking atomicity consistency isolation TCL durabilityDDL DMLCREATE ALTERUPDATE INSERT INTOBEGIN TRANSACTION COMMITROLLBACK