SQL CREATE TABLE in Database
This SQL statement is used to create a new table in that specified database. CREATE TABLE statement defines the table name and required columns name and its data type.
Syntax
CREATE TABLE Table-name( Column-name1 data-type, Column-name2 data-type, Column-name3 data-type.... Column-nameN data-type, PRIMARY KEY (ONE OR MORE COLUMN) );
Where
- CREATE TABLE --> is the SQL keyword that indicates the RDBMS to create a new table.
- Table-name --> is the user-defined name or identifier that must be unique.
- Column-name1....Column-nameN --> They are the list of fields or columns of the table and specify what type of data is stored on each column. Also, you can set one column as a primary key.
For Example
Following is the SQl query to create [Student] tablemysql> CREATE TABLE Student(RollNo int,Name varchar(20),Age int); Query OK, 0 rows affected (1.53 sec)
mysql> SHOW tables;; +---------------------+ | Tables_in_collegedb | +---------------------+ | student | +---------------------+ 1 row in set (0.23 sec)
To show the table strucure by using DESC or DESCRIBE command
mysql> DESC Student; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | RollNo | int | YES | | NULL | | | Name | varchar(20) | YES | | NULL | | | Age | int | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.22 sec)