[(create_definition,...)]
[table_options] [select_statement]
或:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(] LIKE old_tbl_name [)]
create_definition:
column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
| KEY [index_name] [index_type] (index_col_name,...)
| INDEX [index_name] [index_type] (index_col_name,...)
| [CONSTRAINT [symbol]] UNIQUE [INDEX]
[index_name] [index_type] (index_col_name,...)
| [FULLTEXT|SPATIAL] [INDEX] [index_name] (index_col_name,...)
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) [reference_definition]
| CHECK (expr)
column_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string'] [reference_definition]
type:
TINYINT[(length)] [UNSIGNED] [ZEROFILL]
| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
| MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
| INT[(length)] [UNSIGNED] [ZEROFILL]
| INTEGER[(length)] [UNSIGNED] [ZEROFILL]
| BIGINT[(length)] [UNSIGNED] [ZEROFILL]
| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
| NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
| DATE
| TIME
| TIMESTAMP
| DATETIME
| CHAR(length) [BINARY | ASCII | UNICODE]
| VARCHAR(length) [BINARY]
| TINYBLOB
| BLOB
| MEDIUMBLOB
| LONGBLOB
| TINYTEXT [BINARY]
| TEXT [BINARY]
| MEDIUMTEXT [BINARY]
| LONGTEXT [BINARY]
| ENUM(value1,value2,value3,...)
| SET(value1,value2,value3,...)
| spatial_type
index_col_name:
col_name [(length)] [ASC | DESC]
reference_definition:
REFERENCES tbl_name [(index_col_name,...)]
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
table_options: table_option [table_option] ...
table_option:
{ENGINE|TYPE} = engine_name
| AUTO_INCREMENT = value
| AVG_ROW_LENGTH = value
| [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name]
| CHECKSUM = {0 | 1}
| COMMENT = 'string'
| CONNECTION = 'connect_string'
| MAX_ROWS = value
| MIN_ROWS = value
| PACK_KEYS = {0 | 1 | DEFAULT}
| PASSWORD = 'string'
| DELAY_KEY_WRITE = {0 | 1}
| ROW_FORMAT = {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| UNION = (tbl_name[,tbl_name]...)
| INSERT_METHOD = { NO | FIRST | LAST }
| DATA DIRECTORY = 'absolute path to directory'
| INDEX DIRECTORY = 'absolute path to directory'
partition_options:
PARTITION BY
[LINEAR] HASH(expr)
| [LINEAR] KEY(column_list)
| RANGE(expr)
| LIST(column_list)
[PARTITIONS num]
[ SUBPARTITION BY
[LINEAR] HASH(expr)
| [LINEAR] KEY(column_list)
[SUBPARTITIONS(num)]
]
[(partition_definition), [(partition_definition)], ...]
partition_definition:
PARTITION partition_name
[VALUES {
LESS THAN (expr) | MAXVALUE
| IN (value_list) }]
[[STORAGE] ENGINE [=] engine-name]
[COMMENT [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] (tablespace_name)]
[NODEGROUP [=] node_group_id]
[(subpartition_definition), [(subpartition_definition)], ...]
subpartition_definition:
SUBPARTITION logical_name
[[STORAGE] ENGINE [=] engine-name]
[COMMENT [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] (tablespace_name)]
[NODEGROUP [=] node_group_id]
select_statement:
[IGNORE | REPLACE] [AS] SELECT ... (Some legal select statement)
建表过程就是一个画表头的过程(声明字段过程)存储同样的数据,不同的lie类型所占的空间和效率是不一样的,这就是我们建表前要列类型的意义
所以,列类型的存储与占据的字节关系是学习的重点
一般而言,设某类型N字节
N字节,8N位
0 -->2^8N-1
-2^(8N-1) --->2^(8N-1)-1
对于int类型:占的字节越多,存储的范围也越大
添加一个学分 列
alter table class add score tinyint unsigned not null default 0
(M)unsigned zerofill
zerofill: zero是零,fill是填充,代表0填充
M表示补0宽度,即位数不够,用0填充没有补齐的宽度,并不改变其大小
M必须和zerofill配合才有意义
用zerofill表示属性,则该属性也默认为unsigned类型
char、varchar分别称为定长、变长类型
char(100)是存储0~100个长度
varchar(100)也是存储0~100个长度
char为定长,当输入长度不足设定的长度时,会用空格去补,而取出来时,会将空格去掉,所以,用concat连接时,char的空格被自动删除了
【注】char(M),varchar(M)限制的是字符,不是字节
即char(2) charset utf8, 储存2 个utf8字符,比如中国
utf8一个字符占3个字节
一般用来存储文章内容、新闻内容等
声明text时,不必使用默认值
所以不必加上 not null default '' ,而是直接使用 artice text
用来存储图像、音频等二进制信息
意义:blob防止因为字符集的问题,导致信息丢失
比如一张图中有0xFF字节,这个在ASCII字符集默认 非法,在入库的时候,被过滤了
超出范围,year类型只占1字节,最多能存256种变化
范围:1901~2155
year能简写成两位,但不推荐这样写(但现在的版本不支持了)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)