*
create database `公司数据库`; #创建一个数据名
show databases; #列出当前所有的数据库
drop database `公司额数据库`; #删除数据库
use `公司数据库`; #选择要操作的数据库
/*常用的数据类型
INT 整數类型
DECIMAL(m,n) 有小数点的类型 m为整数长度 n为小数点的长度
VARCHAR(n) 字串 类型
BLOB (Binary Large Object) 圖片 影片 檔案 二进制 类型
DATE 'YYYY-MM-DD'日期类型
TIMESTAMP 'YYYY-MM-DD HH:MM:SS'记录时间类型
*/
/* 变量的属性修饰
not null 表示该类型值不能为空。
unique 表示该值不能重复
default "默认值" 设置该类型的默认值
auto_increment 把该类型加1操作。
Primary Key 主键
Foreign Key 外键
Attribute 属性
*/
/*
set sql_safe_updates=0; #关闭预设的更新模式
create table `biaoge`( #创建一个表格 biaoge
`用户名` int auto_increment primary key, #创建一个列名 类型为int 设为主键
`科目1` varchar(20), #创建一个列名 类型为字符串
`科目2` varchar(20), #创建一个列名 类型为字符串
`分数` int
);
describe `biaoge`; #列出表格alter
drop table `biaoge`; #删除一个表格
alter table `biaoge` add var decimal(3,2); #向表格添加一个列表名为 var
alter table `biaoge` drop column var; #把表格中的 var 删除
insert into `biaoge` values(3,"胸大","星儿"); #向表格添加资料 null意思为空
insert into `biaoge` (`科目1`,`科目2`,`分数`) values("中国","新疆",23);
#向表格添加资料 括号可以改变数据写入顺序。
#不指定时 为空
select *from `biaoge`; #显示表格中的数据
#更新表格中的数据
update `biaoge` #要更新的表格名
set `科目1`="祖国" #要更新的值 可设定多个值 `科目1`="祖国",`科目2`="祖国2"
where `科目1`="祖国强大"; #设定要更新的条件 或者多条件 如 `科目1`="祖国强大" or `科目1`="祖国"
#删除表格中的对象
delete from `biaoge` #先指定表格的名字 不指定条件 删除所有成员
where `用户名`=2; #设定条件 可以设定多个条件
#检索资料 where也可用于检索条件返回 IN()函数可获取多个值作为条件判断
select `分数`,`科目1` from `biaoge`; #select 后面写过滤条件
select * from `biaoge` order by `分数`asc; #order by 排序 asc由小到大
select * from `biaoge` order by `分数`desc ; #order by 排序 desc由大到小
select * from `biaoge` limit 3; #限制返回资料数量 条件可以混用
*/
/*------------------------公司数据库笔记部分---------------------------*/
create database `公司数据库`; #创建一个数据名
show databases; #列出当前所有的数据库
drop database `公司数据库`; #删除数据库
use `公司数据库`; #选择要操作的数据库
set sql_safe_updates=0; #关闭预设的更新模式
show tables; #显示数据库所有的表格
#员工表格
create table `employee`( #创建一个表格 员工表格
`emp_id` int primary key, #编号
`name` varchar(20), #名称
`birth_date` date, #日期
`sex` varchar(20), #性别
`salary` int, #工资
`branch_id` int, #部门编号
`sup_id` int #经理编号
);
#部门表格
create table `branch`( #创建一个表格 biaoge
`branch_id` int primary key, #部门表格编号
`branch_name` varchar(20), #部门名称
`manager_id` int, #经理编号
# #把本表格中的manager_id引用到employee的emp_id,当对方的id不存在时救把本表的manager_id的赋值为空
foreign key(`manager_id`) references`employee`(`emp_id`) on delete set null
);
# 1 改变 表格 【表格名称】 添加属性修饰符【列表名称】 2 引用 表格【列表名称】
alter table `employee` add foreign key(`branch_id`) references`branch`(`branch_id`) on delete set null;
# 1 改变 表格 【表格名称】 添加属性修饰符【列表名称】 2 引用 表格【列表名称】
alter table `employee` add foreign key(`sup_id`) references`employee`(`emp_id`) on delete set null;
#客户表格
create table `client`( #创建一个表格 biaoge
`client_id` int primary key, #客户编号
`client_name` varchar(20), #客户名称
`phone` varchar(20) #客户电话
);
#销售表格
create table `Works_With`( #创建一个表格 biaoge
`emp_id` int, #销售编号
`client_id` int, #客户编号
`total_sales` int, #销售金额
#把括号的2个成员设为主键
primary key(`emp_id`,`client_id`),
#修饰本表格成员属性 去引用到 表格名称【表格成员】如果对方表格中没有此id 就删除数据
#此时emp_id 为主键也为外键,当引用对象不存在时 不能设为空 所以只能删除 下方代码 必须为on delete cascade
foreign key(`emp_id`) references`employee`(`emp_id`) on delete cascade,
#修饰本表格成员属性 去引用到 表格名称【表格成员】如果对方表格中没有此id 就删除数据
foreign key(`client_id`) references`client`(`client_id`) on delete cascade
);
#增加部门资料 由于员工表格引用到部门表格中的成员 所以要先创建部门资料,这里先把引用成员设为null
insert into `branch` values(1,'研发',null);
insert into `branch` values(2,'行政',null);
insert into `branch` values(3,'资讯',null);
#添加员工资料
insert into `employee` values(206,'小黄','1998-10-08','F',50000,1,null);
insert into `employee` values(207,'小绿','1985-09-16','M',29000,2,206);
insert into `employee` values(208,'小黑','2000-12-19','M',35000,3,207);
insert into `employee` values(209,'小白','1997-01-22','F',39000,3,208);
insert into `employee` values(210,'小灰','1925-11-10','F',84000,1,209);
#依次更改 buranch 中的 主键manager 中的 branch_id
update `branch` set `manager_id`=206 where `branch_id`=1;
update `branch` set `manager_id`=207 where `branch_id`=2;
update `branch` set `manager_id`=208 where `branch_id`=3;
#增加客户资料
insert into `client` values(400,'阿猫','25636523');
insert into `client` values(401,'阿狗','86543254');
insert into `client` values(402,'旺仔','41259853');
insert into `client` values(403,'路西','14783695');
insert into `client` values(404,'杰克','26589514');
#销售资料alter
insert into `works_with` values(206,400,'256963');
insert into `works_with` values(207,401,'364879');
insert into `works_with` values(208,402,'758967');
insert into `works_with` values(209,403,'146534');
insert into `works_with` values(210,404,'698766');
#-------------------------------练习1---------------------------
#获取表格中的资料
select * from `employee`;
select * from `branch`;
select * from `client`;
select * from `works_with`;
#显示所有员工按薪水低到高alter
select * from `employee` order by `salary` asc;
#显示所有员工按薪高到低alter
select * from `employee` order by `salary` desc;
#获取员工表格中前3的员工
select * from `employee` order by `salary` desc limit 3;
#获取员所有名字
select `name` from `employee`;
#获取员所有名字 不显示重复
select distinct `sex` from `employee`;
#-------------------------------练习2---------------------------
#获取表格中的数据笔数。
select count(*) from `employee`;
#获取表格中出生日期在1980-01-01 之后的F
select count(*) from `employee` where `birth_date` > '1980-01-01' and `sex`='F';
#获取表格平均工资
select avg(`salary`) from `employee`;
#获取所有工资的总和
select sum(`salary`) from `employee`;
#获取表格中最高工资
select max(`salary`) from `employee`;
#获取表格中最低工资
select min(`salary`) from `employee`;
#-------------------------------练习3---------------------------
#获取数据包含 xx的数据客户 %代表 前或者后 2%代表前不关注后面 %3关注后面 不关注前面 %2%只关心中间
select * from `client` where `phone` like '%3%';
#根上方类似
select * from `client` where `client_name` like '阿%';
#查询所有员工生日在12 月份的 一个下划线 代表一个字元 如 1950- 就是5个 _ 下划线
select * from `employee` where `birth_date` like '%12%';
#-------------------------------联合---------------------------
select `name` from `employee`
union #把上方和下方联合到一起 可以联和多个 属性数量统一 数据类型统一
select `client_name` from `client`;
#把员工的id 和名字 和客户的 id 和客户的名字 结合到一起alter
select `emp_id` as `别名1`,`name` as `别名1` from `employee`
union
select `client_id`,`client_name` from `client`;
#员工和薪水结合在一起
select `salary` from `employee`
union
select `total_sales` from `works_with`;
#-------------------------------链接--------------------------
#insert into `branch` values(4,'增加',null);
#把2个表格 中的 数据进行连接在一起 在把成员进行判断 相等在现实出来
#delect 后 '' '' '' 是过滤条件
select `employee`.`emp_id`,`employee`.`name`,`branch`.`branch_name`
from `employee`
right join `branch` #left 左边 right右边
on `employee`.`emp_id`=`branch`.`manager_id`;
#-------------------------------查询--------------------------
#返回 员工表格中的id和部门表格id相同的
select `name` from `employee` where `emp_id`=(
select `manager_id` from `branch` where `branch_name`='研发'
);
#返回 员工表格中id和销售表格ID 超过5000的
select `name` from `employee` where `emp_id` in (
select `emp_id` from `works_with` where `total_sales`>530300
);
#-------------------------------删除--------------------------
#删除 employee 中 id=207的资料
delete from `employee` where `emp_id`=207;
select * from `branch`;
select * from `works_with`;
/*-----------------------链接大概是示意------------------------
import mysql.connector #调用
connection=mysql.connector.connect( #链接
host='localhost' #本机位置
port='3306' #端口
user='root' #用户
password='password' #密码
database='默认指定的资料库'
#开始使用 )
cursor=connection.cursor()
cursor.execute(mysql语句);
#若有返回数据,需要进行处理
records=cursor.fetchall()
cursor.close(); #关闭对象
#有更改到资料 相当于保存
connection.commit();
connection.close(); #关闭连接
*/