Table is a collection of data, organized in terms of rows and columns. In DBMS term, table is known as relation and row as tuple. Table is the simple form of data storage. A table is also considered as a convenient representation of relations.
EMP_NAME | ADDRESS | SALARY |
---|---|---|
John | Kigali | 15000 |
Claude | Nyagatare | 18000 |
Benoit | Kigali | 20000 |
In the above table, "Employee" is the table name, "EMP_NAME", "ADDRESS" and "SALARY" are the column names. The combination of data of multiple columns forms a row e.g. "John", "Kigali" and 15000 are the data of one row.
SQL CREATE TABLE statement is used to create table in a database. If you want to create a table, you should name the table and define its column and each column's data type.
Let us take an example to create a myFirstDB table with ID as primary key and NOT NULL are the constraint showing that these fields cannot be NULL while creating records in the table.
Tip: If you are using phpMyAdmin, you will click SQL menu and write the code above then click go, but if you are using CMD, you will need to start CMD, start mysql in your CMD and then write the code.
If you have successfully created the table, you may check it by looking at the message provided by SQL Server; otherwise, you can use the DESC command as follows:
FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
---|---|---|---|---|---|
ID | Int(11) | NO | PRI | ||
NAME | Varchar(20) | NO | |||
AGE | Int(11) | NO | |||
ADDRESS | Varchar(25) | YES | NULL |
You now have the STUDENTS table in your database, which you can use to record necessary student information.
The create table command can be used to duplicate or create new table from an existing table. The new table inherits the old table's column signature. We have the option of selecting all columns or a subset of them.
If we create a new table from an existing table, the new table will be filled with the old table's existing values.
The following SQL creates a copy of the employee table.