DDL(Data Definition Language)数据定义语言
一. 数据库操作
1.查询所有数据库:show databases;
2.查询当前数据库:select database();
3.创建:create database 【if not exists】 数据库名 【default charset 字符集】【collate 排序规则】;
4.删除:drop database【if exists】数据库名;
5.使用:use 数据库名;
二. 表操作
(一)查询
1.查询当前数据库所有表:show tables;
2.查询表结构:desc 表名;
3.查询指定表的建表语句:show create table 表名;
(二)创建
create table name( 字段 类型 );
(三)数据类型
1.tinyint :1 bytes (-128–127) (0–255) 小整数值;
2.smallint:2 bytes (-32768,32767) (0,65535) 大整数值;
3.mediumint:3 bytes (-83886608 8388607)大整数值;
4.int 或 integer:4 bytes;
5.bigint: 8 bytes 极大整数值;
6.float:4 bytes 单精度浮点数值;
7.double:8 bytes 双精度浮点数值;
8.decimal:小数值
字符串类型:
char:定长字符串;
varchar:变长字符串;
tinyblob:不超过255个字符的二进制数据;
tinytext:短文本字符串;
blob:二进制形式的长文本数据;
text:长文本数据;
mediumblob:二进制形式的长文本数据;
mediumtext:中等长度文本数据;
longblob:二进制形式的极大文本数据;
longtext:极大文本数据;
日期类型:
date:日期值 yyyy–mm–dd;
time:时间值或持续时间 hh:mm:ss;
year:年份值 yyyy;
datetime:混合日期和时间值 yyyy–mm–dd hh:mm:ss;
timestamp:混合日期和时间值,时间戳 yyyy–mm–dd hh:mm:ss
(三)修改
添加字段:
alter table 表名 add 字段名 类型[comment 注释] [约束]
alter table name change 旧字段名 新字段名 类型 同上;
alter table name drop 字段名;
alter table 表名 rename to 新表名;
(三)删除
删除表:drop table 【if exists】 表名;
删除指定表,并重新创建该表:truncate .table 表名;
删除表时,表中的全部数据也会被删除。