数据库相关
查询所有数据库
show databases;
创建数据库
create database 数据库名;
创建数据库指定字符集
create database 数据库名 character set gbk/utf8;
查看数据库详情
show create database 数据库名;
删除数据库
drop database 数据库名;
使用数据库
use 数据库名;
表相关
查看数据库中所有表
show tables;
创建表
create table 表名 (字段 1 名 字段类型, 字段 2 名 字段 2 类型, name varchar(10),age int);
ceate table person(name varchar(10),age int);
创建一个学生表 (student) 保留学号 id, 姓名 name, 年龄 age, 语文 Chinese, 数学 math, 英语培训 English
create table student(id int,name varchar(10),age int,chinese int,math int,english int);
创建表时指定表的引擎和字符集
create table 表名 (name varchar(10))engine=myisam/innodb charset=gbk/utf8;
表的引擎
innodb: 支持数据库的高级操作如: 外键, 事务等, 属于默认引擎
myisam: 只支持基础的增删改查操作;
查看表详情
show create table 表名;
sql 格式
1. 可以有换行.
2. 最后以; 分号结尾
3. 关键字之间需要有空格 (可以写多个空格, 建议写一个)
查看表所有字段
desc 表名;
删除表
drop table 表名;
修改表相关
修改表名
rename table 原名 to 新名;
修改表的引擎和字符集
alter table 表名 engine=myisam/innodb charset=utf8/gbk;
添加表字段
最后面: alter table 表名 add 字段名 字段类型;
最前面: alter table 表名 add 字段名 字段类型 first(第一);
某一个后面: alter table 表名 add 字段名 字段类型 after 指点位置的字段;
删除表字段 (一次只能删除一个)
alter table 表名 drop 字段名;
修改字段名和类型
alter table 表名 change 原字段名 新字段名 新字段类型;
修改字段类型和位置
alter table 表名 modify 字段名 类型 位置;
alter table hero modify age int first(after xxx);
来源: http://www.bubuko.com/infodetail-2898941.html