数据库和mysql常用的语法

数据库的介绍

bin目录存放启动文件:
mysqld.exe(就是服务端)
mysql.exe(客户端)
data目录:
存放核心数据
my-default,ini:
默认的配置文件
readme:
软件说明

mysql的基本使用

服务端启动:mysqld(ctrl + c是停止服务端)

客户端启动:mysql(exit是停止客户端)

net start mysql开机就自动启动关机就自动关闭

image

net stop mysql(管理员)

image

要卸载重新按装

要先关闭服务端:net stop mysql

移除系统服务:mysqld --remove

修改密码

myspladmin(空格)-u用户名(空格)-p原密码(空格)password(空格)新密码

select * form mysql.user\G;:查看用户信息

image

\s查看MySQL相关信息(当前用户、版本、编码、端口号)

image

永久修改编码操作(utf8)

把文件my-default.ini拷贝一个命名为my.ini

内容都删掉,直接拷贝字符编码相关配置即可无需记忆(修改了配置文件中关于[mysqld]的配置 需要重启服务端)

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4

mysql的语句

查看所有数据库名称:show databases

image

查看所有的表名称:要先选择一个库:show tables;

image

查看所有的记录:select * from mysql.user;这样字段太多会错乱在后面加\G就是不会让它在错乱了;

image

创建库:create database库名;

image

查看库:

show databases; 查看所有的库名称

show create database 库名; 查看指定库信息

image

删除库:drop database 库名;

image

切换操作库:use db1;

image

创建表:create table 表名(字段名 字段类型,字段名 字段类型);

image

查看表:show tables; 查看库下所有的表名称

image

查看表结构:describe 表名;

image

查看表结构:desc 表名;

image

如果想跨库操作其他表只需要在表名前加库名即可:desc mysql.user;

image

表改名字:alter table 表名 rename 新表名;

image

删除表:drop table 表名;

image

插入数据:insert into 表名 values(数据值1,数据值2);

image

查询数据:select * from 表名( 查询表中所有的数据)

image

select 表头(id) from 表名 查询指定表头数据 可以查询多个,隔开

image

修改数据:update 表名 set 字段名=新数据 where 筛选条件(第几个数据)不加改所有的;

image

删除数据:delete from 表名;删所有的

delete from 表名 where id=1;指定的可以删多个

image

查看字段的绑定关系 show create table 表名;

select * from 表名;查询表的字段和内容

image

select 字段 from 表名 查询这个字段的内容

image

select char_length(字段) from 表名;查询这个字段的内容长度

image

查询表的关键字

select * from 表名 where id >= 3 and id <= 6;查询表id三到六的数据

image

select * from 表名 where id between 3 and 6;查询表id三到六的数据

image

select * from 表名 where salary=20000 or salary=18000 or salary=17000;查询多个工资不同的员工信息

image

select * from 表名 where salary in (20000,18000,17000);查询多个工资不同的员工信息

image

select * from 表名 where id<3 or id>6;查询id小于三或者大于六的数据

image

查询id小于三或者大于六的数据,select * from 表名 where id not between 3 and 6;

image

select * from 表名 where name like '%关键字%';%:匹配任意个数的任意字符(模糊查询)

image

查询员工姓名是由四个字符组成的员工姓名与其薪资,select * from 表名 where name like '----'

image

select * from 表名 where char_length(name) = 4;查询员工姓名是由四个字符组成的员工姓名与其薪资

image

select * from 表名 where post_comment is NULL;查询岗位描述为空的员工名与岗位名

image

聚合函数:用于分组之后的数据统计查询(max\min\sum\avg\count:最大值、最小值、求和、平均值、计数)

select * from 表名 group by post;将员工数据按照部门分组

image

select post,max(salary) from 表名 group by post;获取每个部门的最高工资

image

修改字段名as跟你要修改的字段名,select post as‘输入你要的字段名’,max(salary) as '同上' from 表名 group by post;

image

一次获取部门最高薪,最低薪,平均薪资和月支出,select post,max(salary)‘最高薪’,min(salary)‘最低薪’,avg(salary)‘平均薪资’,sum(salary)‘月支出’ from 表名 group by post;

image

统计每个部门的人数,select post,count(id) from 表名 group by post;

image

统计每个部门的部门名称以及部门下的员工姓名,select post,name from 表名 group by post;

数据之间的拼接方法关键字(group_concat(字段,一些小功能字段))

select post,group_concat(name) from 表名 group by post;

image

给数据之间添加方法

image
image

having过滤分组(having用在分组之后(二次筛选))

统计各部门年龄在30岁以上的员工平均工资 并且保留大于10000的数据

select * from emp where age > 30;筛选出所有年龄大于30岁的员工数据

筛选出来的数据按照部门分组并统计平均薪资,select post,avg(salary) from emp where age > 30 group by post;

对分组统计之后的结果做二次筛选,select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;

distinct去重(数据必须一模一样才可以去重)

针对多个字段组合去重(这几个字段数据相同)

select distinct id,age from 表名;多个字段去重

select distinct age from 表名;单个字段去重

image

order by排序

select * from emp order by age desc; 默认降序

select * from 表名 order by age; 默认升序,select * from emp order by age asc;升序

image

先按照年龄升序排相同的情况下再按照薪资降序排,select * from emp order by age,salary desc;

image

limit分页(展示的数据多少分页)

select * from 表名 limit 5; 直接限制展示的条数

image

select * from 表名 limit 5,5;从第五条开始往后读五条

select * from 表名 order by salary desc limit 1;查询工资最高的人的详细信息

image

regexp正则表达式(自己写正则查询)

select * from 表名 where name regexp '^j.*?(n|y)$'

字段类型之整型

整型无需添加数字没什么作用(用来控制展示的长度)

image
tinyint 1bytes 正负号(占1bit)
smallint 2bytes 正负号(占1bit)
int 4bytes 正负号(占1bit)
bigint 8bytes 正负号(占1bit)

严格模式

当我们在使用数据库存储数据的时候 如果数据不符合规范 应该直接报错而不是擅自修改数据 这样会导致数据的失真(没有实际意义)

show variables like '%mode%';:查看严格模式

image

永久修改,修改配置文件,记得关闭开启后有用sql_mode=STRICT_TRANS_TABLES

image

字段类型之浮点型

image

float(20,10)

总共存储20位数 小数点后面占10

double(20,10)

总共存储20位数 小数点后面占10

decimal(20,10)

总共存储20位数 小数点后面占10

三者的核心区别在于精确度不同

float < double < decimal(最高)

创建表模式:create table 表名(表头名 字段类型(存储多少位,小数点后面占多少))

image

字段类型之字符类型(存储大小)

char:定长

char(存储数据大小):最多存储规定的字符超出就报错不够拿空格填充至指定长度

varchar:变长

varchar(存储数据大小):最多存储规定的字符超出就报错不够就存储那么多,就是通过把数据存在报头里,通过报头存储/取数据确定数据。

char优势

整存整取 速度快(直接取规定长度好拿取和存)注意mysql会自动的取的时候去掉空格取出来就是没有空格

劣势:浪费存储空间

varchar优势:节省存储空间

劣势:存取数据的速度较char慢

char与varchar的使用需要结合具体应用场景

创建表的模式:create table 表名(表头 数据类型,表头 字符类型(空间大小))

image

字段类型之枚举与集合(多选和单选)

枚举(多选一:给你几个选项只能选一个)

create table 表名(表头 数据类型,表头 字段存储(数据空间),gender enum(选择项,选择项,选择项))

image

添加数据:insert into 表名 values(对应的数据,对应的数据,对应的数据)

image

集合(多选多(多选一):选多个也行选一个也行)

create table 表名(表头 数据类型,表头 字段类型(存储空间),hobbies set(指定数据,指定数据,指定数据));

image

字段类型之日期类型

datetime 年月日时分秒

date 年月日

time 时分秒

year 年

create table 表名(表头 字段类型,表头 字段类型(存储范围),register_time datetime,birthday date,study_time time,work_time year);前面的都是表名

image

添加方法

image

字段约束条件

无符号、零填充:unsigned无符号(不能有符号)

create table 表名(字段名 字段类型 unsigned);

image
image

zerofill:不够拿零填充剩下的,多的存自己的数据

create table 表名(字段名 字段类型(范围)zerofill)

image
image

非空(就是不为空)

所有字段类型不加约束条件的情况下默认都可以为空

创一个没有约束的表

image

添加数据

image

开始约束子段不能为空在字段后面加not null

create table t2(d int,name varchar(16) not null);

但是‘’这样可以加进去要自己做限制

默认值(给字段添加默认值,你不传值它默认给你传默认的值)字段后面加default

create table 表名(字段名 字段类型 default 默认值)

image

唯一值:unique

create table 表名(id int,name varchar(32) unique);

image

联合唯一就是指定几个字段,这几个字段不能都是一样的(只要有一个不一样的就行)

create table 表名(id int,ip varchar(32),port int,unique(ip,port))

主键(不能为空和不能重复)一个表里只有一个主键:primary key

create table 表名(id int primary key,name varchar(32));

自己增加:auto_increment配合主键一起用,一个表里只有一个

create table 表名(id int primary key auto_increment);

自增特性:自增不会因为数据的删除而回退永远自增往前如果自己设置了更大的数则之后按照更大的往前自增

外键(用于标识数据与数据之间关系的字段)

关系的判断

一对多、多对多、一对一、没有关系

一对多关系(针对'一对多'关系外键字段建在'多'的一方)

员工表创建

create table emp(id int primary key auto_increment,name varchar(32),dep_id int,foregin key (dep_id) references dep(id));

部门表

create table dep(id int primary key auto_increment,dep_name varchar(32),dep_desc varchar(64)

一个员工不能对应多个部门、一个部门可以有多名员工

创建表的时候一定要先创建被关联对象,录入表数据的时候一定要先录入被关联表,修改数据的时候外键字段无法修改和删除

级联更新级联删除(on update cascade on delete cascade)

部门表

create table 表名(id int primary key auto increment,dep_name varchar(32),dep_desc varchar(64))

员工表

create table 表名(id int primary key auto_increment,name varchar(32),age int, dep_id int,foreign key(dep_id) references dep1(id) on update cascade on delete cascade);

这个就可以删部门然后员工也会被删掉部门id信息更改员工所对应的部门信息也会更改

多对多关系(书和作者的关系)建三个表多对多的表最后再建

第一个表的多方法

create table 表名(id int primary key auto_increment,title varchar(32),price float(5,2));

第二个表的多方法

create table 表名(id int primary key auto_increment,name varchar(32),phone bigint);

第三表的方法:外键

create table 表名(最好是起一个跟两个表相关的名字)(id int primary key auto_increment,author_id int,foreign key(author_id) references author(id) on update cascade on delete cascade, book_id int,foreign key(book_id) references book(id) on update cascade on delete cascade)

一对一关系(外键在查询频率高的一边)

外键的表

create table user(id int primary key auto_increment,name varchar(32),detail_id int unique,foreign key(detail_id) references userdatail(id) on update cascade on delete cascade);

另一张表

create table 表名(id int primary key auto_increment,phone bigint);

扩展内容

视图(虚拟的表)

触发器(达到某个条件之后自动触发执行)

事务(事务中的各项操作是不可分割的整体,要么同时成功要么同时失败,使数据库从一个一致性状态变到另一个一致性状态,多个事务之间彼此不干扰,也称永久性,指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的)

数据库的内置函数使用

数据库索引相关的概念

链接ps(不想写了)反正这个也是我博客

https://www.cnblogs.com/yinjinxi/p/17347054.html
image
image
image
image

posted @ 2023-04-09 21:19  因近  阅读(8)  评论(0编辑  收藏  举报