继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

MySQL语句详情整理大全【全面总结】

饮歌长啸
关注TA
已关注
手记 188
粉丝 46
获赞 152
一、用户管理

1、新建用户

mysql> create user name identified by 'root';
 

2、更改密码

mysql> set password for name=password('ok');
 

3、设置用户权限

MySQL> show grants for name; //查看name用户的权限

mysql> grant select on db_name.* to name; //给用户name的db_name数据库所有权限

mysql> revoke select on db_name.*to name; //与grant操作相反,去掉权限
 

二、对数据库操作

1、查看所有的数据库

mysql> show databases;
 
2、创建数据库

mysql> create database db_name;  //创建名称为db_name的数据库
 
3、删除数据库

> drop database db_name;  //删除名称为db_name的数据库
 
4、使用数据库

>use db_name; //对数据库操作前必须使用

 
三、对表操作

1、创建表
列的格式:列名+数据类型+属性;

create table student(
id int auto_increment primary key, //设置主键
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;  //编码

 

2、显示数据库中的所有表

show tables;
 

3、查看表的结构

describe student;   # 可以简写为desc student;
 
4、对表名重命名

rename table student to stubents;
或者使用:
alter table stubent rename students;
 
5、设置主键与外键:

    主键:

方法1:primary key:
create table student(
    sno char(4) primary key,   #设置主键(单字段主键)
    sname char(10),
    sage int,
    ssex char(10)
);

   
    外键:
    foreign key(列名) references 父类表(列名)

create table Score (
    Sno varchar(20) not null,
    Cno varchar(20) not null,
    Degree Decimal(4,1),
    foreign key(Sno) references student(Sno),  #外键
    foreign key(Cno) references Course(Cno),
    primary key(Son,Cno)   #设置sno和cno的属性组为主键(即多字段主键,或联合主键)
);
 

    自主增删主外键

增删主键:

1、alter table 表名 modify 主键列名 新列类型(不含auto_increment);   #删除自增,才能删除主键

2、alter table 表名 drop primary key;    #删除主键

3、alter table 表名 add primary key(列名);   #添加主键

 
增删外键:

1、alter table 子表名 add [constraint fk_sno] foreign key(子表的外键名称) references 父表名(父表的主键名称);

--添加外键约束。fk_sno为外键ID名,若不添加,系统会自动配一个。

2、alter table 子表名 drop foreign key fk_sno;

--删除外键约束。fk_sno为外键ID名, 若不知,可查询建表明细(show create table 表名)。
 
6、复制表

create table user2 select * from user; #复制表user命名为user2
 

五、对数据操作

1、插入数据
insert into 表名 values(属性);

insert into student values(null,'aa','男','1988-10-2','......');  #给定所有属性数据

insert into student(name, sex, date) values('bb','女','1889-03-6');  #给定部分属性数据

insert into student values (null,'王老师'), (null, '李老师'); #同时插入多条记录
 
2、删除数据

delete from student where id=5; #删除id为5的数据
 

3、修改数据
update 表名 set 修改内容 where 条件;

update student set age = 20 where id = 2; #修改id为2的学生年龄为20
 

4、查找数据(方法较多)
select 内容 from 表名 (可附属条件);

select name,age ,id from student;  #查询数据表student中的所有名字与年龄
 
六、查询条件关键词用法

1、where用法,where后加条件
注:条件符可用 ‘> ‘, ’ =’ , ’ <’ , ’ >=’, <= , != ;

select * from student where age>40; #查询age大于40的学生信息
 
2、and用法,一般在where后,连接多条件“ 且 ”的关系!两个条件都得满足

select * from student where age>40 and age<60; #小于60且大于40的
 
3、or用法,一般在where后,连接多条件“ 或 ”的关系!A or B满足A或者B都显示出来

select * from c where age<40 or age>60; #大于60和小于40的都显示
 
4、between…and…用法,在两者之间

select * from c where age between 40 and 60; 年龄在40到60之间的
 

5、in用法,在某几个值内的

select * from c where age in (30,48,68,99); #in 查询指定集合内的数据
 
6、order by排序用法,asc升序(从小到大) des降序(从大到小)

select * from c order by age desc; #将年龄降序排列显示
 
7、其他:
not用法,取反;as 另命名;group by 分组查询;

select name,max(age) from c group by sex;  #按性别分组进行,查年龄最大值

select * from c where not age between 40 and 60; #不再40到60之间的

select name as '名字',max(age)  as '年龄' from c group by sex;  #按性别分组进行,查年龄最大值
 
like 字符串匹配;is null 要求指定值等于null;

 

未更完!待续。。。。
---------------------
原文作者:微笑smiled
原文链接:https://blog.csdn.net/weixin_43896747/article/details/86484200

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP