新建列名和老表一样的新表,添加查询到这个表里的所有内容,insert into actor_name
select first_name,last_name from actor;(创建新表,把老表的内容)
ignore如果不存在则插入,如果存在则忽略
INSERT OR IGNORE INTO tablename VALUES(...);
如果不存在则插入,如果存在则替换 两句的or可以不加,mysql里面不加
INSERT OR REPLACE INTO tablename VALUES(...);
这里指的存在表示的是unique属性的列值存在的情况下,unique表示键值唯一
(
mysql中要把or去掉)
limit offset
select * from 表名 order by 列 desc limit 1 offset 2;
查询入职员工时间排名倒数第三的员工的所有信息
limit 后跟两个参数时,第一个跳过的数量,后一位是表要取的量
创建索引
--如何创建索引
create index 索引名 on 表名(列名)
create index ind_name on student(name);
--如何删除索引
drop index ind_name;
--重命名索引:
alter index index_old rename to index_new;--重新命名索引
--查看表的索引:
show index from table_name(表名)
--索引分类:
普通索引:normal
create index 索引名 on 表名(列名);
create index ind_name on student(name);
--唯一性索引:unique
create unique index 索引名 on 表名(列名);
create unique index ind_name on school(phone);
--位图(分类)索引:bitmap
--数据量比较大,基数比较小 比如:男/女
create bitmap index 索引名 on 表名(列名);
create bitmap index ind_sid on student(sid);
--函数索引:
create index ind_email on student(length(email));
视图
-- 创建一个视图。将查询出来的结果保存到这张虚拟表中
-- 标准语法
CREATE VIEW 视图名称 AS 查询语句;
CREATE
VIEW
city_country
AS
SELECT t1.*,t2.country_name FROM city t1,country t2 WHERE t1.cid=t2.id;
MySQL
1. 准备工作
1.1 配置环境变量
C:\Program Files\MySQL\MySQL Server 5.7\bin
将MySQL安装目录下bin文件夹配置到环境变量path中
1.2 设置服务状态
徽标 + R 输入 services.msc 打开系统服务界面
将MySQL57服务右键设置为自动启动
1.3 测试连接
mysql -u root -p9999 这种方式将密码以明文的方式展示,不安全
mysql -u root -p 回车 然后输入密码 推荐使用这种方式
mysql -h 123.45.2.120 -u root -p 可以使用-h 添加其他主机地址 如果不指定则默认连接本机
ipconfig查看本机地址
ping + 主机地址查看本机与对应地址网络是否通畅
shutdown -i 呼出远程关机对话框
shutdown - a 取消远程关机
2. SQL语句分类
DDL 数据定义语言 用于创建,修改表、数据库等
DML 数据操纵语句 比如 增 删 改
DQL 数据查询语句 查询
DCL 数据控制语句 比如 授权 提交 回滚等
3. DDL 定义语句
3.1 创建数据库
创建数据库 CREATE DATABASE [IF NOT EXISTS] 数据库名;
删除数据库 DROP DATABASE [IF EXISTS] 数据库名;
查看数据库 SHOW DATABASES;
使用数据库 USE 数据库名;
3.2 创建表
创建表
create table city(
cid int not null comment '城市编号',
cname varchar(6) not null comment '城市名称'
);
显示表结构
desc city;
显示表创建语句
show create table city;
4. 列的类型
int 对应java中的int类型 可以通过unsigned设置无符号
double 对应java中的double
char 固定长度字符串
varchar 可变长字符串
datetime 年月日时分秒
# 创建表
create table stu(
studentNo int(4) not null comment '学号',
loginPwd varchar(20) comment '登录密码',
studentName varchar(20) comment '学生姓名',
sex tinyint(1) comment '性别取值0女或者1男',
gradeId int(11) comment '班级编号',
phone varchar(50) not null comment '联系方式',
address varchar(255) not null comment '地址',
bornDate datetime comment '出生日期',
email varchar(50) not null comment '邮箱',
identityCard varchar(18) comment '身份证号'
);
5. 列的属性
UNSIGNED 无符号的 声明该数据列不允许负数
ZEROFILL 0填充的 不足位数的用0来填充,如 int(3),5则为 005
AUTO_INCREMENT 自动增长的,每添加一条数据,自动在上一个记录数上加1 通常用于设置主键,且为整数类型 可定义起始值和步长
NULL 和 NOT NULL 默认为NULL,即没有插入该列的数值 如果设置为NOT NULL,则该列必须有值
DEFAULT 默认的 用于设置默认值 例如,性别字段,默认为“男”,否则为“女”;若无指定该列的值,则默认为“男”的值
# 创建表 create table person( pid int primary key auto_increment comment '主键', pname varchar(20) not null comment '名字', psex char(1) default '男', page int unsigned not null comment '年龄', pnum int(5) zerofill comment '测试0填充' ); select * from person;
6. 表的属性
表的注释
create table a( aid int, aname varchar(10) )comment '表a';
表的引擎
create table b( bid int )engine=myisam;
表的编码
create table c( cid int )charset=gbk
7. MyISAM和InnoDB的区别?
8. 表对应文件
表对应文件在Program Data 文件夹下 Data文件夹中
此文件夹默认隐藏,需要手动显示
C:\ProgramData\MySQL\MySQL Server 5.7\Data\2114
9. 修改表
# 修改表名
alter table a rename as abc; --a表名 abc新表名 rename to/rename as
# 添加字段
alter table abc add asize int not null comment '新添加的一列';
# 修改列的类型 和 属性(不能改列名)
alter table abc modify asize varchar(20) not null comment '修改类型为varchar';
#现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为'2020-10-01 00:00:00'
alter table actor add create_date datetime not null default '2020-10-01 00:00:00';
# 修改列的名称 类型 和 属数(数据类型)
alter table abc change asize size int(3) not null comment '修改为int类型';
#abc 表 asize列名 size 新列名
# 删除某一列
alter table abc drop size;
10.删除表
# 删除表 drop table if exists abc;
11.外键
我们可以使用外键来使两个表产生关联关系
# 创建部门表
# 主键 primary key 不能为空 不能重复
#表名+类型+primary key+自增
# 主表 父表
create table dep(
did int primary key auto_increment comment '部门编号',
dname varchar(20) not null comment '部门名称'
);
# 从表 子表
create table emp(
eid int primary key auto_increment comment '员工编号',
ename varchar(10) not null comment '员工名称',
eage int unsigned not null comment '员工年龄',
depid int not null comment '关联部门表中的编号'
);
# 外键 foreign key
# 外键命名规范 FK_开头 后边加上要指定外键的列名 即可
alter table emp add constraint FK_depid foreign key(depid) references dep(did);
# 删除外键
# 因为在创建外键的同时 mysql会自动添加一个索引 index 所以 删除外键 也要删除 index
# 删除外键
alter table emp drop foreign key FK_depid;
# 删除索引
alter table emp drop index FK_depid;
也可以在创建表的同时 创建外键约束
实际开发中 我们不适用物理外键 而是使用逻辑外键
# 创建部门表 # 主键 primary key 不能为空 不能重复 # 主表 父表 create table dep( did int primary key auto_increment comment '部门编号', dname varchar(20) not null comment '部门名称' ); # 从表 子表 # 创建表的同时 创建外键 create table emp( eid int primary key auto_increment comment '员工编号', ename varchar(10) not null comment '员工名称', eage int unsigned not null comment '员工年龄', depid int not null comment '关联部门表中的编号', constraint FK_depid foreign key(depid) references dep(did) ); # 删除索引和外键 alter table emp drop foreign key FK_depid; alter table emp drop index FK_depid;
12. DML
12.1 添加
添加数据使用insert into + 表名 (列名) values (值);
# 添加数据
# 方式1
insert into emp(eid,ename,eage,depid) values(2,'广坤',18,2);
# 方式2
insert into emp(eage,ename,depid) values(19,'刘能',2);
# 方式3
insert into emp values(4,'小宝',20,1);
# 方式4
insert into emp values(5,'富贵',20,1),(6,'富贵',20,1),(7,'富贵',20,1),(8,'富贵',20,1);
# 批量添加 insert into 表名(列1,列2,列3) values(值1,值2,值3);
12.2 删除
删除数据使用delete from + 表名 + where 条件
# 删除数据 # 删除编号为5的人 delete from emp where eid = 5; # 删除名字为富贵的人 delete from emp where ename = '富贵'; # 删除名字为赵四 并且年龄为17 delete from emp where ename = '赵四' and eage = 17; # 删除所有的数据 所以删除通常要加条件 delete from emp;
12.3 修改数据
update + 表名 + set 列名 = 值 ,列名 = 值 where 条件;
# 修改数据 # update + 表名 + set 列名 = 值 ,列名 = 值 where 条件; # 将人事部 改为 人力资源部 update dep set dname = '人力资源部' where did = 1; # 将员工编号为1 的人 名字改为 刘能 年龄 改为 20 update emp set ename = '刘能',eage = 20 where eid = 1;
13.DQL
数据查询语句
select语句 使用频率最高的SQL语句 最复杂的SQL语句
group by分组字段
having分组后条件group by分组字段名 having分组后条件过滤
13.1 基本查询
# 查询 # * 表示所有的列 select * from emp; # 查询名字信息 select ename from emp; # 查询名字和年龄 select ename,eage from emp;
13.2 as关键字
# 给列取别名 使用as关键字 select ename as '员工姓名' , eage as '员工年龄' from emp; # as关键字可以省略 但是不推荐省略 select ename '姓名' , eage '年龄' from emp; # as关键给表取别名 select e.eage,e.ename,e.depid from emp as e;
13.3 去重复
# 使用distinct 关键字 去除重复记录 (数据) select distinct ename from emp;
13.4 where条件
# and 和 && # 查询名字为 赵四 并且 年龄为 19 的人的 地址 和 邮箱信息 select ename ,address,email from emp where ename = '赵四' && eage = 19; # or 和 || # 查询部门编号 为 1 或者 年龄为 17岁的人员信息 select * from emp where depid = 1 || eage = 17; # is not null 或者 is null 查询身份证号不为空的人员信息 select * from emp where identity is null; # 查询名字 不为赵四的人的信息 select * from emp where ename!= '赵四'; # between and 查询年龄在 18 ~ 20 之间的人员信息 select * from emp where eage between 18 and 20;
13.5 模糊查询
# like 模糊查询 # 查询名字中带'四'的人员信息 # % 表示匹配 0个 或者 多个 任意字符 select * from emp where ename like '%四%'; # 查询名字以四结尾的人的信息 select * from emp where ename like '%四'; # 查询名字以 四 开头的人信息 select * from emp where ename like '四%'; # 查询名字 以赵开头 并且 名字为三个字的人的信息 # _ 一个 下划线表示一个 任意字符 select * from emp where ename like '赵__'; # 查询 年龄 在 15,16,17,18 这些之中的 select * from emp where eage in(15,16,17,18);
14. 连接查询
等值连接
# 查询 select * from emp; # 等值连接查询 select * from emp,dep where emp.depid = dep.did; # 指定具体的列来查询 select e.ename,e.eage,d.dname from emp as e,dep as d where e.depid = d.did;
内连接
# 内连接查询 查询结果与等值连接相同 select * from emp inner join dep on emp.depid = dep.did;
左外连接
# 外连接 # 左外连接 以左表为主表 右表不匹配的行 以null填充 select * from emp left join dep on emp.depid = dep.did;
右外连接
# 右外连接 以右表为主表 左表不匹配的 以null填充 select * from emp right join dep on emp.depid = dep.did;
三表连接查询
# 三表连接查询 select * from emp,dep,city where emp.depid = dep.did and emp.cityid = city.cid; select * from emp inner join dep inner join city on emp.depid = dep.did and emp.cityid = city.cid;
自连接
CREATE TABLE `course` ( `cid` int(11) NOT NULL AUTO_INCREMENT, `profession` varchar(255) DEFAULT NULL, `cname` varchar(255) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
# 正常查询 select * from course; # 自连接 一个表当做两个表来用 select c1.profession,c2.cname from course as c1 , course as c2 where c1.cid = c2.profession;