Mysql基础2.0

1增加字段
(1)增加一个字段
alter table 表名 add 新字段名 类型(长度)
ps:给s表增加一个字段:class_no char(6)
alter table s add class_no char(6)
(2)增加多个字段
alter table 表名 add (新字段名1 类型(长度),新字段名2 类型(长度),新字段名2 类型(长度))
alter table s add (class_no1 char(6),class_no2 char(6))


2删除字段
(1)删除一个字段  
alter table 表名 drop 字段名 
ps: alter table s drop class_no
(2)删除多个字段
alter table 表名 drop 字段名1,drop 字段名2
ps: alter table s drop class_no1,drop class_no2


3修改字段(修改字段宽度)
alter table 表名 modify column 字段名 类型(修改后的宽度)
ps: alter table s modify column sno char(8)

4.增加主关键字
alter table 表名 add primary key(字段名)
alter table 表名 add constraint 约束名 primary key(字段名)
ps: alter table t1 add primary key(sno)

5.删除主关键字
alter table 表名 drop primary key
ps: alter table t1 drop primary key

6.增加外键约束
alter table 表名 
add constraint 约束名1 foreign key(外键字段名) references 主表1(主键),
add constraint 约束名2 foreign key(外键字段名) references 主表2(主键),

ps:alter table sc1 
add constraint sc1_fore1 foreign key(sno) references s(sno),
add constraint sc1_fore2 foreign key(cno) references c(cno)


7.删除外键约束
(1)先删除外键
alter table 表名 drop foreign key 外键名
ps: alter table sc1 drop  foreign key sc1_fore1,drop foreign key sc1_fore2


(2)再删除索引
alter table 表名 drop index 外键名
ps: alter table cs1 drop index sc1_fore2


8.删除表
drop table 表名
ps: drop table s


*1插入记录
(1)插入一条完整记录
insert into 表名(字段名1.字段名2.....) values(值1,值2,值3......)
//字段名1,字段名2可省略
ps: insert into s values(‘st1’,‘张三1’‘女’,21,‘计算机’)
(2)插入一条部分字段的记录
insert into 表名(字段名1,字段名2,...) values(值1,值2,值3)
//字段名不能省略


插入数据
insert into 表名(字段名1.字段名2) values(值1,值2...)
(1)插入一条完整的记录(包括所有字段)
insert into 表名 values(值1,值2)
insert into s values()
(2插入部分字段)
insert into 表名(字段名) values (值)


修改数据 update set
update 表名 set 字段名=值,字段名=值 where 查询条件


删除记录 delete from
delete from 表名  删除所有表中数据
delete from 表名 where 条件


order by asc/desc limit  /*limit限制*/

select distinct sno from sc  distinct 将结果中的重复行去掉

加别名
select sno as 学号, sname as 姓名 from s as 学生表

%任意0个或多个字符
_任意一个字符
select * from s where sname like'张%'  /*通配符*/


select
from 表1,表2
where 连接条件and查询条件

ps:select..
from s,sc
where s.sno=sc.sno and sage>20


select..
from 表1 join 表2 on 连接条件
where 查询条件
/*多表查询*/
select..
from 表1 join 表2 on 连接条件
         join 表3 on 连接条件
where 查询条件

ps:select c.cno
from s join sc on s.sno=sc.sno
       join c on c.cno=sc.cno 

往表中插入查询结果
insert 表
新建一个表插入查询结果
create table 表


创建表内索引
create [unque] index 索引名 on 表名(字段 asc/desc)
=alter table 表名 add  [unque] index 索引名(字段)

创建表索引
create table s(
sno char(5) primary key,
unique index (字段名)
);

查看
show create table 表名 查看表结构
explain select from where  查看索引

删除索引
drop index 索引名 on 表名
=alter table 表名 drop index 索引名


创建视图
 create [or replace] view 视图名[(字段列表)] as select from where 条件 [with check option]
/*with check option针对满足条件的查询语句   字段列表即 设置列表别名*/

查看视图
show create vieew 视图名

查看视图结构
describe 视图名

修改视图
alter view 视图名[(字段列表)] as select from where 条件 [with check option]
/*with check option针对满足条件的查询语句   字段列表即 设置列表别名*/

update set
update 视图名 set 字段名=值,字段名=值 where 查询条件

删除视图
drop  view 视图名


创建存储过程
create procedure 过程名 (参数定义)[存储过程选项]
begin 复合语句;end;
/*参数定义 in/out/inout*/

delimiter //
CREATE PROCEDURE p_em1(in dept VARCHAR(30),in ssex VARCHAR(3))
BEGIN
    SELECT *
    from t
    where dep=dept and sex=ssex;
end; //
delimiter ;

执行存储过程
1  call 过程名(参数)
call p_em1('计算机','男')

2  set @变量=...
   call 过程名(@变量,@变量)


set @xi='数学';
set @xb='男';
CALL p_em1(@xi,@xb);/*会话级变量*/


定义变量:declare 变量名 int类型;

db=‘college’ 查询位置在college数据库


创建触发器
create trigger 触发器名
after/before  insert/update/delete  on 表名
for each row 行级触发器 表明满足条件的每一行都要触发一次
begin 触发程序  end    /*触发程序在触发事件之后发生 选择after
既 触发程序先执行 选择 before*/


insert事件只有new对象
delete事件只有old对象
update事件有new 和 old

触发器抛出错误signal SQLSTATE 'HY000'/*signal SQLSTATE 'HY000' 
    SET MESSAGE_TEXT='文本';*/

insert例子:
drop trigger if EXISTS a;
CREATE TRIGGER a BEFORE INSERT on t
for EACH ROW
BEGIN
    if new.sal<0 and new.sal is not null THEN
    signal SQLSTATE 'HY000' 
    SET MESSAGE_TEXT='工资不能小于0,插入失败';
    END IF;
end;

同一字段含多个值的时候用,分开

匹配查询可以用FIND_IN_SET(str,strlist)

str 要查询的字符串

strlist 字段名 参数以”,”分隔 如 (1,2,6,8,10,22)

给date类型数据做修改:增加一天:DATE_ADD(date2,INTERVAL 1 day)

date_add({日期参数}, interval 1 day)

游标:1.声明 2.打开 3.操作 4.关闭

delimiter //
CREATE PROCEDURE p3()

BEGIN
    DECLARE id int DEFAULT 0;
    DECLARE med varchar(50)DEFAULT 0;
    DECLARE date1 date DEFAULT 0;
    DECLARE date2 date DEFAULT 0;
    DECLARE dose FLOAT DEFAULT 0; 
    DECLARE flag int DEFAULT 0;
    DECLARE xx CURSOR for select * from tbl;
    DECLARE CONTINUE HANDLER for not found set flag =1;
  open xx;
    repeat
        FETCH xx into id,med,date1,date2,dose;
        select id,med,date1,dose;
        while(date1<date2)do
        set date1 =date1+1;
        select id,med,date1,date2,dose;
        end while;
        
        until flag=1
    end repeat;
 close xx;
end//
delimiter
call p3();

select..from 表1 join 表2 on 连接条件where 查询条件/多表查询/select..from 表1 join 表2 on 连接条件 join 表3 on 连接条件where 查询条件

ps:select c.cnofrom s join sc on s.sno=sc.sno join c on c.cno=sc.cno

​​​​​​​

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值