SQL(结构化查询语言)

DDL:数据定义语言

  • 添加一行:alter table apple add red varchar[50];
  • 修改名:  rename table apple To banana
  • 修改名:alter table apple change red green int ;
  • 修改一个表的字段类型:alter table apple modify red varchar(50);
  • 查看表的字段信息:desc apple;
  • 删除一列:alter table apple drop red;

DML:数据操作语言

  • 查看表:show tables;
  • 查询表中的所有数据:select * from Apple ;
  • 插入操作:insert into apple(red,green) value(nice,bad);
  • 更新操作:update apple set red=5;      限定条件:update apple set red=10 where green=5;
  • 删除操作:delete from apple ;(删除表中的数据,表结构还在)    truncate table apple;(把表直接drop掉,然后再创建一个同样的表)

DQL:数据查询语言

模糊查询:

_ :任意一个字符

% :任意0~n个字符

select * from apple red like'_i%'    找到nice

 复数据;select distinct red from apple;

把查询字段的结果进行运算,必须都要是数值型:  select * ,red+green as total from apple;

排序:  升序asc    select * from apple order by price asc ;

     降序desc     select * from apple order by price desc ;

COUNT():统计指定列不为NULL的记录行数;    select count(red)ras total from apple ;

寻找最大,最小,和,平均      select sum(price) as total from apple   sum可换为(max,min,avg)

分组:select id,GROUP_CONCAT(name) from stu group by id;        相同的数据化为一组      

select red,group_concat(green),sum(price) from apple group by red having red>1;      having 分组的限定条件  在分组后对数据进行过滤

SELECT id,GROUP_CONCAT(nas),SUM(nas) from stu GROUP BY id having SUM(nas)>311 ;

roup_concat(字段名)可以作为一个输出字段来使用:表示分组之后,根据分组结果,使用group_concat()来放置每一组某字段的值的集合

限制:select id from stu limit 0,5;      从第0行开始查,一共要查5行

 

数据的完整性:

  • 主键约束:primary key  
  • 唯一约束:unique
  • 自动增长列:auto_increment
  • 非空约束:not null
  • 默认值约束:default '男'

 

设置外键:constraint 取得名字(可以不写) foregin key(sid) references student(id);

多对多:创建学生表,创建老师表,创建老师和学生的关联表,添加外键。

添加外键:alter table tea_stu_rel add constraint foregin key(tid) reference teacher(tid);

        alter table tea_stu_rel add constraint foregin key(sid) reference key(sid);

拆分表:避免大量你冗余数据的出现

 

 

多表查询:

  • select * from stu,score where stu.id=score.id;
  • select * from stu join score in stu.id=score.id;
  • select * from stu natural join score;        2张表中列名称和类型完全一致

 

事务:  

  • 开启start transaction  提交:commit  回滚:rollback
  • 重复读:事务开启,不允许其他事务的update修改操作——repeadtable read(默认)   可解决不可重复读(read commited)
  • 脏读:事务还没有提交就可以查看到数据  (read uncommitted)  解决(read committed)
  • 幻读:事务开启过程insert一条记录  解决(serializable)  缺点:比较耗数据库性能。
  • 等级  read uncommitted<read committed<repeadtable read<serializable
  • 查看隔离级别:select @@global.tx_isolation,@@tx_isolation;  
  • 设置隔离级别:全局的    set global transaction isolation level read committed; 

 权限:

grant all privileges on 数据库名.表明(所有:*.*) To 用户名 identify by '密码'(with grant option——给与其给别人权限的权限);

 

子查询:

select name,deparment from emp where deparment = (select deparment from emp where ename = ‘项羽')<--20;

select name,salary from (select name,salary,deparment from emp where deparment = 30)s where s.salary >2000;

自连接:  自己链接自己,起别名

select e1.empno,e1.name,e2.empno,e2.name from emp e1 ,emp e2 where e1.mgr=e2.empno and e1.enpo=7369;

视图:

  • 创建视图:create view name as();
  • 修改视图:create or replace view name as ();
  • 删除视图:drop view name ;
  • algorithm:替代式:merge  具化式:temptable  (更安全)
  • with check option 更新数据时进行检测
  • create algorithm view name as (conditon)  with check option;
  • 视图不可更新部分:不是来自于基表

 

DELIMITER $$:DELIMITER语句将标准分隔符 - 分号(;)更改为:$$

存储过程:

  • create procedure name()
  • begin
  • 语句
  • end$$
  • delimiter ;
  • call name();

查看指定数据库中的存储过程:SHOW PROCEDURE STATUS WHERE db = 'My_test4';

查看指定存储过程源代码:SHOW CREATE PROCEDURE 存储过程名

删除存储过程:drop procedure  名称

while i<n do 

 

end while;

 

琢磨了一晚上,写了个函数,给他命名为rand(i int)  写好运行后发现怎么改都不是自己想要的结果,然后又新建了个程序在敲了一遍,名字改成abondom()就可以运行了,所以,给函数起名字

不要起有含义的!!!

 

 

btree:

 

 

随机生成指定字符串:

delimiter $$
create function abandoms(in n int,out res varchar(255))
BEGIN
declare i int default 0 ;
declare str varchar(100) DEFAULT 'qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM';
declare str_f varchar(255) default '';
while i<n DO
set str_f=concat(str_f,substring(str,floor(1+RAND()*52),1));
set i = i + 1;
end WHILE;
-- return str_f;
END $$
delimiter ;

指定插入数据到emp表中:

delimiter $$
create procedure insert_table(in startNUM int ,in max_num int)
begin 
declare i int default 0;
set autocommit = 0;
repeat 
set i = i + 1;
insert into emp values(startNUM+i,abandom(5),floor(10+rand()*30));
until i = max_num
end repeat;
commit;
END$$
delimiter ;

 给个名字查相关信息:

delimiter $$
create procedure getStu(in n varchar(50))
begin
select * from where name = n ;
end$$
delimiter ;

查询输出信息:

delimiter $$
create procedure getName(in i int,out n varchar(50))
begin
select name into n from student where id = i ;
end$$
delimiter ;

//      call getName(7,@res);
       select @res;

 

 

 

posted on 2019-06-09 17:07  、Discipline  阅读(3472)  评论(0编辑  收藏  举报

导航