1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
|
show databases;
select database();
create database if not exists itcast;
drop database if exists itcast;
use itcast;
create table tb_user ( id int comment '编号', name varchar(50) comment '姓名', age int comment '年龄', gender varchar(1) comment '性别' ) comment '用户表';
desc tb_user;
show create table tb_user;
drop table emp;
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 '员工表';
desc emp;
alter table emp add nickname varchar(20) comment '昵称';
alter table emp modify age tinyint;
alter table emp change nickname username varchar(30) comment '用户名';
alter table emp drop username;
alter table emp rename to employee;
drop table if exists employee;
truncate table employee;
select database(); show tables;
insert into employee(id, workno, name, gender, age, idcard, entrydate) value (1, '1', '小华', '男', 20, '123456789123456789', '2001-01-01');
select * from employee;
insert into employee value (2, '2', '小刚', '男', 23, '123456784124456789', '2008-01-01');
insert into employee values (3, '3', '小李', '男', 23, '123456784124456789', '2008-01-01'), (4, '4', '小黄', '男', 23, '123456784124456789', '2008-01-01');
update employee set name = '张三' where id = 1; update employee set name = '李四', gender= '女' where id = 1;
delete from employee where id = 4;
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 '身份证号', workaddress varchar(50) comment '工作地址', entrydate date comment '入职时间' ) comment '员工表';
insert into emp(id, workno, name, gender, age, idcard, workaddress, entrydate) values (1, '1', '柳岩', '女', 20, '12345678912345678', '北京', '2001-01-01'), (2, '2', '张无忌', '男', 18, '123456789012345670', '北京', '2005-09-01'), (3, '3', '韦一笑', '男', 38, '12345678972345670', '上海', '2005-08-01'), (4, '4', '赵敏', '女', 18, '12345675712345670', '北京', '2009-12-01'), (5, '5', '小昭', '女', 16, '123456769012345678', '上海', '2007-07-01'), (6, '6', '杨逍', '男', 28, '1234567893123456X', '北京', '2006-01-01'), (7, '7', '范瑶', '男', 40, '123456789212345670', '北京', '2005-05-01'), (8, '8', '黛绮丝', '女', 38, '123456157123645670', '天津', '2015-05-01'), (9, '9', '范凉凉', '女', 45, '123156789012345678', '北京', '2010-04-01'), (10, '10', '陈友谅', '男', 53, '123456789012345670', '上海', '2011-01-01'), (11, '11', '张士诚', '男', 55, '12356789712345670', '江苏', '2015-05-01'), (12, '12', '常遇春', '男', 32, '123446757152345670', '北京', '2004-02-01'), (13, '13', '张三丰', '男', 88, '123656789012345678', '江苏', '2020-11-01'), (14, '14', '灭绝', '女', 65, '123456719012345670', '西安', '2019-05-01'), (15, '15', '胡青年', '男', 70, '12345674971234567X', '西安', '2018-04-01'), (16, '16', '周芷若', '女', 18, null, '北京', '2012-06-01');
select * from emp;
select name, workno, age from emp;
select id, workno, name, gender, age, idcard, workaddress, entrydate from emp;
select distinct workaddress '工作地址' from emp;
select * from emp where age = 88;
select * from emp where age < 20;
select * from emp where age <= 20;
select * from emp where idcard is null;
select * from emp where age != 88; select * from emp where age <> 88;
select * from emp where age >= 15 && age <= 20; select * from emp where age >= 15 and age <= 20; select * from emp where age between 15 and 20; #包含了15和20
select * from emp where gender = '女' and age < 25;
select * from emp where age = 18 or age = 20 or age = 48; select * from emp where age in (18, 20, 48);
select * from emp where name like '__';
select * from emp where idcard like '%X';
select count(*) from emp; select count(idcard) from emp;
select avg(age) from emp;
select max(age) from emp;
select min(age) from emp;
select sum(age) from emp where workaddress = '西安';
select gender, count(*) from emp group by gender;
select gender, avg(age) from emp group by gender;
select workaddress, count(*) address_count from emp where age < 45 group by workaddress having address_count >= 3;
select * from emp order by age;
select * from emp order by age desc;
select * from emp order by entrydate desc;
select * from emp order by age asc, entrydate desc;
select * from emp limit 0,10;
select * from emp limit 10,10;
select * from emp where gender = '女' and age in (20, 21, 22, 23);
select * from emp where gender = '男' and (age between 20 and 40) and name like '___';
select gender, count(*) from emp where age < 60 group by gender;
select name, age from emp where age <= 35 order by age asc, entrydate desc;
select * from emp where gender = '男' and age between 20 and 40 order by age, entrydate limit 5;
use mysql;
select * from mysql.user;
create user 'itcast'@'localhost' identified by '123456';
create user 'heima'@'%' identified by '123456';
alter user 'heima'@'%' identified with mysql_native_password by '1234';
drop user 'itcast'@'localhost';
show grants for 'heima'@'%';
grant all on itcast.* to 'heima'@'%';
grant all on *.* to 'heima'@'%';
revoke all on itcast.* from 'heima'@'%';
select concat('hello ', 'word'); # hello word
select lower('Hello'); # hello
select upper('Hello'); # HELLO
select lpad('01', 5, '-'); #
select rpad('01', 5, '-'); # 01
select trim(' Hello Mysql '); # Hello Mysql
select substring('Hello Mysql', 1, 5); # Hello
update emp set workno = lpad(workno, 5, '0');
select ceil(1.5); # 2
select floor(1.1); # 1
select mod(7, 4); # 3
select rand();
select round(2.345, 2); # 2.35
select lpad(round(rand() * 1000000, 0), 6, '0');
select curdate();
select curtime();
select now();
select year(now()); select month(now()); select day(now());
select date_add(now(), INTERVAL 70 DAY); # 当前的时间往后推70天 select date_add(now(), INTERVAL 2 MONTH); #向后推2个月
select datediff('2021-12-01', '2021-10-01'); # 查询两个时间之间的差值,第一个时间减去第二个时间
select name, datediff(curdate(), entrydate) 'entrydays' from emp order by entrydays desc;
select if(true, 'OK', 'ERROR');
select ifnull('OK', 'DEFAULT'); select ifnull(null, 'DEFAULT');
select name, (case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作地址' from emp;
create table score ( id int comment 'ID', name varchar(20) comment '姓名', math int comment '数学', english int comment '英语', chinese int comment '语文' ) comment '学员成绩表'; insert into score(id, name, math, english, chinese) VALUES (1, 'Tom', 67, 88, 95), (2, 'Rose', 23, 66, 90), (3, 'Jack', 56, 98, 76);
select * from score;
select id, name, (case when math >= 85 then '优秀' when math >= 60 then '及格' else '不及格' end) '数学', (case when english >= 85 then '优秀' when english >= 60 then '及格' else '不及格' end) '英语', (case when chinese >= 85 then '优秀' when chinese >= 60 then '及格' else '不及格' end) '语文' from score;
drop table if exists user; create table user ( id int AUTO_INCREMENT PRIMARY KEY COMMENT 'ID唯一标识', name varchar(10) NOT NULL UNIQUE COMMENT '姓名', age int COMMENT '年龄', status char(1) default '1' COMMENT '状态', gender char(1) COMMENT '性别' ); select * from user;
insert into user(name, age, status, gender) values ('Tom1', 19, '1', '男'), ('Tom2', 29, '1', '男'), ('Tom3', 14, '1', '男');
create table dept ( id int auto_increment comment 'ID' primary key, name varchar(50) not null comment '部门名称' ) comment '部门表';
INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'), (3, '财务部'), (4, '销售部'), (5, '总经办');
create table employee ( id int auto_increment comment 'ID' primary key, 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 employee (id, name, age, job, salary, entrydate, managerid, dept_id) VALUES (1, '金庸', 66, '总裁', 20000, '2000-01-01', null, 5), (2, '张无忌', 20, '项目经理', 12500, '2005-12-05', 1, 1), (3, '杨逍', 33, '开发', 8400, '2000-11-03', 2, 1), (4, '韦一笑', 48, '开发', 11000, '2002-02-05', 2, 1), (5, '常遇春', 43, '开发', 10500, '2004-09-07', 3, 1), (6, '小昭', 19, '程序员鼓励师', 6600, '2004-10-12', 2, 1);
select * from dept; select * from employee;
alter table employee add constraint fk_emp_dept foreign key (dept_id) references dept (id);
alter table employee drop foreign key fk_emp_dept;
create table student ( id int auto_increment primary key comment '主键ID', name varchar(10) comment '姓名', no varchar(10) comment '学号' ) comment '学生表';
insert into student values (null, '黛绮丝', '2000100101'), (null, '谢逊', '2000100102'), (null, '殷天正', '2000100103'), (null, '韦一笑', '2000100104');
create table course ( id int auto_increment primary key comment '主键ID', name varchar(10) comment '课程名称' ) comment '课程表';
insert into course values (null, 'Java'), (null, 'PHP'), (null, 'MySQL'), (null, 'Hadoop');
create table student_course ( id int auto_increment comment '主键' primary key, studentid int not null comment '学生ID', courseid int not null comment '课程ID', constraint fk_courseid foreign key (courseid) references course (id), constraint fk_studentid foreign key (studentid) references student (id) ) comment '学生课程中间表';
insert into student_course values (null, 1, 1), (null, 1, 2), (null, 1, 3), (null, 2, 2), (null, 2, 3), (null, 3, 4);
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_userid foreign key (userid) references tb_user (id) ) comment '用户教育信息表'; insert into tb_user(id, name, age, gender, phone) values (null, '黄渤', 45, '1', '18800001111'), (null, '冰冰', 35, '2', '18800002222'), (null, '码云', 55, '1', '18800008888'), (null, '李彦宏', 50, '1', '18800009999'); insert into tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) values (null, '本科', '舞蹈', '静安区第一小学', '静安区第一中学', '北京舞蹈学院', 1), (null, '硕士', '表演', '朝阳区第一小学', '朝阳区第一中学', '北京电影学院', 2), (null, '本科', '英语', '杭州市第一小学', '杭州市第一中学', '杭州师范大学', 3), (null, '本科', '应用数学', '阳泉第一小学', '阳泉区第一中学', '清华大学', 4);
create table dept ( id int auto_increment comment 'ID' primary key, name varchar(50) not null comment '部门名称' ) comment '部门表'; INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'), (3, '财务部'), (4, '销售部'), (5, '总经办'), (6, '人事部');
create table emp ( id int auto_increment comment 'ID' primary key, 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 '员工表';
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept (id);
INSERT INTO emp (id, name, age, job, salary, entrydate, managerid, dept_id) VALUES (1, '金庸', 66, '总裁', 20000, '2000-01-01', null, 5), (2, '张无忌', 20, '项目经理', 12500, '2005-12-05', 1, 1), (3, '杨逍', 33, '开发', 8400, '2000-11-03', 2, 1), (4, '韦一笑', 48, '开发', 11000, '2002-02-05', 2, 1), (5, '常遇春', 43, '开发', 10500, '2004-09-07', 3, 1), (6, '小昭', 19, '程序员鼓励师', 6600, '2004-10-12', 2, 1), (7, '灭绝', 60, '财务总监', 8500, '2002-09-12', 1, 3), (8, '周芷若', 19, '会计', 48000, '2006-06-02', 7, 3), (9, '丁敏君', 23, '出纳', 5250, '2009-05-13', 7, 3), (10, '赵敏', 20, '市场部总监', 12500, '2004-10-12', 1, 2), (11, '鹿杖客', 56, '职员', 3750, '2006-10-03', 10, 2), (12, '鹤笔翁', 19, '职员', 3750, '2007-05-09', 10, 2), (13, '方东白', 19, '职员', 5500, '2009-02-12', 10, 2), (14, '张三丰', 88, '销售总监', 14000, '2004-10-12', 1, 4), (15, '俞莲舟', 38, '销售', 4600, '2004-10-12', 14, 4), (16, '宋远桥', 40, '销售', 4600, '2004-10-12', 14, 4), (17, '陈友谅', 42, null, 2000, '2011-10-12', 1, null);
select * from emp, dept where emp.dept_id = dept.id;
select e.name, d.name from emp e, dept d where e.dept_id = d.id;
select e.name, d.name from emp e inner join dept d on e.dept_id = d.id;
select e.name, d.name from emp e left join dept d on e.dept_id = d.id;
select e.name, d.name from emp e right join dept d on e.dept_id = d.id;
select e1.name '员工', e2.name '领导' from emp e1, emp e2 where e1.managerid = e2.id;
select e1.name '员工', e2.name '领导' from emp e1 left join emp e2 on e1.managerid = e2.id;
select * from emp e where e.salary < 5000 union select * from emp e where e.age > 50;
select * from emp e where e.dept_id = (select id from dept d where d.name = '销售部');
select * from emp e1 where e1.entrydate > (select e2.entrydate from emp e2 where e2.name = '方东白');
select * from emp e where e.dept_id in (select id from dept where dept.name = '销售部' or dept.name = '市场部');
select * from emp e where e.salary > all (select e2.salary from emp e2 where e2.dept_id = (select id from dept where dept.name = '财务部'));
select * from emp e where e.salary > any (select e2.salary from emp e2 where e2.dept_id = (select id from dept where dept.name = '研发部'));
select * from emp e2 where (e2.salary, e2.managerid) = (select e.salary, e.managerid from emp e where e.name = '张无忌');
select * from emp e2 where (e2.job, e2.salary) in (select e.job, e.salary from emp e where e.name = '鹿杖客' or name = '宋远桥');
select * from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;
create table salgrade ( grade int, losal int, hisal int ) comment '薪资等级表'; insert into salgrade values (1, 0, 3000); insert into salgrade values (2, 3001, 5000); insert into salgrade values (3, 5001, 8000); insert into salgrade values (4, 8001, 10000); insert into salgrade values (5, 10001, 15000); insert into salgrade values (6, 15001, 20000); insert into salgrade values (7, 20001, 25000); insert into salgrade values (8, 25001, 30000);
select e.name, e.age, e.job, d.name from emp e, dept d where e.dept_id = d.id;
select e.name, e.age, e.job, d.name from emp e inner join dept d on e.dept_id = d.id where e.age < 30;
select distinct d.id, d.name from emp e, dept d where e.dept_id = d.id;
select * from dept where dept.id in (select e.dept_id from emp e);
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where age > 40;
select e.name '姓名', s.grade '工资等级' from emp e, salgrade s where e.salary between s.losal and s.hisal;
select e.*, s.grade from emp e, dept d, salgrade s where e.dept_id = d.id and (e.salary between s.losal and s.hisal) and d.name = '研发部';
select e.*, s.grade from (select emp.* from emp, dept where emp.dept_id = dept.id and dept.name = '研发部') e, salgrade s where e.salary between s.losal and s.hisal;
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发部';
select * from emp where salary > (select salary from emp where name = '灭绝');
select * from emp where salary > (select avg(salary) from emp);
select * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);
select * from emp e, (select dept_id, avg(salary) 'salavg' from emp group by dept_id) eavg where eavg.dept_id = e.dept_id and e.salary < eavg.salavg;
select d.id, d.name, (select count(*) from emp e where e.dept_id = d.id) '人数' from dept d;
select * from dept d, (select e.dept_id, count(*) '人数' from emp e group by e.dept_id) ecount where d.id = ecount.dept_id;
select s.name, s.no, c.name from course c, student s, student_course sc where c.id = sc.courseid and s.id = sc.studentid;
drop table if exists account; create table account ( id int primary key AUTO_INCREMENT comment 'ID', name varchar(10) comment '姓名', money double(10, 2) comment '余额' ) comment '账户表'; insert into account(name, money) VALUES ('张三', 2000), ('李四', 2000); select * from account;
select * from account where name = '张三';
update account set money = money - 1000 where name = '张三';
update account set money = money + 1000 where name = '李四';
start transaction;
select * from account where name = '张三';
update account set money = money - 1000 where name = '张三';
update account set money = money + 1000 where name = '李四';
commit;
|