您的位置:首页 > 数据库

sql基本语法笔记

2018-03-20 15:57 441 查看
创建库
  create database 库名;
  
查询数据库
  show databases;
  
进入库:use 库名;

删除数据库:drop database 库名;

创建数据表:
create table 表名(字段1 类型,字段2  类型);

删除表名:
drop table 表名;

修改表名:
alter table 旧名 rename 新名;

查询当前数据库所有表:show tables;

查表字段:desc 表名;

字段操作:
修改类型;
alter table 表名 modify 字段名 varchar(20);

增加字段:
alter table 表名 add 字段名 类型;()

设定id为主键,自动增长:
alter table 表名 modify column `id` int(11) not null AUTO_INCREMENT first ,add primary key (`id`);

删除字段:
alter table 表名 drop column 字段名;

修改字段名:
alter table 表名 change 原名 新名 类型;

1.增加
1-1 添加新用户:insert into sys_user(username,password,age...) values("张三",“123”,23,...);
insert into sys_user values(值1,值2,值3,...);

2.删除
2-1 根据用户id删除用户,delete from sys_user where user_id=22;

3.修改
3-1  修改用户密码 :update sys_user set password=【新密码】where user_id=22;
3-2  

4.查询

4-1根据当前用户id查询角色id :
select croup_concat(distinct role_id) as  roles from sys_role_user_rel where user_id=42;

4-2 根据用户所属组织id查询组织的角色id:
select croup_concat(distinct role_id) as  roles from sys_role_org_rel where org_id=42;

4-3 根据用户的角色id(个人的角色+组织角色),查出用户的菜单权限:
select * from sys_menu m,sys_role_menu_rel r where  r.menu_id=m.menu_id  and role_id  in(1,5);---1跟5是角色id;

4-4 查询当前人信息及当前人所在的组织:
select *【要展示的字段】 from sys_user u,sys_org o where u.org_id=o.org_id and u.user_id=15;

4-5 查询当前组织及父组织名称:
select *【要展示的字段】 from sys_org o,sys_org p where o.org_parent_id=p.org_id  and o.org_id=22【当前组织id】;

4-6 查询出当前菜单及当前菜单的父菜单名称:
select *【要展示的字段】from sys_menu m,sys_menu p  where m.menu_parent_id=p.menu_id;

4-7 判断用户账号跟密码,返回用户对象:
select id,username,password from sys_user where username=#{username} and password=#{password}

4-8 分页查询,每页10条:
select * from table limit (i-1)+10,10;

4-9 区间选择:select 字段 from 表 limit 偏移量,数量;

4-10 排序查询,
降序 :select 字段 from 表 order by 字段 desc limit 数量;
升序 :select 字段 from 表 order by 字段 asc limit 数量;

4-11 去重复查询:
select  distinct 字段 from 表;

4-12 id=10的条件查询:
select  字段 from 表 where id =10;

4-13 函数:
4-13-1:查询数量:select count(*) from 表;

4-13-2;求和:select sum(字段) from 表  ;

4-13-3;求平均数:select avg(字段) from 表  ;

4-13-4:求最大值:select max(字段)from 表;

4-14 模糊查找:
select * from 表名 where 字段 like '%要查的词%';

4-15 关联查找:
左连接:select * from 表名a a LEFT JOIN 表名b b ON a.bId=b.id ;
右连接:select * from 表名a a right join 表名b b ON a.bId=b.id ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql基本语句