数据库操作
create database yanliang; 创建数据库
drop database yanliang; 删除数据库
show databases; 显示所有数据库
use yanliang; 选中yanliang数据库进行操作
表操作(前提:use选中数据库)
建表规则
create table 表名(属性名 数据类型 [完整性约束条件],
……
属性名 数据类型 [完整性约束条件] 注意:最后一行没有逗号
)
外键规则
constraint 外键别名 foreign key(属性A)
references 主键表(属性a) 注意:属性a为主键表的主键列
建表实例
create table zhangaonan (stu_id int primary key auto_increment,
stu_name varchar(20) not null unique,
stu_sex char(2) default ‘女’,
class_id char(10),
grad_id char(10),
constraint c_fk1 foreign key(class_id)
references stu_class(class_id),
constraint c_fk2 foreign key(grad_id)
references stu_grad (grad_id)
);
zhangaonan
列名 数据类型 约束
stu_id int 主键 自动递增
stu_name varchar(20) 非空 唯一约束
stu_sex char(2) 默认约束——‘女’
class_id char(10) 外键约束stu_class(class_id)
grad_id char(10) 外键约束stu_grad (grad_id)
stu_class
列名 数据类型 约束
class_id char(10) 主键
stu_grad
列名 数据类型 约束
grad_id char(10) 主键
查看表结构
desc 表名; 查看简略表结构
show create table 表名; 查看详细表结构
desc yanliang;
show create table yanliang;
修改表结构
修改表名
alter table 旧表名 rename 新表名;
alter table stu_class rename stu_newclass;
修改数据类型
alter table 表名 modify 属性名 数据类型;
alter table stu_class modify class_id varchar(30);
修改字段名和字段类型(不能有外键关系)
alter table 表名 change 旧属性名 新属性名 新数据类型
alter table zhangaonan change stu_name name varchar(30);
增加无约束字段
alter table 表名 add 字段名 数据类型
alter table stu_class add class_name varchar(20);
增加约束字段
alter table 表名 add 字段名 数据类型 完整性约束条件
alter table stu_class add class_ok varchar(20) not null;
删除字段
alter table 表名 drop 字段名;
alter table stu_class drop class_ok;
删除外键约束
alter table 表名drop foreign key 外键别名
alter table zhangaonan drop foreign key c_fk1;
删除表
drop table 表名
drop table yanliang;
索引
Create table 表名 (属性名 数据类型 [完整约束条件],
…...
[UNIQUE | FULLTEX | SPATIAL] INDEX
[别名] (属性名 [长度] [desc|asc])
);
--UNIQUE | FULLTEX | SPATIAL为唯一性、全文、空间索引,desc降,asc升序
建表时创建索引
普通索引
create table index1 ( id int,
name varchar(20),
sex Boolean,
index (id)
);
唯一索引
create table index2(id int unique,
name varchar(20),
unique index index2_id (id asc)
);
全文索引(存储引擎必须为MyISAM)
create table index3(id int,
info varchar(20),
fulltext index index3_info (info)
)engine=myisam;
单列索引
create table index4(id int,
subject varchar(20),
index index4_st (subject)
);
多列索引
create table index5(id int,
name varchar(20),
sex char(4),
index index5_st (name , sex)
);
空间索引(存储引擎必须为MyISAM,不推荐)
create table index6(id int,
space geometry not null,
spatial index index6_sp(space),
) engine=myisam;
在已存在的表上创建索引
create [UNIQUE | FULLTEX | SPATIAL] INDEX 索引名 on 表名 (属性名 [长度] [desc|asc]);
--UNIQUE | FULLTEX | SPATIAL为唯一性、全文、空间索引,desc降,asc升序
普通索引
create index index7_id on example0 (id);
--在example0上id字段建立名为index7_id索引
唯一索引
create unique index index8_id on index8(id);
--在index8上id字段建立名为index8_id索引
全文索引
create fulltext index index9_info on index9(info);
--在index9上info字段建立名为index9_info索引
单列索引
create index index10_info on index10(info);
--在index10上info字段建立名为index10_info索引
多列索引
create index index11_na on index11(name,address);
--在index11上name,address字段建名为index11_na索引查询时要包含name|address时用
空间索引
create spatial index index12_line on index12(line);
--在index12上line字段建立名为index12_line索引,存储引擎必须是myisam, line必须是空间数据类型
删除索引
drop index 索引名 on 表名;
drop index index2_id on index2;
视图
create [ algorithm = { undefined | merge | temptable } ]
view 视图名 [( 属性清单 )]
as select语句
[ with [ cascaded | local ] check option ];
创建视图
单表上创建视图
create view department_view1
as select * from department;
--在department表上创建一个名为department_view1的简单视图
多表上创建视图
create algorithm = merge view
worker_view2 (name,department,sex,age,address)
as select name,department.d_name,sex,birthday,address
from worker,department where worker.d_id=deparment.d_id
with local check option;
--在worker、deparment表上创建一个名为worker_view2的多表视图
删除视图
drop view [if exists] 要删除的视图名列表
drop view if exists department-view1, department-view2;
触发器
创建一个执行语句的触发器
create trigger 触发器名 before | after 触发事件
on 表名 for each row 执行语句
--before在触发事件之前执行触发语句;after在触发事件之后执行触发语句
实例:
create trigger ins_trigger before insert on zhangaonan for each row
update count set sum=sum+1;
创建多个执行语句的触发器
create trigger 触发器名 before | after 触发事件
on 表名 for each row
begin
执行语句列表
end
--before在触发事件之前执行触发语句;after在触发事件之后执行触发语句
实例:
create trigger ins_trigger1 before update
on zhangaonan for each row
begin
update count set sum=sum+1;
update count1 set sum1=sum1+1;
end
删除触发器
drop trigger 触发器名
drop trigger ins_trigger1;
SQL查询语句
插入、更新与删除数据
为表的指定字段插入数据
insert into 表名(属性1,属性2,属性3,……,属性n)
values(值1,值2,值3,……,值n);
insert into zhangaonan (stu_name,stu_sex) values('qs','男');
同时插入多条记录
insert into 表名 [(属性列表)]
values(取值列表1),(取值列表2),,
……,
(取值列表n);
insert into zhangaonan (stu_name,stu_sex)
values('开始v','男'), ('是否','男'), ('是大V','男'), ('看电视呢','男');
将查询结果插入到表中
insert into 表名1(属性列表1)
select 属性列表2 from 表名2 [where 条件表达式];
insert into zhangaonan (stu_name)
select name from peo;
更新数据
update 表名
set 属性名1=取值1,属性名2=取值2,
……,
属性名n=取值n
where 条件表达式;
update zhangaonan
set stu_name=’闫亮’,stu_sex=’男’
where stu_id=1 and stu_id=2;
删除数据
delete from表名 [where 条件表达式];
delete from zhangaonan where stu_id=2;
Mysql函数
数学和日期函数
CURDATE()或CURRENT_DATE() 返回当前的日期
CURTIME()或CURRENT_TIME() 返回当前的时间
ABS(x) 返回x的绝对值存储过程
CEILING(x) 返回大于x的最小整数值
FLOOR(x) 返回小于x的最大整数值
SQRT(x) 返回一个数的平方根
MOD(x,y) 返回x/y的模(余数)
聚合函数(常用于GROUP BY从句的SELECT查询中)
AVG(col)返回指定列的平均值
COUNT(col)返回指定列中非NULL值的个数
MIN(col)返回指定列的最小值
MAX(col)返回指定列的最大值
SUM(col)返回指定列的所有值之和