数据库基础(一)概念及分类
; 一、数据库基础篇
相关概念
- 数据库(DataBase 简写DB): 存储数据的仓库,对数据有组织的存储。
- 数据库管理系统(DBMS) :操纵和管理数据库的大型软件。
主流关系型数据库管理系统(RDBMS): Oracle(收费)、Mysql(开源免费)、SQL Server、PostgerSQL、SQLite(嵌入式微型数据库,主要用于Android) - SQL:操作关系型数据库的编程语言,定义了一套操作关系型数据库的同一标准。
- 关系型数据库管理系统(RDBMS):建立在关系模型的基础上,由多张相互连接的二维表组成的数据库。(通过表结构存储数据)
优点:1、使用表存储数据,格式统一,便于维护。2、使用SQL语言操作,标准统一,使用方便。
; SQL分类
SQL语言分类说明DDL(Data Definition Language)数据定义语言,用来定义数据库对象DML(Data Maniputation Language)数据操作语言,用来对数据库表中数据进行增删改等操作DQL(Data Query Language)数据查询语言,用来查询数据库表中记录DCL(Data Control Language)数据控制语言,用来创建数据库用户,控制数据库访问权限
SQL语句
不区分大小写,结尾加分号!!!!!
DDL
- 查询
show databases;
select database();
show tables;
desc 表名;
show create table 表名;
- 创建
create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];
create table user(
id int comment 'ID',
username varchar(50) comment '用户名',
password varchar(20) comment '密码',
age int comment '年龄')comment '用户表';
- 删除
drop database [if exists] 数据库名;
alert table 表名 drop 字段名;
drop table [if exists] 表名;
truncate table 表名;
- 使用
use 数据库名;
- 修改
alert table user add gender tinyint comment '性别';
alert table 表名 modify 字段名 新数据类型(长度);
alert table user change username name varchar(30) comment '用户名';
alert table 表名 rename to 新表名;
DML
添加数据
- 字段顺序与值顺序一一对应;
- 字符串和日期类型需要加引号;
- 插入数据大小应在字段规定范围内。
insert into user(id,username,password,age) values(1,'tyq','123456',18);
insert into 表名 values(值1,值2,...);
insert into 表名(字段1,字段2,...) values(值1,值2,...),(值1,值2,...),(值1,值2,...),(值1,值2,...);
修改数据
update 表名 set 字段名1=值1, 字段名2=值2,... [where 条件];
删除数据
delete from 表名 [where 条件];
DQL
select 字段列表 from 表名 where 条件列表 group by 分组字段列表 having 分组后条件列表 orderby 排序字段列表 limit 分页参数;
- 聚合函数: 将一列数据作为一个整体,进行纵向计算。(count,max,min,avg,sum)
- 基本查询
select 字段1 [as 别名1],字段2 [as 别名2],... from user;
select username as '用户名',password as '用户密码' from user;
select distinct 字段列表 from 表名;
- 条件查询
select 字段列表 from 表名 where 条件列表;
比较运算符功能>大于>=大于等于
- 分组查询
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后的过滤条件];
select gender ,count(*) from emp group by gender;
select gender,count(*) from emp where age <45 group by gender having count(*)>5;
where和having
- 执行时机不同:where是分组之前进行过滤,不满足where条件不参与分组;而having是分组之后对结果进行过滤。
- 判断条件不同:where不能对聚合函数进行判断,而having可以。
-
执行顺序 where> 聚合函数>having
-
排序查询
asc:升序(默认) desc:降序
多个字段排序,优先按照前面的字段,当前一个字段相同时,按照后面字段排序。
select * from user order by age asc;
分页查询
select 字段列表 from 表名 limit 起始索引,查询记录数;
DCL
user mysql;
select * from user;
create user '用户名'@"主机名" identified by '密码';
create user 'root'@'localhost' idebtified by '123456';
alert user '用户名'@"主机名" identified with mysql_native_password by '新密码';
drop user '用户名'@"主机名"
show grants for '用户名'@"主机名";
grant 权限列表 on 数据库.表名 to '用户名'@"主机名";
revoke 权限列表 on 数据库.表名 from '用户名'@"主机名";
Original: https://blog.csdn.net/weixin_43978588/article/details/127156229
Author: yq02
Title: # 数据库基础(一)概念及分类
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815090/
转载文章受原作者版权保护。转载请注明原作者出处!