Create table

This creates a database table with the specified name. Here is its syntax:

CREATE TABLE table_name (column_name column_type,column_name column_type,.....);

Here:

The create table statement creates a users table with three columns: email_address, password, and address_of_delivery. Assuming that this table will contain information of the users who have placed orders online, we will be storing their email address, password, and the location where the order has to be delivered:

MySQL [ecommerce]> create table users(email_address varchar(30), password varchar(30), address_of_delivery text);
Query OK, 0 rows affected (0.38 sec)

To confirm that the table has been successfully created, we will use the show tables command to display the list of existing tables in the currently opened database, as shown here:

MySQL [ecommerce]> show tables;
+---------------------+
| Tables_in_ecommerce |
+---------------------+
| users |
+---------------------+
1 row in set (0.00 sec)

The output of the show tables command displays the users table, thus confirming that the table has indeed been created successfully. To see the table structure (that is, its column names, column types, and column width), we will use the describe statement. The following statement displays the structure of the users table:

MySQL [ecommerce]> describe users;
+---------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+-------------+------+-----+---------+-------+
| email_address | varchar(30) | YES | | NULL | |
| password | varchar(30) | YES | | NULL | |
| address_of_delivery | text | YES | | NULL | |
+---------------------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)

So, now that we have learned about some basic commands to work with our database, we can begin with the first recipe of this chapter.