赞
踩
目录
SQL语句——DDL(数据定义语言,用来定义数据库对象(数据库、表、字段))
SQL语句——DML(数据操作语言,用来对数据库表的数据进行增删改)
SQL语句——DQL(数据查询语言,用来查询数据库中表的记录)
SQL语句——DCL(数据控制语言,用来创建数据用户、控制数据库的访问权限)
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
前面也发过一篇相关数据库博客:https://mp.csdn.net/mp_blog/creation/editor/134504925
SQL:一门操作关系型数据库的编程语言,定义操作所有关系型数据库的统一标准。
通用语法
SQL语句可以单行或多行书写,以分号结尾。
SQL语句可以使用空格/缩进来增强语句的可读性。
MySQL数据库的SQL语句不区分大小写。
注释:
1.单行注释:-- 注释内容 或 #注释内容(MySQL特有)
2. 多行注释: /*注释内容 */
创建数据库:create database [if not exists] 名称;
查询数据库:show database; (所有)
select database();(当前)
删除数据库:drop database [if exists]名称;
使用数据库:use 名称;
创建表:
create table 表名(
字段1 字段类型 [约束] [comment 字段1注释],
.......
字段n 字段类型 [约束] [comment 字段n注释]
)comment 表注释;
查询当前库所有表:show tables;
查询表结构:desc 表名;
查询建表语句:show create table 表名;
表操作
添加字段:alter table 表名 add 字段名 类型(长度)[comment 注释][约束];
修改字段类型:alter table 表名 modify 字段名 新数据类型(长度);
修改字段名和字段类型:alter table 表名 change 旧字段名 新字段名 类型 (长度)[comment 注释][约束];
删除字段:alter table 表名 drop column 字段名;
修改表名: rename table 表名 to 新表名;
删除表:drop table [if exists]表名;
- #创建表
- create table tb_user(
- id int primary key comment 'ID,唯一标识',
- username varchar(20) not null unique comment'用户名',
- name varchar(10) not null comment '姓名',
- age int comment '年龄',
- gender char(1) default '男' comment'性别'
- ) comment '用户表';
-
-
- create table tb_emp(
- id tinyint auto_increment primary key comment'ID,唯一标识',
- username varchar(20) not null unique comment'用户名',
- password varchar(32) default '123456' comment'用户密码',
- name varchar(10) not null comment'员工姓名',
- gender tinyint unsigned not null default '男' comment'性:1男\2女',
- image varchar(100) comment'图像url',
- job tinyint unsigned comment'职位:1为班主任、2为讲师、3为学工主管、4为教研主管',
- entrydate date comment'入职日期',
- create_time datetime comment '创建时间',
- update_time datetime comment '修改时间'
- )comment '员工表';

数据类型
指定字段添加数据:insert into 表名(字段名1,字段名2)values(值1,值2);
全部字段添加数据:insert into 表名 value(值1,值2);
批量添加数据(指定字段):insert into表名(字段名1,字段名2)value(值1,值2)(值1,值2);
批量添加数据(全部字段):insert into表名 value(值1,值2)(值1,值2);
修改数据:update 表名 set 字段名1 = 值1,字段名2=值2,....[where 条件];
删除数据:delete from 表名 [where 条件];
- -- 指定字段添加数据:insert into 表名(字段名1,字段名2)values(值1,值2);
- insert into tb_emp(id,username,password,name) values (null,'hanli','123','韩立',1);
- -- 全部字段添加数据:insert into 表名 value(值1,值2);
- insert into tb_emp values (null,'waner','1234','婉儿',2,null,2,'2024-1-1',now(),now());
- -- 批量添加数据(指定字段):insert into表名(字段名1,字段名2)value(值1,值2)(值1,值2);
- insert into tb_emp(id,username,name) values (null,'lier','灵儿',2), (null,'lifeiyu','历飞羽',1);
- -- 批量添加数据(全部字段):insert into表名 value(值1,值2)(值1,值2);
- insert into tb_emp values (null,'waner2','1234','儿',1,null,2,'2024-1-1',now(),now()),
- (null,'waner3','1234','婉',2,null,4,'2024-1-1',now(),now());
- #修改数据
- update tb_emp set name ='韩立',update_time = now() where id='2';
- update tb_emp set entrydate = '2024-2-14',update_time=now();
查询多个字段:select 字段1,字段2,字段3 from 表名;
查询所有字段(通配符):select*from 表名;
设置别名:select 字段1[as 别名1],字段2[as 别名2]from 表名;
去除重复记录:select distinct 字段列表 from 表名;
select 字段列表 from 表名 where 条件列表;
聚合函数:将一列数据作为一个整体,进行纵向计算。
select 聚合函数(字段列表)from 表名;
函数:count 统计数量 max 最大值 min 最小值 avg 平均值 sum 求和
分组查询:select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
条件查序:select 字段列表 from 表名 [where 条件] [group by 分组字段] order by 字段1 排序方式1,字段2 排序方式2...;
排序方式: ASC 升序(默认值) DESC 降序
select 字段列表 from 表名 limit 起始索引,查询记录数;
use mysql;
select * from user;
在MySQL中 用户的信息和具有的权限的信息 都是存放在系统数据库mysql中的user表中。
※host意为主机,只有同时确定了主机名和用户名才能定位mysql中的用户。
注:下列语句中紫色字体为固定内容,黑色字体改为具体信息
create user '用户名'@'主机名' identified by '密码';
※如果希望能够在任意主机上访问数据库,在主机名位置处输入通配符%即可
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';
drop user '用户名'@'主机名';
show grants for '用户名'@'主机名';
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
※数据库名和表名可以使用通配符*代替。
*.*意为给用户授权的对象是所有数据库;数据库名.*意为对象是该数据库的所有表。
多个权限之间使用逗号分隔。
常用的权限有以下几种:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。