MySQL CREATE TABLE

CREATE TABLE SYNTAX:

Below is the basic syntax for creating a Table in MySQL.

CREATE TABLE EXAMPLE

To demonstrate CREATE TABLE example, let us first create a sample database MySQLTutorial and set it as current contextual database by executing the following script:

Let us now create a simple Customer table with three columns CustomerId, Name and PhoneNumber by executing the following script:


Here CustomerId and Name columns have NOT NULL constraint, which enforces that each row in this table needs to have a value for these two columns. Where as PhoneNumber column in this table is optional, it allows NULL values for this column. By default if we don’t specify the NOT NULL constraint for column during table creation, then it is considered as Null-able column.

INSERT DATA

We can add records to the Table by using INSERT statement as shown below Example:

RETRIEVE DATA

We can use the SELECT statement like below to fetch records from the Customer Table.

RESULT:
mysql-create-table

Leave a Reply

Your email address will not be published. Required fields are marked *