Create a data table
It belongs to a data definition language DDL
Syntax:
create table [if not exists] `表名`(
'字段名1' 列类型 [属性][索引][注释],
'字段名2' 列类型 [属性][索引][注释],
#...
'字段名n' 列类型 [属性][索引][注释]
)[表类型][表字符集][注释];
Description: Anti-quotes are used (keyboard esc key below) the difference between MySQL reserved words and common character introduced.
And column data types
Column type: a predetermined type of data stored in the database of the column
-
Numeric types
-
String type
-
The date and time type
-
NULL value
- Understood as "no value" or "unknown value"
- Do not perform arithmetic operations with NULL, the result is still NULL
Data field properties
UnSigned :
- Unsigned integer
- Declare the data column does not allow negative.
ZEROFILL :
- 0 filled
- Insufficient number of bits are padded with zeros, such as int (3), 5, compared with 005
Auto_InCrement :
- Automatic increase, adding each piece of data, an automatic (default) on a number of records
- Is typically used to set the primary key index, you must be an integer type.
- Define the start value and the step size
- The current table setting step (AUTO_INCREMENT = 100): Table affects only the current
- SET @@ auto_increment_increment = 5; increment affect all tables (global)
NULL and NOT NULL:
- The default is NULL, i.e., no value is inserted into the column
- If set to NOT NULL, the column must have a value
DEFAULT :
- default
- For setting default values
- For example, the gender field, the default is "male", otherwise "female"; if no value is specified for the column, is "man" is the default value
/*创建数据表例子
1、创建一个名为School01的数据库。
1、创建一个学生信息表:学生id(自增,主键),姓名,年龄,性别,电话,籍贯,入学时间,所属班级id(外键)。
2、创建一个学生成绩表:成绩id(自增,主键),科目,成绩,学生id(外键),创建时间。
3、创建一个学生班级表:班级id(主键,自增),班级名称。
*/
#如果存在数据库School,则删除。否则创建数据库
DROP DATABASE IF EXISTS `School01`;
#创建数据库
CREATE DATABASE `School01`;
USE `School01`;
#如果存在数据表,则删除,否则创建
DROP TABLE IF EXISTS `tb_class`;
#创建一个学生班级表:班级id(主键,自增),班级名称。
CREATE TABLE `tb_class`
(
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Name` VARCHAR(32) NOT NULL
);
DROP TABLE IF EXISTS tb_student;
#创建一个学生信息表:学生id(自增,主键),姓名,年龄,性别,入学时间,所属班级id(外键)。
CREATE TABLE `tb_student`
(
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Name` VARCHAR(32) NOT NULL,
`Age` INT DEFAULT 0,CHECK(`Age`>0 AND `Age`<=100),
`gender` BOOLEAN DEFAULT 0,CHECK(`gender`=0 OR `gender`=1),
`date` DATETIME DEFAULT NOW()
);
#创建一个学生成绩表:成绩id(自增,主键),科目,成绩,学生id(外键),创建时间。
DROP TABLE IF EXISTS `tb_score`;
CREATE TABLE `tb_score`
(`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`course` VARCHAR(32) NOT NULL,
`Score` FLOAT(3,1) NOT NULL,
`stuId` INT(11) NOT NULL ,
CONSTRAINT `FK_Stuid` FOREIGN KEY(`stuId`) REFERENCES `tb_student`(`id`)
);
Result: The
type of MySQL data tables: MyISAM , InnoDB , HEAP, BOB, CSV, etc. ...
Common MyISAM and InnoDB type
name | MyISAM | InnoDB |
---|---|---|
Transaction Processing | not support | stand by |
Datarows | not support | stand by |
Foreign key constraint | not support | stand by |
Full-text index | stand by | not support |
Table space size | Small | Large, approximately 2-fold |
- Applicable MyISAM: the corresponding velocity and space-saving
- Applicable InnoDB: security, transaction processing and multi-user operating data table