• 【SQL数据库】数据库的创建、查询、插入等操作使用方法(结合黑皮书教材网站(db-book中的例子)在MySQL Workbench和shell中实现查询操作


    SQL 数据库的创建、查询、插入等操作使用方法(结合黑皮书教材网站中的例子)

    注:数据来源: https://www.db-book.com/university-lab-dir/sample_tables-dir/index.html

    结合网站: https://www.db-book.com/university-lab-dir/exercises-dir/

    image-20220907182528649

    I. 进入SQL Server

    在安装好SQL之后,输入以下命令,并输入mysql密码:

    mysql -uroot -p
    
    • 1

    格式:

    mysql -h 主机地址 -u 用户名 -p 用户密码
    
    • 1

    II.基本SQL语句

    yourdatabasename 指代你所创建的数据库名称

    创建数据库

    create database yourdatabasename;
    
    • 1

    查看已有的数据库

    show databases;
    
    • 1

    使用数据库

    use yourdatabasename;
    
    • 1

    删除数据库中表的数据(需要先use数据库)

    delete from yourtablename;
    
    • 1

    查看表的指定列的内容:

    show columns from yourtablename;
    
    • 1

    (更多语句,请查阅手册)

    III.DEMO实例

    数据来源: https://www.db-book.com/university-lab-dir/sample_tables-dir/index.html

    1. 初始化工作:

    以University数据库为例:

    按次序输入:

    创建数据库University

    create database University;
    
    • 1

    使用数据库University

    use University;
    
    • 1

    以下有两种方法导入下面文件sql文件;

    • MySQL Workbench

    直接打开文件运行

    image-20220907175725651

    • 直接在命令行中输入或者使用source命令

      source sql文件路径
      
      • 1

    DDL.sql (在University数据库中创建表)

    use University;
    create table classroom
    	(building		varchar(15),
    	 room_number		varchar(7),
    	 capacity		numeric(4,0),
    	 primary key (building, room_number)
    	);
    
    create table department
    	(dept_name		varchar(20), 
    	 building		varchar(15), 
    	 budget		        numeric(12,2) check (budget > 0),
    	 primary key (dept_name)
    	);
    
    create table course
    	(course_id		varchar(8), 
    	 title			varchar(50), 
    	 dept_name		varchar(20),
    	 credits		numeric(2,0) check (credits > 0),
    	 primary key (course_id),
    	 foreign key (dept_name) references department (dept_name)
    		on delete set null
    	);
    
    create table instructor
    	(ID			varchar(5), 
    	 name			varchar(20) not null, 
    	 dept_name		varchar(20), 
    	 salary			numeric(8,2) check (salary > 29000),
    	 primary key (ID),
    	 foreign key (dept_name) references department (dept_name)
    		on delete set null
    	);
    
    create table section
    	(course_id		varchar(8), 
             sec_id			varchar(8),
    	 semester		varchar(6)
    		check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), 
    	 year			numeric(4,0) check (year > 1701 and year < 2100), 
    	 building		varchar(15),
    	 room_number		varchar(7),
    	 time_slot_id		varchar(4),
    	 primary key (course_id, sec_id, semester, year),
    	 foreign key (course_id) references course (course_id)
    		on delete cascade,
    	 foreign key (building, room_number) references classroom (building, room_number)
    		on delete set null
    	);
    
    create table teaches
    	(ID			varchar(5), 
    	 course_id		varchar(8),
    	 sec_id			varchar(8), 
    	 semester		varchar(6),
    	 year			numeric(4,0),
    	 primary key (ID, course_id, sec_id, semester, year),
    	 foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
    		on delete cascade,
    	 foreign key (ID) references instructor (ID)
    		on delete cascade
    	);
    
    create table student
    	(ID			varchar(5), 
    	 name			varchar(20) not null, 
    	 dept_name		varchar(20), 
    	 tot_cred		numeric(3,0) check (tot_cred >= 0),
    	 primary key (ID),
    	 foreign key (dept_name) references department (dept_name)
    		on delete set null
    	);
    
    create table takes
    	(ID			varchar(5), 
    	 course_id		varchar(8),
    	 sec_id			varchar(8), 
    	 semester		varchar(6),
    	 year			numeric(4,0),
    	 grade		        varchar(2),
    	 primary key (ID, course_id, sec_id, semester, year),
    	 foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
    		on delete cascade,
    	 foreign key (ID) references student (ID)
    		on delete cascade
    	);
    
    create table advisor
    	(s_ID			varchar(5),
    	 i_ID			varchar(5),
    	 primary key (s_ID),
    	 foreign key (i_ID) references instructor (ID)
    		on delete set null,
    	 foreign key (s_ID) references student (ID)
    		on delete cascade
    	);
    
    create table time_slot
    	(time_slot_id		varchar(4),
    	 day			varchar(1),
    	 start_hr		numeric(2) check (start_hr >= 0 and start_hr < 24),
    	 start_min		numeric(2) check (start_min >= 0 and start_min < 60),
    	 end_hr			numeric(2) check (end_hr >= 0 and end_hr < 24),
    	 end_min		numeric(2) check (end_min >= 0 and end_min < 60),
    	 primary key (time_slot_id, day, start_hr, start_min)
    	);
    
    create table prereq
    	(course_id		varchar(8), 
    	 prereq_id		varchar(8),
    	 primary key (course_id, prereq_id),
    	 foreign key (course_id) references course (course_id)
    		on delete cascade,
    	 foreign key (prereq_id) references course (course_id)
    	);
    
    show tables;
    
    • 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
    2.插入数据

    以下sql代码用于对指定表插入数据

    smallRelationInsertFile.sql

    use University;
    delete from prereq;
    delete from time_slot;
    delete from advisor; 
    delete from takes;
    delete from student;
    delete from teaches;	
    delete from section;
    delete from instructor;
    delete from course;
    delete from department;
    delete from classroom;
    insert into classroom values ('Packard', '101', '500');
    insert into classroom values ('Painter', '514', '10');
    insert into classroom values ('Taylor', '3128', '70');
    insert into classroom values ('Watson', '100', '30');
    insert into classroom values ('Watson', '120', '50');
    insert into department values ('Biology', 'Watson', '90000');
    insert into department values ('Comp. Sci.', 'Taylor', '100000');
    insert into department values ('Elec. Eng.', 'Taylor', '85000');
    insert into department values ('Finance', 'Painter', '120000');
    insert into department values ('History', 'Painter', '50000');
    insert into department values ('Music', 'Packard', '80000');
    insert into department values ('Physics', 'Watson', '70000');
    insert into course values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
    insert into course values ('BIO-301', 'Genetics', 'Biology', '4');
    insert into course values ('BIO-399', 'Computational Biology', 'Biology', '3');
    insert into course values ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
    insert into course values ('CS-190', 'Game Design', 'Comp. Sci.', '4');
    insert into course values ('CS-315', 'Robotics', 'Comp. Sci.', '3');
    insert into course values ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
    insert into course values ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
    insert into course values ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
    insert into course values ('FIN-201', 'Investment Banking', 'Finance', '3');
    insert into course values ('HIS-351', 'World History', 'History', '3');
    insert into course values ('MU-199', 'Music Video Production', 'Music', '3');
    insert into course values ('PHY-101', 'Physical Principles', 'Physics', '4');
    insert into instructor values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
    insert into instructor values ('12121', 'Wu', 'Finance', '90000');
    insert into instructor values ('15151', 'Mozart', 'Music', '40000');
    insert into instructor values ('22222', 'Einstein', 'Physics', '95000');
    insert into instructor values ('32343', 'El Said', 'History', '60000');
    insert into instructor values ('33456', 'Gold', 'Physics', '87000');
    insert into instructor values ('45565', 'Katz', 'Comp. Sci.', '75000');
    insert into instructor values ('58583', 'Califieri', 'History', '62000');
    insert into instructor values ('76543', 'Singh', 'Finance', '80000');
    insert into instructor values ('76766', 'Crick', 'Biology', '72000');
    insert into instructor values ('83821', 'Brandt', 'Comp. Sci.', '92000');
    insert into instructor values ('98345', 'Kim', 'Elec. Eng.', '80000');
    insert into section values ('BIO-101', '1', 'Summer', '2017', 'Painter', '514', 'B');
    insert into section values ('BIO-301', '1', 'Summer', '2018', 'Painter', '514', 'A');
    insert into section values ('CS-101', '1', 'Fall', '2017', 'Packard', '101', 'H');
    insert into section values ('CS-101', '1', 'Spring', '2018', 'Packard', '101', 'F');
    insert into section values ('CS-190', '1', 'Spring', '2017', 'Taylor', '3128', 'E');
    insert into section values ('CS-190', '2', 'Spring', '2017', 'Taylor', '3128', 'A');
    insert into section values ('CS-315', '1', 'Spring', '2018', 'Watson', '120', 'D');
    insert into section values ('CS-319', '1', 'Spring', '2018', 'Watson', '100', 'B');
    insert into section values ('CS-319', '2', 'Spring', '2018', 'Taylor', '3128', 'C');
    insert into section values ('CS-347', '1', 'Fall', '2017', 'Taylor', '3128', 'A');
    insert into section values ('EE-181', '1', 'Spring', '2017', 'Taylor', '3128', 'C');
    insert into section values ('FIN-201', '1', 'Spring', '2018', 'Packard', '101', 'B');
    insert into section values ('HIS-351', '1', 'Spring', '2018', 'Painter', '514', 'C');
    insert into section values ('MU-199', '1', 'Spring', '2018', 'Packard', '101', 'D');
    insert into section values ('PHY-101', '1', 'Fall', '2017', 'Watson', '100', 'A');
    insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2017');
    insert into teaches values ('10101', 'CS-315', '1', 'Spring', '2018');
    insert into teaches values ('10101', 'CS-347', '1', 'Fall', '2017');
    insert into teaches values ('12121', 'FIN-201', '1', 'Spring', '2018');
    insert into teaches values ('15151', 'MU-199', '1', 'Spring', '2018');
    insert into teaches values ('22222', 'PHY-101', '1', 'Fall', '2017');
    insert into teaches values ('32343', 'HIS-351', '1', 'Spring', '2018');
    insert into teaches values ('45565', 'CS-101', '1', 'Spring', '2018');
    insert into teaches values ('45565', 'CS-319', '1', 'Spring', '2018');
    insert into teaches values ('76766', 'BIO-101', '1', 'Summer', '2017');
    insert into teaches values ('76766', 'BIO-301', '1', 'Summer', '2018');
    insert into teaches values ('83821', 'CS-190', '1', 'Spring', '2017');
    insert into teaches values ('83821', 'CS-190', '2', 'Spring', '2017');
    insert into teaches values ('83821', 'CS-319', '2', 'Spring', '2018');
    insert into teaches values ('98345', 'EE-181', '1', 'Spring', '2017');
    insert into student values ('00128', 'Zhang', 'Comp. Sci.', '102');
    insert into student values ('12345', 'Shankar', 'Comp. Sci.', '32');
    insert into student values ('19991', 'Brandt', 'History', '80');
    insert into student values ('23121', 'Chavez', 'Finance', '110');
    insert into student values ('44553', 'Peltier', 'Physics', '56');
    insert into student values ('45678', 'Levy', 'Physics', '46');
    insert into student values ('54321', 'Williams', 'Comp. Sci.', '54');
    insert into student values ('55739', 'Sanchez', 'Music', '38');
    insert into student values ('70557', 'Snow', 'Physics', '0');
    insert into student values ('76543', 'Brown', 'Comp. Sci.', '58');
    insert into student values ('76653', 'Aoi', 'Elec. Eng.', '60');
    insert into student values ('98765', 'Bourikas', 'Elec. Eng.', '98');
    insert into student values ('98988', 'Tanaka', 'Biology', '120');
    insert into takes values ('00128', 'CS-101', '1', 'Fall', '2017', 'A');
    insert into takes values ('00128', 'CS-347', '1', 'Fall', '2017', 'A-');
    insert into takes values ('12345', 'CS-101', '1', 'Fall', '2017', 'C');
    insert into takes values ('12345', 'CS-190', '2', 'Spring', '2017', 'A');
    insert into takes values ('12345', 'CS-315', '1', 'Spring', '2018', 'A');
    insert into takes values ('12345', 'CS-347', '1', 'Fall', '2017', 'A');
    insert into takes values ('19991', 'HIS-351', '1', 'Spring', '2018', 'B');
    insert into takes values ('23121', 'FIN-201', '1', 'Spring', '2018', 'C+');
    insert into takes values ('44553', 'PHY-101', '1', 'Fall', '2017', 'B-');
    insert into takes values ('45678', 'CS-101', '1', 'Fall', '2017', 'F');
    insert into takes values ('45678', 'CS-101', '1', 'Spring', '2018', 'B+');
    insert into takes values ('45678', 'CS-319', '1', 'Spring', '2018', 'B');
    insert into takes values ('54321', 'CS-101', '1', 'Fall', '2017', 'A-');
    insert into takes values ('54321', 'CS-190', '2', 'Spring', '2017', 'B+');
    insert into takes values ('55739', 'MU-199', '1', 'Spring', '2018', 'A-');
    insert into takes values ('76543', 'CS-101', '1', 'Fall', '2017', 'A');
    insert into takes values ('76543', 'CS-319', '2', 'Spring', '2018', 'A');
    insert into takes values ('76653', 'EE-181', '1', 'Spring', '2017', 'C');
    insert into takes values ('98765', 'CS-101', '1', 'Fall', '2017', 'C-');
    insert into takes values ('98765', 'CS-315', '1', 'Spring', '2018', 'B');
    insert into takes values ('98988', 'BIO-101', '1', 'Summer', '2017', 'A');
    insert into takes values ('98988', 'BIO-301', '1', 'Summer', '2018', null);
    insert into advisor values ('00128', '45565');
    insert into advisor values ('12345', '10101');
    insert into advisor values ('23121', '76543');
    insert into advisor values ('44553', '22222');
    insert into advisor values ('45678', '22222');
    insert into advisor values ('76543', '45565');
    insert into advisor values ('76653', '98345');
    insert into advisor values ('98765', '98345');
    insert into advisor values ('98988', '76766');
    insert into time_slot values ('A', 'M', '8', '0', '8', '50');
    insert into time_slot values ('A', 'W', '8', '0', '8', '50');
    insert into time_slot values ('A', 'F', '8', '0', '8', '50');
    insert into time_slot values ('B', 'M', '9', '0', '9', '50');
    insert into time_slot values ('B', 'W', '9', '0', '9', '50');
    insert into time_slot values ('B', 'F', '9', '0', '9', '50');
    insert into time_slot values ('C', 'M', '11', '0', '11', '50');
    insert into time_slot values ('C', 'W', '11', '0', '11', '50');
    insert into time_slot values ('C', 'F', '11', '0', '11', '50');
    insert into time_slot values ('D', 'M', '13', '0', '13', '50');
    insert into time_slot values ('D', 'W', '13', '0', '13', '50');
    insert into time_slot values ('D', 'F', '13', '0', '13', '50');
    insert into time_slot values ('E', 'T', '10', '30', '11', '45 ');
    insert into time_slot values ('E', 'R', '10', '30', '11', '45 ');
    insert into time_slot values ('F', 'T', '14', '30', '15', '45 ');
    insert into time_slot values ('F', 'R', '14', '30', '15', '45 ');
    insert into time_slot values ('G', 'M', '16', '0', '16', '50');
    insert into time_slot values ('G', 'W', '16', '0', '16', '50');
    insert into time_slot values ('G', 'F', '16', '0', '16', '50');
    insert into time_slot values ('H', 'W', '10', '0', '12', '30');
    insert into prereq values ('BIO-301', 'BIO-101');
    insert into prereq values ('BIO-399', 'BIO-101');
    insert into prereq values ('CS-190', 'CS-101');
    insert into prereq values ('CS-315', 'CS-101');
    insert into prereq values ('CS-319', 'CS-101');
    insert into prereq values ('CS-347', 'CS-101');
    insert into prereq values ('EE-181', 'PHY-101');
    
    
    • 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

    对于MySQL Workbench用户,你可能需要关闭以下选项:

    因为在sql文件的delete语句中,出于安全因素考虑无法删除空表内容

    image-20220907175811581

    IV.对表进行查询

    我们使用如下语句:

    select * from instructor
    select name from instructor where dept_name = 'Comp. Sci.' and salary > 70000
    select * from instructor, department where instructor.dept_name = department.dept_name
    
    • 1
    • 2
    • 3

    可以得到:

    image-20220907180340034

    此外,还有另一个数据量比较大的sql代码:

    点击下载 largeRelationsInsertFile.sql

    按照如上操作插入数据之后,再执行相同的Query操作可得:

    image-20220907170753537

    image-20220907182219518

    image-20220907182230157

    以上就是全部内容,如果有任何疑问,欢迎评论区留言。

  • 相关阅读:
    springboot整合ldap
    第六章 机器学习技巧——参数的更新&权重的初始值&Batch Normalization&正则化&超参数的验证
    网络安全(黑客)自学
    Linux环境下Nginx安装及Ubuntu Server 15.0.4尝试安装Nginx
    Mozilla Firefox侧边栏和垂直标签在131 Nightly版本中开始试用
    视频批量剪辑工具,自定义视频速率,批量剪辑工具助力创意无限”
    ELF文件格式-笔记
    Cloud Native=Cloud+Native 理解云原生
    部分依赖图(Partial Dependence Plots)以及实战-疾病引起原因解释
    day01 快速认识mongodb以及基本的增删改查
  • 原文地址:https://blog.csdn.net/m0_52387305/article/details/126751411