# MysqlDemo **Repository Path**: hovoh/mysql ## Basic Information - **Project Name**: MysqlDemo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-17 - **Last Updated**: 2024-10-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ![Mysql数据类型.png] # 表操作-查询 /** 使用数据库 */ use db2024; /** 查询当前数据库中所有表 */ show tables; /** 查询表结构 */ DESC t_pay; /** -查询指定表的建表语句 */ Show CREATE TABLE t_pay; # 表操作-创建 create table emp( id int comment "编号", workno varchar(10) comment "工号", name varchar(10) comment "姓名", gender char(1) comment "性别", age tinyint unsigned comment "年龄", idcard char(18) comment "身份证号", entrydate date comment "入职时间" ) comment "员工表"; /** 添加字段 */ Alter table emp add nickname varchar(20) comment "昵称"; DESC emp; /** 修改字段 1.修改数据类型 */ ALTER table emp Modify nickname varchar(18); DESC emp; /** 2.修改字段名和字段类型 */ Alter table emp change nickname nickname1 varchar(20); DESC emp; /** 删除字段 */ ALTER TABLE emp DROP nickname1; /** 修改表名 */ ALTER TABLE emp RENAME TO employee; # DML 数据库操作语言 插入 修改 删除 insert into employee(id, workno, name, gender, age, idcard, entrydate)value (1,123,'sada','1',1,123435565434343424,'2000-1-1'); insert into employee value (2,133,'adsda','1',1,157835565434343424,'2000-1-2'); /** 选择 表所有项目 */ select * from employee; /** 修改字段 */ update employee set name = 'itheima' , age = 18 where id = 1; /** 删除数据 */ delete from employee where age = 1; # DQL 数据查询语言 SELECT Select * from employee; /** 查询字段 */ Select name,gender from employee; SELECT * from employee; /** 设置别名 并筛选重复项目 */ Select distinct name as 'n' from employee; /** 条件筛选 */ Select * from employee where workno > 300; SELECT * from employee where id is not null; /** in 搜寻 列表中任意符合 */ SELECT * from employee where age in (18,23); /** like 模糊匹配 _匹配单个字符 % 匹配任意个字符 */ Select * from employee where name like '__'; Select * from employee where name like '%i'; /** 聚合函数 (count、max、min、avg 、sum) */ select count(*) from employee; select count(id) from employee; select avg(age) from employee; select max(age) from employee; select min(age) from employee; select sum(age) from employee; /** 分组查询 */ select gender ,count(*) from employee group by gender; select gender ,avg(age) from employee group by gender; /** 查询 年龄小于 20 且 以 性别分类, 获取 数量大于 1 的 年龄 */ select employee.gender,count(*) from employee where age < 20 group by gender having count(*) > 1; /** 排序查询 order by */ select * from employee order by age desc; /** 分页查询 */ /** 查询 employee 从 0 开始 每页 10 个 */ select * from employee limit 0,10; select * from employee limit 10; /** 查询 employee 第二页开始 每页 10 个 */ select * from employee limit 10,10; # DCL 数据库管理语言 Use mysql; SELECT * from user; /** 创建 用户 itcast,只能在当前主机 localhost 访问 密码 123456 */ create user 'itcast'@'localhost' identified by '123456'; /** 创建用户在任意主机上都能访问数据库 密码 123456 */ create user 'heima'@'%' identified by '123456'; /** 修改用户密码 */ Alter user 'heima'@'%' identified with mysql_native_password by '1234'; /** 删除用户 */ Drop user 'heima'@'%'; /** 查询权限 */ use mysql; /** 查询当前用户的权限 */ SHOW GRANTS FOR 'itcast'@'localhost'; /** 授予用户权限 */ grant all on emp.* to 'itcast'@'localhost'; /** 撤销用户权限 */ revoke all on emp.* from 'itcast'@'localhost'; # 外键约束 /** 表约束 */ use db2024; show tables from db2024; /** 创建表并约束字段 */ create table user( id int primary key auto_increment comment '主键', name varchar(10) not null unique comment '姓名', age int check ( age > 0 and age < 120) comment '年龄', status char(1) default '1' comment '状态', gender char(1) comment '性别' ) COMMENT '用户表'; insert into user(name, age, status, gender) value ('dada',10,'1','男'),('def',23,'0','女'); /** 外键约束 */ create table dept ( id int auto_increment comment 'ID' primary key , name varchar(50) not null comment '部门名称' ); insert into dept (id, name) value (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办'); create table emp( id int auto_increment primary key comment 'ID', name varchar(50) not null comment '姓名', age int comment '年龄', job varchar(20) comment '职位', salary int comment '薪资', entrydate date comment '入职时间', managerid int comment '直属领导ID', dept_id int comment '部门ID' )comment '员工表'; insert into emp (id, name, age, job, salary, entrydate, managerid, dept_id) VALUE (1,'金庸',66,'总裁',20000,'2000-01-01',null,5), (2,'张无忌',45,'项目经理',10000,'2009-01-01',1,1), (3,'杨过',30,'马仔',1000,'2019-01-01',2,1), (4,'搜答',30,'开发',800,'2019-03-01',2,1); /** 创建外键关联 */ alter table emp add constraint fk_emp_det_id foreign key (dept_id) references dept(id); /** 删除外键 */ alter table emp drop foreign key fk_emp_det_id ; /** 外键删除和更新行为 */ /** 父表删除/更新对应记录 首先检查该记录是否有对应外键,如果有 则删除/更新外键在子表中的记录 */ alter table emp add constraint fk_emp_det_id foreign key (dept_id) references dept(id) on UPDATE cascade on delete cascade ; /** 父表删除/更新对应记录 首先检查该记录是否有对应外键,如果有 则删除/更新外键在子表中的记录为 null (这时候要求该外键允许为 null) */ alter table emp add constraint fk_emp_det_id foreign key (dept_id) references dept(id) on UPDATE set null on delete set null ; # 多表查询 1.一对多的关系 一个部门对应多个员工 实现:在多的一方建立外键,指向一的一方的主键 2.多对多的关系 学生和课程的关系 一个学生能选修多门课程,一门课程也可以供多个学生选择 实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键 ![关联-多对多.png](img%2F%B9%D8%C1%AA-%B6%E0%B6%D4%B6%E0.png) 3. 一对一的关系 多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率 实现: 在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique) ![关联一对一.png](img%2F%B9%D8%C1%AA%D2%BB%B6%D4%D2%BB.png) create table tb_user( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', age int comment '年龄', gender char(1) comment '1:男,2:女', phone char(11) comment '手机号' )comment '用户基本信息表'; create table tb_user_edu( id int auto_increment primary key comment '主键ID', degree varchar(20) comment '学历', major varchar(50) comment '专业', primarySchool varchar(50) comment '小学', middleSchool varchar(50) comment '初中', university varchar(50) comment '大学', userId int unique comment '用户ID', constraint fk_user_id foreign key (userId) references tb_user(id) )comment '用户教育信息表'; create table tb_user( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', age int comment '年龄', gender char(1) comment '1:男,2:女', phone char(11) comment '手机号' )comment '用户基本信息表'; create table tb_user_edu( id int auto_increment primary key comment '主键ID', degree varchar(20) comment '学历', major varchar(50) comment '专业', primarySchool varchar(50) comment '小学', middleSchool varchar(50) comment '初中', university varchar(50) comment '大学', userId int unique comment '用户ID', constraint fk_user_id foreign key (userId) references tb_user(id) )comment '用户教育信息表'; # 多表查询 select * from emp,dept where emp.dept_id = dept.id; # 内连接(查询两张表交集的部分) # 隐式内连接 select emp.name,dept.name from emp,dept where emp.id = dept.id; select e.name,d.name from emp e, dept d where e.id = d.id; # 显示内连接实现 INNER JOIN...ON... select e.name,d.name from emp e inner join dept d on e.dept_id = d.id; # 外连接 # 左外连接 (左表和右表交集的部分) select e.*,d.name from emp e left outer join dept d on e.dept_id = d.id; # 右外连接(右表和左表交集的部分) select e.*,d.* from emp e right outer join dept d on e.dept_id = d.id; # 自连接