侧边栏壁纸
  • 累计撰写 119 篇文章
  • 累计创建 25 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

MySQL数据库的基本使用

梁来福
2022-03-08 / 0 评论 / 0 点赞 / 0 阅读 / 5882 字
温馨提示:
本文最后更新于 2024-05-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

MySQL介绍

image.png

MySQL特点

image.png

SQL语句

数据库操作

1、登录数据库

mysql -u root -p

2、显示当前时间

select now();

3、登出(退出)数据库

exit/quit/ctr+d

4、查看所有数据库

show databases;

5、创建数据库

create database python charset=utf8;

6、使用数据库

use python;

7、查看当前使用的数据库

select database();

8、删除数据库

drop database python;

表结构操作

1、查看当前数据库中的所有表

show tables;

2、创建表

create table students(id int unsigned primary key auto_increment not null, name varchar(10) not null, age tinyint default 0, gender enum("男","女")default "男");

说明:

create table 表名(

字段名称 数据类型 可选的约束条件,

column1 datatype contrai,

...

);

3、修改表-添加字段

alter table students add birthday datetime not null;

4、修改表-修改字段类型

alter table students modify birthday date;

说明:

modify:只能修改字段类型或者约束,不能修改字段名

5、修改表-修改字段名和字段类型

alter table students change birthday birth datetime not null;

说明:

change:既能对字段重命名又能修改字段类型还能修改约束

6、修改表-删除字段

alter table students drop birth;

7、查看创表SQL语句

show create table students;

8、查看创库SQL语句

show create database python;

9、删除表

drop table students;

表数据操作

查询数据-

所有列

select * from students;

指定列

select name,age from students;

添加数据-

主键列插入数据的时候可以指定:0/default/null

全列插入

insert into students values(0, "梁来福", 1.1, default);

insert into students values(default, "刘来福", 1.1, default);

部分列插入

insert into students(name, age) values("健康", 99);

全列多行插入

insert into students values(0, "发财", 99, "女"),(0, "努力", 88, default);

部分列多行插入

insert into students(name, age) values("幸运", 99),("开心", 99);

修改数据-

单列

update students set age = 88 where id = 3;

多列

update students set age = 66,gender = "女" where id = 8;

删除数据-

delete from students where id = 99999;

逻辑删除数据

添加一个标识字段

alter table students add is_del tinyint default 0;

update students set is_del = 1 where id = 6;

关键字

as

as关键字,用户给表的字段和表设置别名

select name as 姓名,age as 年龄 from students as 学生表;

提示

as关键字可以省略

select name 姓名,age 年龄 from students 学生表;

distinct

distinct关键字,用于去除重复的数据行

select distinct age, gender from students;

select distinct gender from students;

where条件查询

支持的运算符

  • 比较运算符
  • 逻辑运算符
  • 模糊查询
  • 范围查询
  • 空判断

格式

select * from 表名 where 条件;

比较运算符

  • 等于:=
  • 大于:>
  • 大于等于:>=
  • 小于:<
  • 小于等于:<=
  • 不等于:!=或<>

1、id大于3的

select * from students where id > 3;

2、id小于8的

select * from students where id <=8;

3、姓名不是“梁来福”的

select * from students where name != "梁来福";

逻辑运算符

  • and
  • or
  • not

1、is_del等于0的

select * from students where is_del = 0;

2、id大于3并且性别是女的

select * from students where id > 3 and gender = "女";

3、id小于4或者id_del等于0的

select * from students where id > 4 or is_del = 0;

4、年龄大于1并且小于90的

select * from students where age >=1 and age <= 90;

5、取反“年龄大于1并且小于90的”

select * from students where not(age >=1 and age <= 90);

范围查询

between ... and ...

表示在一个连续的范围内查询

1、编号是3至8的

select * from students where id >= 3 and id <= 8;

select * from students where id between 3 and 8;

2、编号不是3至8的

select * from students where not(id between 3 and 8);

in

表示在一个非连续的范围内查询

1、编号是1,3的

select * from students where id in (1, 3);

2、编号不是1,3的

select * from students where id not in (1, 3);

模糊查询

  • like是模糊查询关键字
  • %表示任意多个任意字符
  • _表示一个任意字符

1、查询姓梁的

select * from students where name like "梁%";

2、查询姓梁并且名字是一个字符的

select * from students where name like "梁_";

3、查询姓梁并且名字是两个字符的

select * from students where name like "梁__";

4、查询姓梁的或者名字叫来福的

select * from students where name like "梁%" or name like "%来福";

空判断查询

1、查询没有年龄的数据

select * from students where age is null;

2、查询有年龄的数据

select * from students where age is not null;

注意

  • 不能使用where age = null 判断为空
  • null不等于“空字符串”
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin
博主关闭了所有页面的评论