MySQL 常用命令:
概括:结构化查询语言,用于操作关系型数据库服务器,对数据进行增删改查;
行业习惯:SQL 命令不区分大小写,习惯上关键字大写,非关键字(可变值)小写。
[tip type="warning" title="注意事项:"]
1:登录前不加分号,登录后命令以分号结束,可多行空格。
2:如使用 xampp 需设置 mysql 服务端 config->my.ini 文件内 default-character-set=gbk
以支持汉字。
[/tip]
常用命令示例 | 解释 |
---|---|
mysql -h127.0.0.1 -P3306 -uroot -p | 登录本地数据库,全写示例 |
mysql -uroot | 登录本地数据库,简写示例 |
set names utf8; | 登录后设置为 utf-8 字符编码 |
quit; | 退出 |
show databases; | 显示所有数据库 |
use 数据库名; | 进入数据库 |
show tables; | 显示当前数据库所有的表 |
desc 表名称; | 描述数据表的表头有哪些项和数据类型 |
mysql -uroot< 拖拽文件到这里然后回车 | 运行 xx.sql 脚本文件(连接前) |
# | 单行注释,加在命令前 |
/**/ | 多行注释 |
create database abc charset=utf8; | 创建数据库,名为 abc 并设置字符编码为 utf-8 |
drop database if exists abc; | 丢弃数据库 abc,如果存在 |
create table abc1( id int, name varchar(8) ); | 创建数据表 abc1,并添加项: id, name |
insert into abc1 values( '1', 'aaa' ); | 结合上条,在数据表 abc1 里按顺序插入值: id 为 1, name 为 aaa |
update abc1 set name='bbb',sex='W'; | 修改数据表 abc1 内某项所有的值 |
update abc1 set name='bbb',sex='W' where id='1'; | 修改数据表 abc1 内 name 的值为 bbb,sex 的值为 W,条件为 id=1 的 |
delete from abc1 where id='1'; | 删除数据表 abc1 里的 id 为 1 的这条数据 |
查询数据命令:
查询数据建议使用交互模式,登录并进入数据库查询,以展示表格线。
常用查询命令示例 | 解释 |
---|---|
select * from abc1; | 查询数据表 abc1 中所有内容 |
select id,name from abc1; | 查询数据表 abc1 中指定的项 id 和 name |
select id as a,name as b from abc1; | 查询数据表 abc1 中指定的项 id (换名 a)和 name( 换名 b);as 可用空格省略。 |
select distinct deptid from abc1; | 查询数据表 abc1 中指定的项 deptid,不出现重复。 例:适用于查询“都有哪些不同的....(部门?)” |
select name a,(salary+500)*12+20000 b from abc1; | 查询数据表 abc1 中 name(换名 a) 和 salary 加 500 再乘以 12 再加 20000,然后换名 b,可做加减乘除计算。 适用例:月工资加 500,年终奖 20000,查询出所有员工姓名和计算后的年薪,以及换名为 b。 |
select * from abc1 order by id asc,name; | 查询数据表 abc1 ,先按照 id 的升序排序,id 相同则按 name 排序; 知识点: 1、 ,name 不需要则可删除。2、使用 asc 为升序,desc 为降序;3、按首字符 的 Unicode 码排序; 4、不加排序规则,默认按升序排序。 |
select * from abc1 where id=5; | 查询数据表 abc1 中 id 的条件等于 5 的其他数据; 较为常用。 知识点: 1、其中 = 符号,也可以使用如 > 、<= 、!= (不等于)、等条件进行查询。 |
select * from abc1 where name is null; | 查询数据表 abc1 中 name 为 null 的数据; 如找 不为空 的值:null 替换 not null |
select * from abc1 where salary>2000 and sex=0; | 查询数据表 abc1 中 salary 大于 2000 并 sex=0 的数据。and 也可以替换为 or ,即表示两个条件只需满足一个; |
select * from abc1 where salary in(1000,2000); | 查询数据表 abc1 中 salary 值为 1000 或 2000 的数据。 或替换为 salary not in(1000) 即表示 salary 不等于 1000 的值。 |
select * from abc1 where name like e; | 查询数据表 abc1 中 name 项包含字母 e 的值。知识点: 1: % 表示任意个字符,_ 表示任意一个字符如:如: e 换为 %e ,最后一个字符为 e 的值。e 换为 %e_ ,倒数第二个字符为 e 的值。 |
select * from abc1 limit 0,5; | 查询数据表 abc1 ,从 0 开始查询,每页显示 5 个。 知识点: 1:0 表示开始查询的值,5 表示每页显示数量; 2:mysql 中第一个数据是从 0 开始的; 3:开始查询的值=(当前页码-1)*每页数量。 比如要查询第 4 页,即 0,5 替换成 15,5 |
复杂查询示例: | 解释 |
---|---|
select id,name,birthday,salary from abc1 where salary>8000 and sex=0 order by salary desc; | 查询数据表 abc1 中,工资在 8000 以上的女员工的编号、姓名、生日、工资,结果按照工资降序排列。 解释: salary>8000 and sex=0 :工资 8000 以上的女员工;id,name,birthday,salary :编号、姓名、生日、工资;order by salary desc :结果按照工资降序排列。 |
select max(salary) from abc1 where sex=0; | 聚合查询, 查询数据表 abc1 中最高工资的女员工。 知识点: count() :统计sum() :相加avg() :平均max() :最大min() :最小 |
select avg(salary),max(salary),min(salary),deptid from abc1 group by deptid; | 分组查询, 查询数据表 abc1 中各部门平均工资、最高工资、最低工资,一般带上分组条件 deptid 。group by deptid 按 deptid 进行分组 |
select * from abc1 where salary=(select max(salary) from abc1); | 子查询, 以另一个查询结果 (select max(salary) from abc1) 为条件,查询数据表 abc1 中工资最高的对应员工。 |
select * from abc1 where year(birthday)=1995; | 查询数据表 abc1 中 1995 年出生的员工。 |
select name,dname from abc1,abc2 where bid=did; | 多表查询,外键约束前提下查询的列分别在多个表中。 查询所有员工的姓名以及部门名称。 比如:name 姓名 ,dname 部门名称,不在一个表中,id 在 abc1 表中且 bid 项与 abc2 表中的 did 项建立有外键约束。 如果 也可以指定表: name,dname 替换为 abc1.name,abc2.dname |
select name,dname from abc1 left outer join abc2 on id=did; | 左外连接, 显示左侧表中所有的记录,先写哪个表,哪个就在左。 知识点: 1: left 可替换为 right ;2: outer 可以省略不写。 |
数据类型:
创建数据表:create table 数据表名(项名 数据类型,项名 数据类型);
例:create table abc1(id int,name varchar(8));
常用类型 | 解释 |
---|---|
tinyint | 微整型,占 1 字节,范围-128~127, 常用年龄, |
smallint | 小整型,占 2 字节,范围-32768~32767 |
int | 整型,占 4 个字节,范围-2147483648~2147483647, 一般用于 ID |
bigint | 大整型,占 8 个字节,范围 63 个 1 的 2 进制转 10 进制 |
decimal(7,2) | 定点小数,小数点不会位移, 7 表示小数全部位数,2 表示小数后 2 位有效位数 常用工资等... |
boolean | 布尔型,可简写 bool,只有 2 个值, true 为真或用 1 表示,false 为假或用 0 表示, 插入时使用 true 或 false 不能加引号,插入后会自动转为 tinyint 微整型 |
date | 日期型,'2021-12-25' ,常用生日 |
time | 时间型,'15:28:30' |
datetime | 日期时间型,'2021-12-25 15:28:30' |
varchar(16) | 变长字符串型,最大 16 个字符, 最大可 65535 字符 最常用,姓名、标题、文本等... |
char(11) | 定长字符串型,固定 11 个字符, 最大 255 字符,可能产生空间浪费 例如身份证,手机,固定位数密码等... |
text(200) | 大型变长字符串型,最大存储 2GB |
[tip type="info" title="空值:null
"]
插入数据时,null
不能加引号,可以使用 desc 表名; 以查看数据类型是否允许为空。
[/tip]
[tip type="info" title="非空约束:not null
"]
创建数据表时,在某个数据类型后面加入 not null
,即约束此项不允许插入 null 空值。
[/tip]
[tip type="info" title="主键约束:primary key
"]
创建数据表时,在某个数据类型后面加入 primary key
,即以此项为主键。
知识点:
1:一个表内只允许有一个主键约束;
2:插入主键约束后,则按主键约束进行排序,并该项值不能为 null“空”。
例:create table abc1(id int primary key,name varchar(8));
[/tip]
[tip type="info" title="重复约束:unique
"]
创建数据表时,在某个数据类型后面加入 unique
,即约束此项不允许重复插入。
[/tip]
[tip type="info" title="指定默认值:default'默认值'
"]
创建数据表时,在某个数据类型后面加入 default'默认值'
,即约可以给某项指定默认值。
例:create table abc1(id int,name varchar(8) default'未知');
方式 1:插入时某项的值写 default,按默认值填写,不能加引号。
方式 2:插入时指定给其他某项赋值,例:insert into abc1(id) values(1);
即只给 id 项赋值为 1,name 值就会为默认值。
[/tip]
[tip type="info" title="外键约束:foreign key(fid) references abc1(id)
"]
例:
表 1 :create table abc1(id int);
表 2 :create table abc2(fid int,foreign key(fid) references abc1(id));
创建数据表 abc2 时,写在最后,约束此表内的 fid 项,必须在 abc1 表中 的 id 项中出现过。
知识点:
1:允许插入 null;
2:与另一个表的数据类型需保持一致。
[/tip]
[tip type="info" title="自增:auto_increment"]
创建数据表时,在主键项后面加入 auto_increment
,插入值时只需赋值 null
,就会自动获得最大值后 +1 插入,适用于 id 项。
例:create table abc1(id int primary key auto_increment);
知识点:
必须添加在主键上,并保证数据类型为 int 整数。
[/tip]
set names utf8;
drop database if exists tedu;
create database tedu charset=utf8;
use tedu;
create table dept(
did int primary key,
dName varchar(8) unique,
addTime datetime,
Score decimal(4,2)
);
insert into dept values(
'10',
'研发部',
'2020-01-01 09:01:29',
'76.21'
);
insert into dept values(
'20',
'市场部',
'2020-01-01 09:31:05',
'66.09'
);
create table emp(
eid int primary key auto_increment,
ename varchar(4) not null,
phone char(11) default'未填写',
sex varchar(3),
birthday date,
deptid int,
admin boolean,
foreign key(deptid) references dept(did)
);
insert into emp values(
'1',
'明志',
'13912341234',
'男',
'1989-06-27',
'10',
1
);
insert into emp values(
null,
'春娇',
'13952015201',
default,
null,
'20',
0
);
4 条评论
555
骚年,我怀疑你写了一篇假的文章!::funny:03:: 写得好好哟,我要给你生猴子!::funny:04:: 滴!打卡时间:8:33:05 PM,请上车的乘客系好安全带~
骚年,我怀疑你写了一篇假的文章!::funny:03:: 写得好好哟,我要给你生猴子!::funny:04:: 滴!打卡时间:8:33:05 PM,请上车的乘客系好安全带~