本帖最后由 武汉18期-黄美铃 于 2022-8-7 22:20 编辑
create table txt like xiaoling;==>创建一个txt表,表结构和xialing表结构一致
insert into txt select*from xiaoling;==》往txt表里插入xiaoling表中所有的数据
insert into txt2(id,name,class)select id,name,class from xiaoling;==>往txt2表中插入xiaoling表中的id,class,name中的数据
注意:插入的表是必须存在的,插入的表是新表,没有数据
备份库
mysqldump -uroot -p123456 lili>/sql==>把lili这个库备份到根目录下sql文件中
mysqldump -uroot -p123456 li6/sql==》把sql文件还原到li6这个数据库中
注意:li6这个库要先存在,不然会找不到相应的库
用户权限
use mysql;==进入masql库
select host,user,password from user;==》查询有哪些用户
localhost和127.0.0.1 表示本地用户==》可以通过centos和xsell对数据库进行操作的用户
%代表具有远程访问权限的用户==》可以通过数据库客户端工具进行连接操作
新增用户
1.往user表新增一个dcs18的用户
insert into user(host,user,password)values("localhost","dcs18",password("123456"));
2.show grants for "dcs18"@"localhost";==》查看是否有权限
3.flush privileges;==》刷新加载权限
4.grant select,update on *.* to "dcs18"@"localhost" identified by "123456";==》
*.*==》表示所有的库和库下所有的表
5.revoke all on *.* from "dsc18"@"localhost";==》移除本地用户的dcs18的所有权限
新建远程用户并赋予所有权限
grant all privileges on *.* to "dcs18"@"%" identified by "123456";
删除本地用户
delete from user where user='dcs18' and host='localhost';
update user set password=password('123456') where user='root';修改 root用户的密码
连接不上
1.用户是否具有远程访问权限
2.关闭防火墙(linux界面操作)
service iptables stop
3.检查mysql服务是否启动
service mysqld start
ctrl+q新建一个窗口
ctrl+w退出一个窗口
单行注释: 用#号, 被注释的sql语句不会执行
多行注释:/* 注释内容*/
两个表中字段的值相同时则可以进行拼接
1.基本连接(取aa和cc表交集部分)
select*from aa,cc where aa.id=cc.s_id ;
2.内连接
select*from aa inner join cc on aa.id=cc.s_id ;
3.左连接(左边表中数据全部显示,右边表符合条件的显示,不符合条件的用null填充,以左表为基准)
select*from aa left join cc on aa.id=cc.s_id;
4.右连接(右表中的数据全部显示,左表符合条件显示,不符合条件的以null填充,以右表为基准)
select*from aa right join cc on aa.id=cc.s_id;
5.硬链接--追加(2张表的字段数量必须相同)
select*from aa
UNION
select*from cc;
求张三成绩
1.临时表
select*from aa,cc where aa.id=cc.s_id;
select t.score from (select*from aa,cc where aa.id=cc.s_id) as t where t.name='zhangsan';
2.基本连接
select cc.score from aa,cc where aa.id=cc.s_id and aa.name='zhangsan';
3.嵌套方法(=和in的区别,=只能是一个值,in后面可以是多个值)
select id from aa where name='zhangsan'==》先求出学号
select score from cc where s_id=(select id from aa where name='zhangsan');
嵌套"in"
select score from cc where s_id in (select id from aa where name='zhangsan');
求谁没参加考试?
左连接
select aa.name from aa left join cc on aa.id=cc.s_id where cc.score is null;
右连接
select aa.name from cc right join aa on aa.id=cc.s_id where cc.score is null;
基本连接+嵌套
select name from aa where aa.id not in(select id from aa,cc where aa.id=cc.s_id);
嵌套
select name from aa where id not in(select s_id from cc);
|
|