Thursday, October 5, 2017

Create Table based on ERD

Given a ERD table as in Figure 1 below:
Figure 1: Entity Table Student
To create a table using ERD, first need to login into the database using the following instructions. Since this is a new lab, we gonna create a new database with your Matric Number as the database name. The instructions to create and use the database are as below:
  1. Login to the MySQL/MariaDB Console like in Lab 1.
  2. Create a database the name "dbXXXXXX" where XXXXXX is your Matric Number.
  3. select the database using the "use [database name]" command.
  4. Now you are in the database.
The command to create a table is shown below:
Figure 2: Syntax to create a new table.
The table in Figure 1 can be created through the following command:
Figure 3: Syntax to create the Table "Students"
The result of the table creation can be viewed through the show tables command, while the metadata of the table can be viewed through the describe table command. Both are shown in action in Figure 4 below:
Figure 4: shows and describe "Students" table.
In the newly created "Students" table, primary are not defined. Therefore a new table called "students_with_pk" is created using the syntax in Figure 5 below:
Figure 5: Creating a students table with primary key.
Comparing the SQL command to create table in Figure 3 and Figure 5, it is noticed that the "not null" command is added behind the line "stdid int(10)". This line is to show that the column or attributes can never be null value in an entries. While the extra "primary key (stdid)" indicated that the "stdid" column is the primary key for this table.