bitsCN.com
一、表的crud操作
指增加(Create)、查询(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)// select 查询后面再讲SQL Code
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
alter table t_emp modify ename varchar(30);
alter table t_emp drop esex;
alter table t_emp add esex char(2);
insert into t_emp(empno, ename, esex) values(1000, 'tom', 'm');
insert into t_emp values(1000, 'maggie', 'f'); /* 还没设置主键,故empno可以相同 */
insert into t_emp(empno, ename) values(1002, 'john');
insert into t_emp(empno, ename, esex) values(1003, null, 'm');
insert into t_emp values(1004, '张三', '男');
show variables like 'character_set%'; /* 查看字符集设置 */
set character_set_database=utf8; /* 也可设置配置文件 */
set names gbk; /* 支持插入中文 */
update t_emp set empno=1001 where ename='maggie';
delete from t_emp where esex is null;
delete from t_emp; /* 表结构还在 */
drop table t_emp; /* 整表删除 */
二、完整性约束表完整性约束
主键 (constraint)外键 (constraint)用户自定义完整性约束 (check)SQL Code
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
create table t_emp(empno int, ename varchar(20), esex char(2), primary key (empno));
create table t_emp(empno int, ename varchar(20), esex char(2), constraint PK_EMPNO primary key(empno)); /* 设置主键*/
create table t_emp(empno int, ename varchar(20), esex char(2));
alter table t_emp add constraint PK_EMPNO primary key(empno); /* 这种方式也可以设置主键 */
insert into t_emp values(1000, 'john', 'm');
insert into t_emp values(1000, 'lily', 'f'); /* error,empno不能相等 */
insert into t_emp values(null, 'lily', 'f'); /* error,主键不能为空 */
create table t_emp(empno int, deptno int, ename varchar(20), esex char(2));
alter table t_emp add constraint PK_EMPNO primary key(empno);
create table t_dept(deptno int, dname varchar(20));
alter table t_dept add constraint PK_DEPTNO primary key(deptno);
alter table t_emp add constraint FK_DEPTNO foreign key(deptno) references t_dept(deptno); /*设置t_emp 的外键为t_dept 的主键 */
set names gbk;
insert into t_dept values(2001, '人事部');
insert into t_dept values(2002, '技术部');
insert into t_emp values(1001, 2001, 'john', 'm');
insert into t_emp values(1003, 2003, 'john', 'm');
create table t_test1(id int auto_increment primary key, name varchar(30), age int default 20);
insert into t_test1 values(null, 'aaa');
insert into t_test1 values(null, 'aaa', null);
insert into t_test1 (name) values( 'bbb');
create table t_test2(id int, name varchar(30), age int);
alter table t_test2 add constraint CC_AGE check (age >=18 and age
alter table t_test2 add constraint CC_AGE check (length(name)>2);
三、select 查询
练习前先导入数据:create database scott;use scott;source C:/scott.sql scott.sql 点这下载
select 单表查询:
SQL Code
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
select * from emp;
SELECT empno as '工号',ename '姓名' FROM emp; /* as 后面是别名 */
SELECT empno, '暨南大学' FROM emp; /* 常量列查询 */
SELECT empno, concat(ename,'#') FROM emp; /* concat连接 */
SELECT empno, ename||'#' FROM emp; /* oracle可以用||作为连接符 */
SELECT empno, ename, job FROM emp WHERE ename = 'SMITH'
SELECT empno, ename, job FROM emp WHERE ename 'SMITH' /* 也可以使用!= */
SELECT empno, ename, sal FROM emp WHERE sal>= 1500
SELECT * FROM emp WHERE deptno=30 and sal>1500; /* and */
SELECT * FROM emp WHERE job='MANAGER' or job='SALESMAN' /* or */
SELECT * FROM emp where sal BETWEEN 800 and 1500;
SELECT * FROM emp where sal >= 800 and sal SELECT empno, ename, sal, comm FROM emp WHERE comm is null
SELECT empno, ename, sal, comm FROM emp WHERE comm is not null /* not */
SELECT * FROM emp where sal not BETWEEN 800 and 1500; /* between */
SELECT * FROM emp where ename in ('SMITH', 'KING'); /* in */
SELECT * FROM emp where ename like 'S%'; /* 模糊查询 通配符: ‘%’(0个多个字符); 通配符: ‘_’ (单个字符) */
SELECT * FROM emp where ename like 'S_ITH';
SELECT * FROM emp ORDER BY ename desc; /* order by 默认是升序 asc */
SELECT empno, ename, job FROM emp ORDER BY 2 desc;
SELECT * FROM emp ORDER BY job asc, sal desc;
SQL Code
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
select * from emp ORDER BY sal limit 0,5; /* 0表示offet, 5表示从0开始的5条记录*/
select * from emp ORDER BY sal limit 5,5;
select * from emp ORDER BY sal limit 10,5;
select job,deptno from emp;
select all job,deptno from emp; /* 默认是all */
select distinct job,deptno from emp; /* 去除重复记录 */
select * from dept where deptno in (SELECT DISTINCT deptno from emp); /* 查询有员工的部门信息 */
/* UNION (无重复并集):当执行UNION 时,自动去掉结果集中的重复行,并以第一列的结果进行升序排序。*/
select empno,ename,job from emp where job='SALESMAN'
union /* union即联合查询 */
select empno,ename,job from emp where job='MANAGER';
select empno,ename,job from emp where job='SALESMAN' or job='MANAGER' /* 比较结果 */
/* UNION ALL (有重复并集):不去掉重复行,并且不对结果集进行排序。*/
select job, sal from emp where empno=7902
union all
select job, sal from emp where empno=7788;
select job, sal from emp where empno=7902
union
select job, sal from emp where empno=7788;
select 多表查询:多表查询
交叉连接内连接自身连接外连接
自然连接左外连接右外连接全连接
交叉连接是不带WHERE子句的多表查询,它返回被连接的两个表所有数据行的笛卡尔积。返回到结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
内连接(等值连接):在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接表中的所有列,包括其中的重复列。
内连接(不等连接):在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值。这些运算符包括>、>=、、!
内连接(自身连接)
外连接(左连接):返回包括左表中的所有记录和右表中联结字段相等的记录;即左外连接就是在等值连接的基础上加上主表中的未匹配数据。
外连接(右连接):返回包括右表中的所有记录和左表中联结字段相等的记录;即右外连接是在等值连接的基础上加上被连接表的不匹配数据。
外连接(全连接):全外连接是在等值连接的基础上将左表和右表的未匹配数据都加上。
自然连接:在连接条件中使用等于(=)运算符比较被连接列的列值,但它使用选择列表指出查询结果集合中所包括的列,并删除连接表中的重复列。
SQL Code
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
SELECT * FROM emp INNER JOIN dept ON emp.deptno = dept.deptno; /*内连接(等值连接) */
select * from emp,dept where emp.deptno=dept.deptno;
select * from emp INNER JOIN dept on emp.deptno > dept.deptno; /* 内连接(不等连接)*/
select * from emp,dept where emp.deptno > dept.deptno;
select A.ename 员工, B.ename 领导 from emp A, emp B where A.mgr = B.empno; /*内连接(自身连接) */
SELECT * FROM emp INNER JOIN dept ON emp.deptno = dept.deptno;
select * from emp left join dept on emp.deptno=dept.deptno /*外连接(左连接) */
/* scott.sql并未设置emp表的外键为deptno,故这里可以插入在dept表中不存在的deptno值*/
/* 主要是为了演示左连接和右连接的区别 */
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (9999, 'XXXX', 'CLERK', 7782, '1982-01-23', 1300, null, 90);
select * from emp right outer join dept on emp.deptno=dept.deptno /* 外连接(右连接) */
select * from emp left join dept on emp.deptno=dept.deptno
/* 自然连接会合并deptno一项,而外连接不会 */
SELECT * FROM emp NATURAL JOIN dept;
SELECT * FROM emp NATURAL LEFT JOIN dept;
SELECT * FROM emp NATURAL RIGHT JOIN dept;
参考:
《数据库系统概论》
mysql 5.1 参考手册
bitsCN.com

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools