The following information is required to create an MySQL data table:
Table name
Table field name
Define each table field
Grammar ¶
The following is the general SQL syntax for creating MySQL data tables:
CREATE TABLE table_name (column_name column_type);
In the following example, we will create the data table runoob_tbl in the RUNOOB database:
CREATE TABLE IF NOT EXISTS `runoob_tbl`(
`runoob_id` INT UNSIGNED AUTO_INCREMENT,
`runoob_title` VARCHAR(100) NOT NULL,
`runoob_author` VARCHAR(40) NOT NULL,
`submission_date` DATE,
PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Instance resolution:
If you don’t want the field to be NULL You can set the property of the field to NOT NULL When manipulating the database, if the data entered in this field is NULL , you will report an error.
AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1。
The PRIMARY KEY keyword is used to define the column primary key. You can use multiple columns to define the primary key, separated by commas.
ENGINE sets the storage engine and CHARSET sets the encoding.