Mysql Navicate 基础操作与SQL语句 版本5.7.29

Mysql Navicate 基础操作与SQL语句 版本5.7.29

SQL数据的增删改查:此部分所有SQL语句在navicat中与mysql命令行执行效果一样,只是mysql服务端在命令行执行,而navicat只是在客户端的图形化打开操作。

一、进入数据库

.连接数据库:mysql -uroot -p

.查看数据库:show databases;

.进入指定数据库:use test;

.查看表:show tables;

.查看当前用户名:select user();

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.查看当前所在数据库:select database();

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.修改字符集:set names 'gbk';

.查看设计表:desc students;

.修改普通用户密码:update user set authentication_string=password('123456') where user='wzx';

                                 flush privileges;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.忘记root用户密码:进入mysql的配置文件,找到my.ini的配置文件:

Mysql Navicate 基础操作与SQL语句 版本5.7.29

再[mysqld]下加入:skip-grant-tables后再重新启动mysql服务:

 Mysql Navicate 基础操作与SQL语句 版本5.7.29

 再进行连接测试:

Mysql Navicate 基础操作与SQL语句 版本5.7.29

 .登录后记得删除配置文件并重新设置root密码:update user set authentication_string=password('123456') where user='wzx';

                                                                            flush privileges;

.修改命令结束符:delimiter   //?;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.查看设计表语句:show create table students;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.给表字段添加索引:create index name_index on students(name(255));

.开启运行时间监控:set profiling=1;

.查看监控结果:show profiles;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.查看表索引:show index from students;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

 注:主键、unique唯一值约束时默认会创建索引。一个表中的一个字段理论上可以有多个索引。

.删除表索引:drop index name_index on students;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.创建表数据时直接设置索引:

create table students(
id int primary key auto_increment ,
name varchar(10) not Null,
sex varchar(4) default "男",
age tinyint unsigned,
height decimal(5,2),
registration_time datetime

key (name)
);

二、创建数据库以及数据恢复与备份

.创建数据库:create database test0412 charset='utf8';

.删除数据库:drop database test0412;

windows下备份与恢复数据库:

.备份数据库:需要以管理员身份运行sql:mysqldump -uroot -p test > e: est_back.sql;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

.恢复数据:需要先创建数据库再导入数据库:

1:创建数据库:testback:create database testback charset='utf8';

2:导入数据库:mysql -uroot -p testback < e: est_back.sql

linux下备份与恢复数据库:

备份数据库:mysqldump -h127.0.0.1 -uroot -proot test>/home/testback .sql

恢复数据库:mysql -h127.0.0.1 -uroot -proot -f test<testback .sql --default-character-set=gbk

三、创建表

1.1:直接创建新表

create table students(
id int primary key auto_increment ,
name varchar(10) not Null,
sex varchar(4) default "男",
age tinyint unsigned,
height decimal(5,2),
registration_time datetime
);

1.2:将需要备份数据表到新表

create table good_back select * from goods;

1.3:查询表数据后添加到新表

create table good_cate(
id int primary key auto_increment ,
goods_name varchar(30)
) select distinct good as goods_name from goods;

1.4:添加外键关系到已有数据表:

####  alter table 从表 add foreign key (从表字段) references 主表(主表字段);

alter table goods add foreign key (brand_id) references goods_brand(brand_id);

1.5:删除外键:

####  alter table 从表 drop foreign key (从表字段);

alter table goods drop foreign key (brand_id);

1.6:数据表时添加外键:

create table goods(
id int primary key auto_increment ,
goods_id int,

foreign key(goods_id) references goods_brand(goods_id)
) ;

四、增加值

2.1:按照数据库中字段顺序插入

INSERT into students value(0,"aa","女",21,3,"2019-02-12");

2.2:按照自己指定指定字段顺序插入
INSERT into students(sex,name,height,registration_time,age) value("保密","wqww",123,"2017-02-2",21);

2.3:在设计表中添加字段并给予说明
alter table students add  new varchar(20) COMMENT '用户名';

2.4:一次性插入多个记录

insert into students values (0,'老夫3',20),(0,'老夫4',20),(0,'老夫5',20);

2.5:将查询结果插入:

insert into goos_cate(cate_name) select distinct cate from goods;

五、修改记录

3.1:静态数据修改

update students set name="明天会更高" where id=1;

3.2:动态数据修改
update students set age=age+20 where id=2;

3.3:表数据修改

update goods  g inner join goods_cate c on g.cate=c.cate_name set g.cate=c.cate_id;

六、删除记录

delete from students where id=2;

七、删除表

drop  table students1;

drop table if exists sutdents1;

八、查询

6.1:给字段起别名:select name as 姓名,age as 年龄 from students;

6.2:给表起别名:select s.name as 姓名,s.age as 年龄 from students as s;

6.3:去重:select distinct age,class from students;

6.4:逻辑运算:select * from students where not name='王昭君';

6.5:模糊查询所有字符:select * from students where name like '王%';

6.6:模糊查询指定字符个数:select * from students where name like '王__';

6.7:范围查询:select * from students where age between 18 and 20;

6.8:为空:select * from students where sex is null;

Mysql Navicate 基础操作与SQL语句 版本5.7.29

6.9:为空字符:select * from students where sex='    ';

6.10:多字段排序:select * from students order by age desc,studentNo asc;

6.11:聚合函数:select count(*) as 学生总数 from students;

6.12:分组:select sex,count(*) from students group by sex;

6.13:分组后过滤having:select sex,count(*) from students group by sex having sex='男';

十、连接查询

1、等值连接,默认会形成笛卡尔积,需要条件过滤

select * from courses,scores where courses.courseno=scores.courseno;

2、内连接

select sc.score,cs.name from scores sc inner join courses cs on sc.courseno=cs.courseno

3、左连接

select stu.name,sc.score from students stu left join scores sc on stu.studentno=sc.studentno;

4、右链接

select sc.score,cs.name from scores sc right join courses cs on sc.studentno=cs.courseno

5、自关联

select * from areas p,areas c where p.aid=c.pid and p.atitle='河南省';

十一、子查询

6、标量子查询(一行一列的数据,一般是聚合函数)

select * from students where age > (select avg(age) from students);

7、列级子查询(一列多行的数据)

select * from students where name in (select name from students where hometown like '北京%');

8、行级子查询(一行多列的数据)

select * from students where (sex,age)=(select sex,age from students where sex='男' order by age desc limit 1);

9、表级子查询(多行多列)

十二、navicat操作

.表清空:删除所有数据,不删表结构,自动递增的值继续累加

.表截断:删除所有数据,不删表结构,自动递增的值从1开始

.备份:右键点击数据库-->存储sql文件

.恢复:右键点击数据库-->运行sql文件

.注释 ctrl + /

.取消注释 ctrl + shift + /

。查询mysql日志功能是否开启:show variables like'general%';

Mysql Navicate 基础操作与SQL语句 版本5.7.29

。开启mysql日志功能:set global general_log=1;

.关闭mysql日志功能:set global general_log=0;