One of the easiest method to try out SQL commands is through phpmyadmin interface.
Refer to the basic and categories of SQL commands.
1. Start XAMPP Apache and MySQL servers.
2. In browser, access localhost/phpmyadmin interface
DDL (Data Definition Language) commands
3. Create the database: swd
To create database, the command is CREATE DATABASE xxx;
SQL Commands are case-insensitive but for name of database, tables and fields, they are case sensitive. So we could write “CREATE DATABASE swd;’ instead.
Each SQL command ends with ‘;’
4.1 Create the staff table with the given definitions below:
Since each SQL command ends with ‘;’ and white spaces such as blanks and next line have NO effect on the command, we should use white spaces to make code readable and easier to check.
To create table, the command is CREATE TABLE xxx (aaaa, bbbb, cccc);
Do NOT type the following since white space is NOT used to format.
Explanation of each part is as follows:
End each field definition with ‘,’ except the last one.
Difference between command and using the phpmyadmin interface to define a field can or cannot be empty: In the phpmyadmin interface, we only check the null box when the field can be empty. Whereas using command, we need to specify when the field cannot be empty.
4.2 Create the classes table with given definitions below:
5. Create relationship between staff and classes:
Note that ‘—’ is used to add comments:
6. Confirm that the staff table, classes table and their relationship are created.
DML (Data Manipulation Language) commands
7. Insert values to the staff table:
To add values, the command is ‘INSERT INTO xxx (yyy, yyy, yyy) values (aaa, aaa, aaa), (bbb, bbb, bbb), (ccc, ccc, ccc);’
8. Insert values to the classes table:
9. Make sure the values are added:
We tried a few SELECT commands to retrieve data from the database.
That’s all for this simple tutorial. Refer to the basic and categories of SQL commands for more details.