MySQL-----表操作
select查询
1 简单查询 select * from employee; select name,salary from employee; 2 where条件 select name,salary from employee where salary > 10000; select name,salary from employee where salary > 10000 and salary < 20000; select name,salary from employee where salary between 10000 and 20000; select name,salary from employee where salary not between 10000 and 20000; select name,salary from employee where salary = 10000 or salary = 20000 or salary = 30000; select name,salary from employee where salary in (10000,20000,30000); select * from employee where salary = 10000 or age = 18 or sex='male'; select * from employee where post_comment is Null; select * from employee where post_comment = Null; select * from employee where post_comment is not Null; select * from employee where name like '%n%'; select * from employee where name like 'e__n'; 3 group by分组 mysql> select depart_id,group_concat(name) from employee group by depart_id; +-----------+--------------------------------------------------------------+ | depart_id | group_concat(name) | +-----------+--------------------------------------------------------------+ | 1 | egon,alex,wupeiqi,yuanhao,liwenzhou,jingliyang,jinxin,成龙 | | 2 | 歪歪,丫丫,丁丁,星星,格格 | | 3 | 张野,程咬金,程咬银,程咬铜,程咬铁 | +-----------+--------------------------------------------------------------+ 3 rows in set (0.00 sec) mysql> select depart_id,count(id) from employee group by depart_id; +-----------+-----------+ | depart_id | count(id) | +-----------+-----------+ | 1 | 8 | | 2 | 5 | | 3 | 5 | +-----------+-----------+ 3 rows in set (0.01 sec) mysql> select depart_id,group_concat(id) from employee group by depart_id; +-----------+------------------+ | depart_id | group_concat(id) | +-----------+------------------+ | 1 | 1,2,3,4,5,6,7,8 | | 2 | 9,10,11,12,13 | | 3 | 14,15,16,17,18 | +-----------+------------------+ 3 rows in set (0.00 sec) mysql> select depart_id,count(id) from employee group by depart_id; +-----------+-----------+ | depart_id | count(id) | +-----------+-----------+ | 1 | 8 | | 2 | 5 | | 3 | 5 | +-----------+-----------+ 3 rows in set (0.00 sec) mysql> select depart_id,max(salary) from employee group by depart_id; +-----------+-------------+ | depart_id | max(salary) | +-----------+-------------+ | 1 | 1000000.31 | | 2 | 4000.33 | | 3 | 20000.00 | +-----------+-------------+ 3 rows in set (0.00 sec) mysql> select depart_id,min(salary) from employee group by depart_id; +-----------+-------------+ | depart_id | min(salary) | +-----------+-------------+ | 1 | 2100.00 | | 2 | 1000.37 | | 3 | 10000.13 | +-----------+-------------+ 3 rows in set (0.00 sec) mysql> select depart_id,sum(salary) from employee group by depart_id; +-----------+-------------+ | depart_id | sum(salary) | +-----------+-------------+ | 1 | 1070200.64 | | 2 | 13001.47 | | 3 | 84000.13 | +-----------+-------------+ 3 rows in set (0.00 sec) mysql> select depart_id,avg(salary) from employee group by depart_id; +-----------+---------------+ | depart_id | avg(salary) | +-----------+---------------+ | 1 | 133775.080000 | | 2 | 2600.294000 | | 3 | 16800.026000 | +-----------+---------------+ 3 rows in set (0.00 sec)
建立表之间的关系
创建表 语法: create table 表名( 字段名1 类型[(宽度) 约束条件], 字段名2 类型[(宽度) 约束条件], 字段名3 类型[(宽度) 约束条件] ); 注意: 1. 在同一张表中,字段名是不能相同 2. 宽度和约束条件可选 3. 字段名和类型是必须的 永久解决编码问题 #1. 修改配置文件 [mysqld] default-character-set=utf8 [client] default-character-set=utf8 [mysql] default-character-set=utf8 #mysql5.5以上:修改方式有所改动 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set=utf8 [mysql] default-character-set=utf8 #2. 重启服务 #3. 查看修改结果: \s show variables like '%char%' 注意注意注意:表中的最后一个字段不要加逗号 1 多对一:左边表的多条记录对应右面表的唯一一条记录 create table dep( id int primary key auto_increment, #被关联的字段必须保证是唯一的 name varchar(20), comment varchar(50) ); create table emp( id int primary key auto_increment, name varchar(20), dep_id int, #关联的字段,一定要保证是可以重复的 constraint fk_depid_id foreign key(dep_id) references dep(id) on update cascade on delete cascade ); 建立多对一的关系需要注意 1 先建立被关联的表,被关联的字段必须保证是唯一的 2 再创建关联的表,关联的字段,一定要保证是可以重复的 ps:关联的字段一定是来自于表关联的表对应字段的值 2 一对一的关系: create table user( uid int primary key auto_increment, name varchar(15) ); insert into user(name) values ('egon1'), ('egon2'), ('egon3'), ('egon4'), ('egon5'), ('egon6'); create table admin( id int primary key auto_increment, user_id int unique, password varchar(20), foreign key(user_id) references user(uid) on update cascade on delete cascade ); insert into admin(user_id,password) values (3,'alex3714'), (5,'alex371asdf4');
乱乱的练习
create table class( cid int primary key auto_increment, caption varchar(20) ); create table student( sid int primary key auto_increment, sname varchar(20), gender varchar(20), class_id int, foreign key(class_id) references class(cid) on delete cascade on update cascade )engine=innodb; create table teacher( tid int primary key auto_increment, tname varchar(20) ); create table course( cid int primary key auto_increment, cname varchar(10), teacher_id int, foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade )engine=innodb; create table score( sid int primary key auto_increment, student_id int not null, corse_id int not null, number int ); insert into class(caption) values('三年二班'),('一年三班'),('三年一班'); insert into student(sname,gender,class_id) values('钢蛋','女',1), ('铁锤','女',1), ('山炮','男',2); insert into teacher(tname) values('刘老师'),('田老师'),('林老师'); insert into course(cname,teacher_id) values('生物',1),('体育',1),('物理',2); insert into score values(1,1,1,60),(2,1,2,59),(3,2,2,100); 练习:账号信息表,用户组,主机表,主机组 复制代码 #用户表 create table user( id int not null unique auto_increment, username varchar(20) not null, password varchar(50) not null, primary key(username,password) ); insert into user(username,password) values ('root','123'), ('egon','456'), ('alex','alex3714') ; #用户组表 create table usergroup( id int primary key auto_increment, groupname varchar(20) not null unique ); insert into usergroup(groupname) values ('IT'), ('Sale'), ('Finance'), ('boss') ; #主机表 create table host( id int primary key auto_increment, ip char(15) not null unique default '127.0.0.1' ); insert into host(ip) values ('172.16.45.2'), ('172.16.31.10'), ('172.16.45.3'), ('172.16.31.11'), ('172.10.45.3'), ('172.10.45.4'), ('172.10.45.5'), ('192.168.1.20'), ('192.168.1.21'), ('192.168.1.22'), ('192.168.2.23'), ('192.168.2.223'), ('192.168.2.24'), ('192.168.3.22'), ('192.168.3.23'), ('192.168.3.24') ; #业务线表 create table business( id int primary key auto_increment, business varchar(20) not null unique ); insert into business(business) values ('轻松贷'), ('随便花'), ('大富翁'), ('穷一生') ; #建关系:user与usergroup create table user2usergroup( id int not null unique auto_increment, user_id int not null, group_id int not null, primary key(user_id,group_id), foreign key(user_id) references user(id), foreign key(group_id) references usergroup(id) ); insert into user2usergroup(user_id,group_id) values (1,1), (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) ; #建关系:host与business create table host2business( id int not null unique auto_increment, host_id int not null, business_id int not null, primary key(host_id,business_id), foreign key(host_id) references host(id), foreign key(business_id) references business(id) ); insert into host2business(host_id,business_id) values (1,1), (1,2), (1,3), (2,2), (2,3), (3,4) ; #建关系:user与host create table user2host( id int not null unique auto_increment, user_id int not null, host_id int not null, primary key(user_id,host_id), foreign key(user_id) references user(id), foreign key(host_id) references host(id) ); insert into user2host(user_id,host_id) values (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (1,11), (1,12), (1,13), (1,14), (1,15), (1,16), (2,2), (2,3), (2,4), (2,5), (3,10), (3,11), (3,12) employee 表 create table employee( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) unsigned not null default 28, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, #一个部门一个屋子 depart_id int ); insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values ('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部 ('alex','male',78,'20150302','teacher',1000000.31,401,1), ('wupeiqi','male',81,'20130305','teacher',8300,401,1), ('yuanhao','male',73,'20140701','teacher',3500,401,1), ('liwenzhou','male',28,'20121101','teacher',2100,401,1), ('jingliyang','female',18,'20110211','teacher',9000,401,1), ('jinxin','male',18,'19000301','teacher',30000,401,1), ('成龙','male',48,'20101111','teacher',10000,401,1), ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 ('丫丫','female',38,'20101101','sale',2000.35,402,2), ('丁丁','female',18,'20110312','sale',1000.37,402,2), ('星星','female',18,'20160513','sale',3000.29,402,2), ('格格','female',28,'20170127','sale',4000.33,402,2), ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门 ('程咬金','male',18,'19970312','operation',20000,403,3), ('程咬银','female',18,'20130311','operation',19000,403,3), ('程咬铜','male',18,'20150411','operation',18000,403,3), ('程咬铁','female',18,'20140512','operation',17000,403,3) ;