mysql> drop database if exists myschool;
mysql> create database if not exists myschool;
mysql> use myschool;
Database changed
mysql> drop table if exists tb_testinfo;
#create table 表名 (字段名 类型 长度 <约束,默认值,注释>);
mysql>
create table tb_testinfo
(
test_id integer(10),
test_name varchar(30)
);
Query OK, 0 rows affected, 1 warning (0.40 sec)
约束类型 | 关键字 |
---|---|
非空 | NOT NULL ,指定列在插入数据时必须有值。 |
非负 | UNSIGNED , 插入的数字不能是负数 |
主键 | PRIMARY KEY , 列的值不能有重复值 |
自增 | AUTO INCREMENT , 只应用于整型的主键列 |
默认 | DEFAULT , 指定列的默认值 |
注释 | COMMENT , 说明字段 |
drop table if exists tb_testinfo;
create table tb_testinfo
(
test_id integer(10) auto_increment primary key comment '主键id',
test_name varchar(30) not null comment '名称',
test_pwd varchar(20) not null default '888888' comment '密码'
);
类型 | 关键字 |
---|---|
极小整型 | TINYINT 有符号时( -128 ,127);无符号时,最大255 ,1个字节 |
小整型 | SAMLLINT 非负最大65535 ,2个字节 |
整型 | INT , 非负最大值4294967295,4个字节 |
长整型 | LONG |
单精度 | FLOAT 4字节; |
小数 | decimal(6,3) 可以指定小数位数(总长6位,保留3位小数) |
定长字符串 | CHAR ,最大保存255个字节,如果值没有到给定长度用空格补充 |
变长字符串 | VARCHAR 最大保存255个字节,用多大占多大 |
文本 | text 最大保存65535 个字节 |
日期类型 | Date DateTime Timestamp |
以utf8编码 | 能存储汉字数量 |
---|---|
LANGTEXT | 4294967295/3=1431655765个汉字,14亿,存储空间占用:4294967295/1024/1024/1024=4G的数据; |
MEDIUMTEXT | 16777215/3=5592405个汉字,560万,存储空间占用:16777215/1024/1024=16M的数据; |
TEXT | 65535/3=21845个汉字,约20000,存储空间占用:65535/1024=64K的数据; |
类型使用样例:
create table tb_user
(
user_id int auto_increment primary key comment '用户编号',
user_name varchar(30) not null ,
user_birthday date,
user_gender char(3),
user_state tinyint(1) not null,
user_height decimal(4,1) not null,
user_desc text
);
表字段的增删改查
操作 | 语法 |
---|---|
字段添加 | alter table tb_name add 添加字段 字段类型 非空约束 默认 注释;alter table tb_name add address varchar(100) not null default '' comment '地址'; |
字段类型修改 | alter table tb_name MODIFY 字段名称 新字段类型 非负 非空 默认 注释;alter table tb_name MODIFY address varchar(50) not null default '' comment '地址'; |
字段名称类型修改 | alter table tb_name change 旧字段名称 新字段名称 新字段类型 约束 默认 注释;alter table tb_name change address addr varchar(100) not null default '' comment '地址'; |
字段类型查询 | desc tb_name; |
字段删除 | alter table tb_name drop addr; alter table 表名 drop 要删除的字段名 |
表修改
表修改 | 语法 |
---|---|
表名修改 | alter table tb_name rename to new_tb_name; |
引擎修改 | alter table tb_name engine=InnoDB; |
案例:
#新增字段
mysql> alter table tb_user add address varchar(100) not null default '' comment '地址';
#修改字段类型
mysql> alter table tb_user MODIFY address varchar(50) not null default '' comment '地址';
mysql> alter table tb_user change address addr varchar(100) not null default '' comment '地址';
#字段查看
mysql> desc tb_user;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int | NO | PRI | NULL | auto_increment |
| user_name | varchar(30) | NO | | NULL | |
| user_birthday | date | YES | | NULL | |
| user_gender | char(3) | YES | | NULL | |
| user_state | tinyint(1) | NO | | NULL | |
| user_height | decimal(4,1) | NO | | NULL | |
| user_desc | text | YES | | NULL | |
| addr | varchar(100) | NO | | | |
+---------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
#删除字段
mysql> alter table tb_user drop addr;
Query OK, 0 rows affected (0.32 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_user;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int | NO | PRI | NULL | auto_increment |
| user_name | varchar(30) | NO | | NULL | |
| user_birthday | date | YES | | NULL | |
| user_gender | char(3) | YES | | NULL | |
| user_state | tinyint(1) | NO | | NULL | |
| user_height | decimal(4,1) | NO | | NULL | |
| user_desc | text | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
#修改表名
mysql> alter table tb_user rename to t_user;
#修改表引擎(show create table t_user; 查看建表语句)
mysql> alter table t_user engine=myisam;
删除表 drop table [if exists] tb_name;
查询表 show tables;
查询建表语句 : show create table tb_name;