第一篇博客

 


mysql的使用

前言

切换表单 use +表名

查询当前数据库 select database();

一、DDL语句

数据库操作

  • show databases;查询所有数据
  • create database 数据库名;
  • use 数据库名;切换数据库
  • select database();定位当前数据库
  • drop database 数据库名;删除数据库

1.查询

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

  • 详细查询每个表:

    desc 表名;

    show create table 表名 ;查询指定表的建表数据(即建表时加的注释)

2.创建

create table 表名(

字段一 字段1类型[comment 字段一注释],

字段二 字段2类型[comment 字段二注释],

字段三 字段3类型[comment 字段三注释],

..........)[comment 表注释];

3.修改

1.添加

alter 表名 add 字段名 类型(长度) [commend 注释] [约束];

例:将sex数据加入到myfirst表中

alter table myfirst add sex varchar(3);

2.修改

表的修改

修改表名

alter table 表名 rename to 新表名;

例:将 表名 myfirst 改为 first;

alter table myfirst rename to first;

数据的修改

修改数据类型

alter table 表名modify 字段名 新数据类型(长度);

例:将date 数据类型改为varchar

alter table myfirst modify date varchar(50);

修改字段名和字段类型

alter table 表名 change 旧字段名 新字段名 类型(长度)[commend注释] [约束];

例:将'日期'改为日期(原来带引号)

alter table myfirst change ‘日期’ 日期 varchar(50);

3.删除

表的删除

直接删除表

drop table[if exit] 表名;

加 if exit 意义 删除表的时候如果表不存在会报错,加上 if exit 即便不存在也不会报错;

删除del 表

drop table del;

删除指定表,并重新创建表

truncate table 表名;

相当于删除表内容,留下表结构;

表内容的删除

删除字段

alter table 表名 drop 字段名;

例:将 data 删除

alter table myfirst drop date;

二、DML语句

1.添加数据

  • 给指定字段添加数据

    语法:insert into 表名(字段1,字段2.....)values(值1,值2值3.....)

    例:插入 一个编号为1,姓名为刘汉康的 数据插入到表中

    insert into user(id, name) value (1,"刘汉康");
  • 给全部字段添加数据

    语法 : insert into 表名values(值1,值2....);

    insert into user values(4,"刘汉康");
  • 批量添加数据

    语法:insert into 表名values(值1,值2...)(值1,值2)(...);

    insert into user values(7,"小明"),(8,"白也");

    $\textcolor{red}{注意}$

​ $\textcolor{red}{1.插入数据时,指定的字段需要与值的顺序一致}$

​ $\textcolor{red}{2.字符串和日期类型 数据应该包含在双引号中}$

​ $\textcolor{red}{3.插入数据的大小,应该在字段的规定范围内}$

2.更新和删除

  • 修改数据

    语法:update 表名 set 字段名1=值1 ,字段名2 =值2,....{where 条件};

    update user set name = "赵无极" where name = "ddd";
    update user set name = "陈平安",id=10 where id=10;

    $\textcolor{red}{注意:修改条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据。}$

  • 删除数据

    语法:delete from 表名 [where 条件];

    $\textcolor{red}{注意:删除条件可以有,也可以没有,如果没有条件,则会删除整张表的所有数据。}$

三、DQL语句

查询

  • 基本查询

    语法:select 字段1,字段2,字段3... from 表名;

    ​ select * form 表名;//查询所有数据

    ​ select 字段 as 加别名;

    ​ select distinct 字段 表名;

    select id,name,age from emp;//部分字段
    select * from emp;//查询所有数据
    select workaddress as 工作地点 from emp;
    select distinct workaddress as 工作地点 from emp;//去除重复记录
  • 条件查询

    语法:select 字段列表 from 表名 where 条件列表;

    select * from emp where id<=10;
    select * from emp where age between 15 and 20;等价于select * from emp where age>=15 &&age <=20;
    select * from emp where name like"__";//查询名字为两个字符的
    select * from emp where idcard like "%x";//结尾为x的数据

    $\textcolor{red}{注:between 后跟范围较小值}$

  • 分组查询

    语法:select 字段列表 from 表名 [where 条件] group by 分组字段名[having 分组后过滤条件];

    select gender,count(*) from emp group by gender;
    select gender,avg(age) from emp group by gender;
    select gender,count(*) from emp where age<=45 group by workaddress;

    $\textcolor{red}{注:where和having区别}$

    $\textcolor{red}{执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果过滤}$

    $\textcolor{red}{判断条件不同:where不能对聚合函数进行判断,而having可以}$

    $\textcolor{red}{注意}$

    $\textcolor{red}{执行顺序 where>聚合函数>having}$

    $\textcolor{red}{分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义}$

聚合函数

  1. 聚合函数将一列数作为一个整体,进行纵向计算

  2. 常见聚合函数

    count 统计数量

    max 最大值

    min 最小值

    avg 平均值

    sum 求和

  3. 使用语法

    select 聚合函数(字段列表) from 表名[亦可加约束条件];

    $\textcolor{red}{注:null值不参与所有聚合函数运算}$

    使用例子

    select count(*) from emp;
    select avg(age) from emp;
    select max(age) from emp;
    select min(age) from emp;
    select avg(age) from emp where workaddress="西安";

分组查询

语法:select 字段列表 from 表名 where限定条件 group by 分组字段名 having 分组后限定条件;

where>having

注:查询字段列表为 分组字段和聚合函数,查询其它字段无意义

分页查询

语法:select 字段列表 from表名 limit 起始索引,查询条目数;

索引从0开始

计算公式: 起始索引=(当前页码-1)*查询条目数

limit关键字是mysql专属,其它数据库不一样

四、约束

约束名称 描述 关键字
非空约束 保证列中所有数据不能有null值 NOT NULL
唯一约束 保证列中所有数据各不相同 UNIQUE
主键约束 主键是一行数据的唯一标识,要求非空且唯一 PRIMARY KEY
检查约束 保证列中的值满足某一条件 CHECK
默认约束 保存数据时,未指定值则采用默认值 DEFAULT
外键约束 外键用来让两个表的数据之间建立联系,保证数据的一致性和完整性 FOREIGN KEY

  • [ ]
posted @   白也,,  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示