客户端图形化工具基本操作

客户端图形化工具:SQLyog

DML:增删改表中数据(重点)

​ 1.添加数据
​ *语法:
​ *insert into 表名(列名1,列名2, , , ,列名n)values(表1,表2 , , 表n);
​ *注意:
​ 1.列名和值一一对应
​ 2.如果表名后,不定义列名,则默认给所有列添加值
​ insert into 表名 values(值1,值2,…值n);
​ 3.除了数字类型,其他类型要用引号
​ 2.删除数据
​ *语法:
​ *delete from 表名 where 条件}
​ *注意:
​ 1.如果不加条件,则删除表中所有记录
​ 2.如果要删除所有数据:TRUNCATE TABLE 表名; /先删除表,然后再创建一张一样的表
​ 3.修改数据
​ *语法:
​ *update 表名 set 列名1 = 值1,列名2 = 值2,…{where 条件};
​ *注意:
​ 1.如果不加条件,则删除表中所有记录

DQL:查询表中的记录

​ * slect* from 表名;

1.语法:
select
字段列表
from
表名列数
where
条件列表
group by
分组字段
having
分组之后的条件
order by
排序
limit
分页限定

2.基础查询
1.多个字段的查询
select 字段名1.字段名2…from 表名;
*注意:
如果查询所有字段,则可以使用代替字段列表
2.去除重复
*distinct
3.计算列
*一般可以使用四则运算来计算一些列额值。(一般只会进行数值型的计算)
*ifnull(表达式1,表达式2)
*表达式1:哪个字段需要判断是否为null
*如该字段为null后的替换值
4.起别名
*as:as也可以省略 打个空格就行
3.条件查询
1.where子句后面跟条件
2.运算符
*<,>,<=,>=,=,<>
*between and
*in(集合)
*like:模糊查询
*占位符:
*-单个任意字符
*%多个任意字符
*is null
*and 或&&
*or 或||
*not 或 |

        查询年龄大于20岁
		select *from student where age > 20;
		select *from student where age >= 20;
		查询年龄等于20岁
		select *from student where age = 20;
		查询年龄不等于20岁
		select *from student where age <> 20;
		select *from student where age |= 20;
		查询年龄大于等于20小于等于30
		select *from student where age >= 20; && age <=30;
		select *from student where age >= 20; and age <=30;
        select *from student where age between 20 and 30;
		查询年龄22岁,18岁,25岁的信息
		select *from student where age = 22 or age = 18 or age = 25;
		select *from student where age in (22.18,25);
		查询英语成绩为null
		select *from student where english = null;这是不对的。null值不能用= (|=)判断
		select *from student where english is null;
		
		查询英语成绩不为null
		select *from student where english is not null;

DQL:查询语句

​ 1.排序查询
​ *语法:order by 子句
​ *order by 排序字段1 排序方式1,排序字段2,排序方式2…
​ *排序方式:
​ *ASC:升序
​ *DESC:降序
​ *注意:
如果有多个排序条件,则当前边的条件值一样时,才会判断第二条件

​ 2.聚合查询:将一列数据作为一个整体,进行纵向的计算
​ 1.count:计算个数
​ 1.一般选择非空的列;主键
​ 2.count(

​ 2.max:计算最大值 select max(math) from student;
​ 3.min:计算最小值
​ 4.sun:计算和
​ 5.avg:计算平均值

​ *注意;聚合函数的计算,排除null值
​ 解决方案:
​ 1.选择不包含非空的列进行计算
​ 2.ifunll函数

​ 3.分组查询
​ 1.语法:group by 分组字段
​ 2.注意:
​ 1.分组之后查询的字段:分组字段。聚合函数
​ 2.where和having 的区别?

  • where 在分组之后进行限定,如果不满足条件,则不参与分组。having在分组之后进行限定,如果不满足结果,则不会被查询出来

  • where后不可以跟聚合函数,having 可以进行聚合函数的判断

    4.分页查询
    1.语法:limit 开始的索引,每页查询的条数;
    2.公式:开始的索引 = (当前的页码 - 1 ) * 每页显示的条数
    –每页显示三条记录

          select * from student limit 0,3;  --第一页
          select * from student limit 3,3;  --第二页
          select * from student limit 6,3;  --第三页
          
       3.limit是一个“方言”
    

    约束

    ​ *概念:对表中的数据进行限定,保证数据的正确性,有效性和完整性。
    ​ *分类:
    ​ 1.主键约束:primary key
    ​ 2.非空约束:not null
    ​ 3.唯一约束:nuique
    ​ 4.外键约束:foreign key
    ​ *非空约束:not full

            1.创建表是添加约束
    		  create table stu(
    		       id int,
    			   name varchar(20)not null --name为非空
    		  );
    	    2.创建表完后,添加非空约束 
    		  alter table stu modify name varchar(20) not null;
    		3.删除name的非空约束
    		  alter table stu modify name varchar(20);
    

    ​ *唯一约束:unique ,值不能重复
    ​ 1.创建表时,添加唯一约束
    ​ create table stu(
    ​ id int,
    ​ phone_number varchar(20) unique --添加了唯一约束
    ​ );
    ​ *注意:在mysql中,唯一约束限定的列的值可以有多个null
    ​ 2.删除唯一约束
    ​ alter table stu drop index phone _number;
    ​ 3.在创建表后,添加唯一约束
    ​ alter table stu modify phone_number varchar(20) unique;
    ​ *主键约束:primary key
    ​ 1.注意:
    ​ 1.含义:非空并唯一
    ​ 2.一张表只能有一个字段为主键
    ​ 3.主键就是记录的唯一标识
    ​ 2.在创建表时,添加主键约束
    ​ create table stu(
    ​ id int primary key,–给id添加主键约束
    ​ name varchar(20)
    ​ );

    ​ 3.删除主键
    ​ 错误形式 alter table stu modify id int;
    ​ 正确 alter table stu drop primary key;

    ​ 4.创建完表之后,添加主键
    ​ alter table stu modify id int primary key;

    ​ 5.自动增长:
    ​ 1.概念:如果某一列是数值型的,使用auto_increment 可以来完成值得自动增长

    ​ 2.在创建表时,添加主键约束,并且完成主键自动增长
    ​ create table stu(
    ​ create table stu(
    ​ id int primary key auto_increment, --给Id添加主键约束
    ​ name varchar(20)
    ​ );

    ​ 3.删除自动增长
    ​ alter table stu modify id int;

    ​ 4.添加自动增长
    ​ alter table stu modify id int auto_increment,;

    ​ *外键约束:foreign key,让表与表产生关系,从而保证数据的正确性
    ​ 1.在创建表时,可以添加外键
    ​ *语法:
    ​ create table 表名(
    ​ …
    ​ 外键列
    ​ costraint 外键名称 foreign key (外键列名称) rferences 主表名称 (主表列名称)
    ​ );

    约束

    ​ *概念:对表中的数据进行限定,保证数据的正确性,有效性和完整性。
    ​ *分类:
    ​ 1.主键约束:primary key
    ​ 2.非空约束:not null
    ​ 3.唯一约束:nuique
    ​ 4.外键约束:foreign key
    ​ *非空约束:not full

    ​ *唯一约束:unique ,值不能重复
    ​ 1.创建表时,添加唯一约束
    ​ create table stu(
    ​ id int,
    ​ phone_number varchar(20) unique --添加了唯一约束
    ​ );
    ​ *注意:在mysql中,唯一约束限定的列的值可以有多个null
    ​ 2.删除唯一约束
    ​ alter table stu drop index phone _number;
    ​ 3.在创建表后,添加唯一约束
    ​ alter table stu modify phone_number varchar(20) unique;
    ​ *主键约束:primary key
    ​ 1.注意:
    ​ 1.含义:非空并唯一
    ​ 2.一张表只能有一个字段为主键
    ​ 3.主键就是记录的唯一标识
    ​ 2.在创建表时,添加主键约束
    ​ create table stu(
    ​ id int primary key,–给id添加主键约束
    ​ name varchar(20)
    ​ );

    ​ 3.删除主键
    ​ 错误形式 alter table stu modify id int;
    ​ 正确 alter table stu drop primary key;

    ​ 4.创建完表之后,添加主键
    ​ alter table stu modify id int primary key;

    ​ 5.自动增长:
    ​ 1.概念:如果某一列是数值型的,使用auto_increment 可以来完成值得自动增长

    ​ 2.在创建表时,添加主键约束,并且完成主键自动增长
    ​ create table stu(
    ​ create table stu(
    ​ id int primary key auto_increment, --给Id添加主键约束
    ​ name varchar(20)
    ​ );

    ​ 3.删除自动增长
    ​ alter table stu modify id int;

    ​ 4.添加自动增长
    ​ alter table stu modify id int auto_increment,;

    ​ *外键约束:foreign key,让表与表产生关系,从而保证数据的正确性
    ​ 1.在创建表时,可以添加外键
    ​ *语法:
    ​ create table 表名(
    ​ …
    ​ 外键列
    ​ costraint 外键名称 foreign key (外键列名称) rferences 主表名称 (主表列名称)
    ​ );

​ 2.删除外键
​ alter table 表名 drop foreign key 外键名称 ;
​ 3.创建表之后,添加外键
​ alter table 表名 adda costraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称);

​ 4.级联操作
​ 1.添加级联操作
​ 语法:alter table 表名 add costraint 外键名称
​ foreign key (外键字段名称) references 主表名称(主表列名称) on update cascade on delete cascade;
​ 2.分类:
​ 1.级联更新:on update cascade
​ 2.级联删除:on delete cascade

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值