mysql知识点大全(考前必备)

1.基本建库建表建视图语句
create database 数据库名;
create table 表名(字段列表,可加primary key(字段名),unique/index/fulltext 索引名(字段名),foreign key(子表字段名) references 父表名(字段名));
create view 视图名 as select 字段名 from 表名 where条件;
desc 表名/视图名;
show databases;
 

2.针对字段的语句
字段的更改
alter table 旧表名 rename [as/to] 新表名; 
rename table 旧表名1 to 新表名1, 旧表名2 to 新表名2;
alter table 表名 change sname 旧字段名 新字段名 类型;
alter table 表名 modify 字段名 类型 [after 字段名] [first];//添加/修改字段类型
alter table 表名 add 字段名;
alter table 表名 add primary key(字段列表);//复合字段构成主键
alter table 表名 drop 字段名 [primary key];
alter table 表名 drop foreign key 外键名;
alter table 表名 drop index 索引名;
alter table 表名 modify column 字段名 comment "新备注";
  
索引
create unique index si(索引名) on 表名(字段名 asc/desc(升/降序));//创建唯一索引
alter table 表名 add unique/index/fulltext 索引名(字段名);//index时字段可为多个

外键重点
可以在属性后直接加 ,字段名 类型 references 父表名(字段名)//非正规
create table 表名(字段列表,constraint 外键名 foreign key(子表字段名) references 父表表(字段名);
alter table 子表名 add foreign key(子表字段名) references 父表名(字段名);

3.记录的操作
插入
insert into 表名 values(字段值1),(字段值2).....;
insert into 表名(字段名) values(字段值1),(字段值2).....;
create table 表名 select*from 被复制表名;
修改
update 表名 set 字段名1=内容,字段名2=内容... where ;
删除
delete from 表名 where;
查询
select*from;
select 字段名 from 表名;
select 字段名 as 别名 from 表名;
select 字段名 from 表名 group by 另一个字段名;
count,max,min,avg
子查询重点
select 字段名 from 表名 where 字段名=(select  字段名 from 表名);
多表联合查询
select  a.字段名,b.字段名 from 表1 as a,表2 as b where a.字段名=b.字段名;
 


4.函数、过程、触发器
函数
delimiter $换结束符
drop function if exits 函数名$
create function 函数名(参数列表) returns 返回类型
begin
    函数体
end
return 返回类型;

存储过程
drop procedure if exists 过程名;
create procedure 过程名 ([in|out|inout] 参数列表)//in是传入的参数,out是返回的参数,格式(in x int,out y int)
begin
过程体;
end $

触发器
create trigger 触发器名字 触发时机(after/before) 触发事件(insert/delete/updata) 
on 表(非临时表/视图) for each row(行级触发器) 
begin
操作的内容;//注意new和old
end

注意:&& 也可以是and
if ...then... ;elseif ...;else ...;
end if;

label:loop
循环语句;
if 终止条件 then leave label;
end loop label;

repeat 
循环语句;
until 终止条件 end repeat;

while 循环条件 do
循环语句;
end while;

declare 局部变量名 double default 0;
set @用户变量名
select 字段名 into 局部变量名/@用户变量名 from 表名;

5.符号
%通配符 例如s%显示以s开头的所有
_任意一个字段
*全部
>,<,>=,<=,!=,&&,||
6.条件
日期where date(字段名) between '' and '';where year(字段名)=2020 and month(字段名)=2;
针对date:
weekday(日期字段名/日期)、quarter季度、dayname星期,monthname月份
区间where  and/or  ;where 字段名 like "";
范围where 字段名 [not] in ( , , );where 字段名 [not] between...and...;
为空where 字段名 is null;
指定顺序order by 字段名 desc/asc;
限制行数limit 起始行(省略就是从0开始),结束行 ;


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值