Mysql 表的操作

表操作的基本语法

# 创建表语法:
create table 表名(
    字段名1 类型[(宽度) 约束条件],
    字段名2 类型[(宽度) 约束条件],
    字段名3 类型[(宽度) 约束条件]
);

# 删除表语法
drop table 表名;

# 修改表名   
alter table 表名 rename 新表名;

# 增加字段   
alter table 表名 add 字段名  数据类型 [完整性约束条件…]
alter table 表名 add 字段名  数据类型 [完整性约束条件…]  first              # 字段显示第一位
alter table 表名 add 字段名  数据类型 [完整性约束条件…]  after 字段名        # 插入到某个字段后
                            
# 删除字段
alter table 表名 drop 字段名;

# 修改字段
alter table 表名 modify 字段名 数据类型 [完整性约束条件…]
alter table 表名 modify 字段名 数据类型 [完整性约束条件…] first
alter table 表名 modify 字段名 数据类型 [完整性约束条件…] after 字段名

alter table 表名 change 旧字段名 新字段名 数据类型 [完整性约束条件…]
alter table 表名 change 旧字段名 新字段名 数据类型 [完整性约束条件…] first;
alter table 表名 change 旧字段名 新字段名 数据类型 [完整性约束条件…] after 某字段;

# 查看所有的表名字
mysql> show tables; 

# 查看表结构
mysql> desc 表名;
mysql> describe 表名;
mysql> show create table 表名\G;
View Code

完整的创建表的语法  Navicat 导出的表sql 分析

set names utf8;
set foreign_key_checks = 0;                 # 禁用外键

drop table if exists employee;              # 如果存在 删除表
create table employee (
    id int(11) unsigned not null auto_increment,
    name varchar(20) character set utf8 collate utf8_general_ci null default NULL,
    sex enum('male','female') character set utf8 collate utf8_general_ci not null default 'male',
    age tinyint(2) unsigned null default NULL,
    dep_id int(11) unsigned null default NULL,
    primary key (id) using BTREE,
    index depids(dep_id) using BTREE,
    constraint depids foreign key (dep_id) references department (id)
    on delete cascade
    on update cascade
) engine = InnoDB auto_increment = 7 character set = utf8 collate = utf8_general_ci row_format = Dynamic;

set foreign_key_checks = 1;                # 启用外建

# unsigned        无符号
# character set   设置字符集
# collate         数据库校对规则。ci是case insensitive的缩写,意思是大小写不敏感;相对的是cs,即case sensitive,大小写敏感;
# using BTREE     BTREE 和 HASH
View Code

Mysql的row_format

在mysql中, 若一张表里面不存在varchar、text以及其变形、blob以及其变形的字段的话,那么张这个表其实也叫静态表,即该表的row_format是 fixed,就是说每条记录所占用的字节一样。其优点读取快,缺点浪费额外一部分空间。

若一张表里面存在varchar、text以及其变形、blob以及其变形的字段的话,那么张这个表其实也叫动态表,即该表的row_format是 dynamic,就是说每条记录所占用的字节是动态的。其优点节省空间,缺点增加读取的时间开销。

静态表,查询速度快,空间利用率不高 
动态表,查询速度慢,空间利用率较高 
所以,做搜索查询量大的表一般都以空间来换取时间,设计成静态表。

posted @ 2019-04-25 17:33  __Invoker  阅读(162)  评论(0编辑  收藏  举报