创建数据库
Create database 数据库名字 [库选项];
创建数据库
create database mydatas charset utf8;
查看数据库
show databases;
若是需要中文
set names gbk;
查看数据库
show databases like 'my%'; -- 查看以 my 开始的数据库
查看数据库创建语句
show create database mydatas;
删除数据库
drop dababase mydatas;
使用数据库
use mydatas;
创建表
创建表
指定数据库创建表
- create table if not exists mydatas.table1(
- id int not null primary key,
- name varchar(20) not null
- )charset utf8;
使用数据库 use mydatas 之后
- create table if not exists tabke2(
- id int not null primary key auto_increment,
- age int not null
- )charset utf8;
查看所有表
show tables;
查看表的创建结构
show create table 表名;
show create table table1;
查看表 以 ta 开始
show tables like 'ta%';
查看表结构
desc 表名
desc table1;
describe 表名
describe table1;
show columns from 表名
show columns from table1;
重命名表
rename table tabke2 to table2;
修改表选项 - 字符集
alter table table1 charset = GBK;
给 table1 增加一个 uid 放在第一个位置
alter table table1 add column uid int first;
增加一个字段 放到具体的字段之后
alter table table1 add column number char(11) after id;
修改字段的属性并放在一个字段之后
alter table table1 modify number int after uid;
修改表字段名称
alter table 表名 change 需要修改的字段名 新的字段名 字段类型 (必须存在);
alter table table1 change uid uuid varchar(50);
删除表中字段
alter table 表名 drop 字段名;
alter table table1 drop uuid;
删除数据表
dtop table 表名;
drop table table2;
插入数据
insert into 表名 (字段列表) values(对应的字段列表值);
-- 省略自增长的 id
insert into t2(name,age) values('nordon',22),('wy',21);
-- 使用 default,null 进行传递字段列表值, id 会自动增加
insert into t2 values(null,'张三',22),(default,'李欧尚',21);
查看数据
-- select 字段名 from 表名 where 条件;
select * from t2 where id = 1;
更新数据
-- update 表名 set 字段 = 新值 where 条件
update t2 set name = '王耀' where id = 2;
删除数据
-- delete from 表名 whrer 条件
delete from t2 where id = 5;
小知识
查看所有的字符集
show character set;
查看服务器默认的对外处理的字符集
show variables like 'character_set%';
-- 修改服务器认为的客户端数据的字符集为 GBK
set character_set_client = gbk;
-- 修改服务器给定数据的字符集为 GBK
set character_set_results = gbk;
-- 快捷设置字符集
set names gbk;
校对集
-- 查看所有校对集
show collation;
-- 创建表使用不同的校对集
- create table my_collate_bin(
- name char(1)
- )charset utf8 collate utf8_bin;
- create table my_collate_ci(
- name char(1)
)charset utf8 collate utf8_general_ci;
-- 插入数据
- insert into my_collate_bin values('a'),('A'),('B'),('b');
- insert into my_collate_ci values('a'),('A'),('B'),('b');
-- 排序查找
- select * from my_collate_bin order by name;
- select * from my_collate_ci order by name;
-- 有数据后修改校对集
- alter table my_collate_ci collate = utf8_bin;
- alter table my_collate_ci collate = utf8_general_ci;
来源: http://www.bubuko.com/infodetail-2586547.html