约束条件
null 空;
not null 非空;
default 默认插入值;
整型: zerofill (用0填充)
unsigned(无符号)
auto_increment(自增)
枚举型:enum
主键 primary key;
unique 唯一索引
创建表 create table 表名(
id int unsigned not null auto_increment primarykey,
name char(32) not null default"值"
unique,
sex enum('w','m') not null default"m",
age tinyint unsigned not null default"20",
class char(7) not null default"121"
)
h 可以请求帮助
c 清楚当前输入状态
q 退出数据库(quit exit)
登录: mysql -u root -p -b
创建数据库:create database 数据库名
创建数据库并指定字符集:create database 库名 default character set 字符集
查看数据库:show databases
查看当前数据库:show create databas
e 数据库名;
删除数据库:drop database 数据库名
进入数据库: use 数据库名
查看已经选择的数据库:select database();
导出数据库
mysqldump -u root -p 数据库名 >D:/名字.sql
导入数据库
mysql -u root -p 数据库名 <D:/名字.sql
导数据表同理
查看数据库中的表: show databases;
创建数据表(指定字符集):create table (
字段名1 类型 约束条件,
字段名2 类型 约束条件
............
)charset 字符集;
删除数据表:drop table 表名
查看建表语句:show create table 表名
查看表结构:desc 表名
插入数据
(1) insert into 表名(字段名1,字段名2,.....) values (值1,值2....)
(2)insert into 表名 values(值1,值2...)
(3)insert into 表名(字段名1,字段名2,.........)values(值1,值2..),(值1,值2...)
(4)insert into 表名(字段名1,字段名2,.....)select 字段名1,字段名2......from 表名
(5)insert into 表名 set 字段名1=值1,字段名2=值2.........
删除数据
delete from 表名 where 条件
查询语句
(1)select 字段名1,字段名2,.....from 表名
(2)select 字段名1,字段名2,......from 表名 where 字段名=值
(3) select * from 表名;
(4)select * from 表名 where 字段名 in('','','','','','');
(5)select * from 表名 where 字段名 like "%值%";(like not like)
(6)select * from 表名 where 字段名 like "_值";
(7)select * from 表名 where 字段名 between 值1 and 值2;
not null 检查非空值 null 检查空值
修改表结构
修改字段名
alter table 表名 change 原表名 新表名 字段类型 约束条件
修改字段类型
alter table 表名 modify 字段名 字段类型 约束条件
添加字段
alter table 表名 add 字段名 字段类型 约束条件 ;(first after字段名)
删除字段
alter table 表名 drop 字段名;
改表名
alter table 旧表名 rename as 新表名
索引
创建索引
create table 表名(
字段名 类型 约束条件,
。。。。。。。
index 索引名(要加索引的字段)
)
删除索引
drop index 索引名 on 表名
添加索引
create unique index 索引名 on 表名(要加索引的字段)
创建唯一索引
create table 表名(
字段名 类型 约束条件,
.....................................
unique 索引名(要加索引的字段)
)
删除唯一索引
drop index 索引名 on 表名
添加唯一索引
create unique index 索引名 on 表名(要加索引字段)
主键索引
create table 表名 (
字段名 类型 约束条件 primary key,
................................................
)
删除主键索引
alter table 表名 drop primary key;(有自增先删自增)
count() 数量
min()最小值
max()最大值
sum()和
avg()平均数
limit n,m 限制结果集 放最后
group by 分组 having 条件
order by 排序 [desc|asc]
select 字段名 ===》from 表名 ====》 where 条件 ===》group by
having ===》 order by ==》 limit
完全限定表名
select 表名.字段名 from 库名.表名
拼接字段
concat();
mysql> select concat(id,'-',name,'-',gid,'-',money) from xiangqin;
别名用as 或空格
select 字段名 as 别名 from 表名 as 别名
更新数据:
update 表名 set 字段名1=值,字段名2=值 where .....
删除数据:
delete from 表名 where 条件 (一定要加条件!!)
truncate table 表名 清空所有数据
更改密码
mysqladmin -u root -p password 新密码