This article brings everyone the basic knowledge of MySQL to organize and share. Although the knowledge points are simple, there are many, which will make it easier for you to search in the future. I hope it will be helpful to everyone.
Structure Query Language (structured query language) is referred to as SQL. It was determined as the American standard for relational database language by the American National Standards Institute (ANSI), and was later adopted as the international standard for relational database language by the International Standards Organization (ISO). The database management system can manage the database through SQL; define and operate data, and maintain data integrity and security.
1. Simple and easy to learn, with strong operability
2. Most important database management systems support SQL
3. Highly Non-procedural; when using SQL to operate the database, most of the work is automatically completed by the DBMS
1. DDL (Data Definition Language) data definition language, used to operate the database , tables, columns, etc.; Commonly used statements: CREATE, ALTER, DROP
2. DML (Data Manipulation Language) data manipulation language, used to operate data in tables in the database; Commonly used statements: INSERT, UPDATE, DELETE
3. DCL (Data Control Language) data control language, used to operate access permissions and security levels; common statements: GRANT, DENY
4. DQL (Data Query Language) data query language, used to query data. Common statements: SELECT
1. First normal form (1NF) means that each column of the database table is an indivisible basic data line; that is to say: each column Values are atomic and cannot be subdivided.
2. The second normal form (2NF) is established on the basis of the first normal form (1NF). To satisfy the second normal form (2NF), you must first satisfy the first normal form (1NF). If the table has a single primary key, then the columns other than the primary key must be completely dependent on the primary key; if the table is a composite primary key, then the columns other than the primary key must be completely dependent on the primary key and cannot only rely on part of the primary key.
3. The third normal form (3NF) is established on the basis of the second normal form, that is, to satisfy the third normal form, the second normal form must first be satisfied. Third Normal Form (3NF) requires that non-primary key columns in the table must be directly related to the primary key but not indirectly; that is to say: non-primary key columns cannot be related to each other.
When using MySQL database to store data, different data types determine the way MySQL stores data. To this end, the MySQL database provides a variety of data types, including integer types, floating point types, fixed point types, date and time types, string types, binary... and other data types.
According to the different value ranges, the integer types in MySQL can be divided into 5 types, namely TINYINT, SMALUNT, MEDIUMINT, INT and BIGINT. The following figure lists the byte sizes and value ranges corresponding to different MySQL integer types. The most commonly used is the INT type,
Data type | Number of bytes | The value range of unsigned numbers | The value range of signed numbers |
---|---|---|---|
TINYINT | 1 | 0~255 | -128~127 |
SMALLINT | 2 | 0~65535 | -32768~32768 |
3 | 0~16777215 | -8388608 ~8388608 | |
4 | 0~4294967295 | -2147483648~ 2147483648 | |
8 | 0~18446744073709551615 | -9223372036854775808~9223372036854775808 |
Number of bytes | Signed value range | Unsigned value range | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4 | -3.402823466E 38~-1.175494351E-38 | 0 and 1.175494351E-38~3.402823466E 38 | ||||||||||||||||||||||||||||||||||||||||
8 | -1.7976931348623157E 308~2.2250738585072014E-308 | 0 and 2.2250738585072014E-308~1.7976931348623157E 308 | ||||||||||||||||||||||||||||||||||||||||
M 2 | -1.7976931348623157E 308~2.2250738585072014E-308 | 0 and 2.2250738585072014E-308~1.7976931348623157E 308 |
Insert value | CHAR(3) | Storage requirements | VARCHAR(3) | Storage requirements |
---|---|---|---|---|
'' | '' | 3 Bytes | '' | 1 byte |
'a' | 'a' | 3 bytes | 'a' | 2 bytes |
'ab' | 'ab' | 3 bytes | 'ab' | 3 bytes |
'abc' | 'ab' | 3 bytes | 'abc' | 4 bytes |
'abcd' | 'ab' | 3 bytes | 'abc' | 4 bytes |
The text type is used to represent large text data, such as article content, comments, details, etc. Its types are divided into the following 4 types:
Data type | Storage range |
---|---|
0~255 bytes | |
0~65535 bytes | |
0~16777215 bytes | |
0~4294967295 bytes |
word Number of sections | Value range | Date format | Zero value | |||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1901~2155 | YYYY | 0000 | |||||||||||||||||||||||||||||||||||||||
4 | 1000-01-01~9999-12-31 | YYYY-MM-DD | 0000-00-00 | |||||||||||||||||||||||||||||||||||||||
3 | -838:59:59~ 838:59:59 | HH:MM:SS | 00:00:00 | |||||||||||||||||||||||||||||||||||||||
8 | 1000-01-01 00:00:00~9999-12-31 23:59:59 | YYYY- MM-DD HH:MM:SS | 0000-00-00 00:00:00 | ##TIMESTAMP | ||||||||||||||||||||||||||||||||||||||
1970-01-01 00:00:01~2038-01-19 03:14:07 | YYYY-MM-DD HH:MM:SS | 0000-00-00 00: 00:00 |
Data type | Storage range |
---|---|
TINYBLOB | 0~255 bytes |
BLOB | 0~65535 bytes |
MEDIUMBLOB | 0~16777215 bytes |
LONGBLOB | 0~4294967295 bytes |
After MySQL is installed, if you want to store data in a database table, you must first create a database. Creating a database is to divide a space in the database system to store data. The syntax is as follows:
create database 数据库名称;
Create a database called db1 MySQL command:
-- 创建一个叫db1的数据库show create database db1;
Running effect Display:
After creating the database, view the basic information of the database. MySQL command:
show create database db1;
Running effect display:
Delete database MySQL command:
drop database db1;
Running effect display:
Query all databases in MySQL Database MySQL command:
show databases;
Running effect display:
Change the character set of the database to gbk MySQL command:
alter database db1 character set gbk;
Running effect display:
Switch database MySQL command:
use db1;
Running effect display:
View the currently used database MySQL command:
select database();
Running effect display:
After the database is successfully created, a data table (referred to as a table) can be created in the database to store data. Please note: Before operating the data table, you should use "USE database name;" to specify in which database the operation is to be performed first, otherwise the "No database selected" error will be thrown.
The syntax is as follows:
create table 表名( 字段1 字段类型, 字段2 字段类型, … 字段n 字段类型);
Example: Create student table MySQL command:
create table student( id int, name varchar(20), gender varchar(10), birthday date );
Running effect display :
Example: View all tables in the current database MySQL command:
show tables;
Running effect display:
Example: Basic information MySQL command for table lookup:
show create table student;
Running effect display:
Example: View the field information of the table MySQL command:
desc student;
Running effect display:
Sometimes, you want to modify some information in the table, such as: modify the table name, modify the field name, modify the field data type...etc. Use alter table in MySQL to modify the data table.
Example: Modify table name MySQL command:
alter table student rename to stu;
Running effect display:
Example: Modify field name MySQL command:
alter table stu change name sname varchar(10);
Running effect display:
Example: Modify field data type MySQL Order:
alter table stu modify sname int;
运行效果展示:
示例:增加字段 MySQL命令:
alter table stu add address varchar(50);
运行效果展示:
示例:删除字段 MySQL命令:
alter table stu drop address;
运效果展示:
语法:
drop table 表名;
示例:删除数据表 MySQL命令:
drop table stu;
运行效果展示:
为防止错误的数据被插入到数据表,MySQL中定义了一些维护数据库完整性的规则;这些规则常称为表的约束。常见约束如下:
约束条件 | 说明 |
---|---|
PRIMARY KEY | 主键约束用于唯一标识对应的记录 |
FOREIGN KEY | 外键约束 |
NOT NULL | 非空约束 |
UNIQUE | 唯一性约束 |
DEFAULT | 默认值约束,用于设置字段的默认值 |
以上五种约束条件针对表中字段进行限制从而保证数据表中数据的正确性和唯一性。换句话说,表的约束实际上就是表中数据的限制条件。
主键约束即primary key用于唯一的标识表中的每一行。被标识为主键的数据在表中是唯一的且其值不能为空。这点类似于我们每个人都有一个身份证号,并且这个身份证号是唯一的。
主键约束基本语法:
字段名 数据类型 primary key;
设置主键约束(primary key)的第一种方式
示例:MySQL命令:
create table student(id int primary key,name varchar(20));
运行效果展示:
设置主键约束(primary key)的第二·种方式
示例:MySQL命令:
create table student01(id intname varchar(20),primary key(id));
运行效果展示:
非空约束即 NOT NULL指的是字段的值不能为空,基本的语法格式如下所示:
字段名 数据类型 NOT NULL;
示例:MySQL命令:
create table student02(id intname varchar(20) not null);
运行效果展示:
默认值约束即DEFAULT用于给数据表中的字段指定默认值,即当在表中插入一条新记录时若未给该字段赋值,那么,数据库系统会自动为这个字段插人默认值;其基本的语法格式如下所示:
字段名 数据类型 DEFAULT 默认值;
示例:MySQL命令:
create table student03(id int,name varchar(20),gender varchar(10) default 'male');
运行效果展示:
唯一性约束即UNIQUE用于保证数据表中字段的唯一性,即表中字段的值不能重复出现,其基本的语法格式如下所示:
字段名 数据类型 UNIQUE;
示例:MySQL命令:
create table student04(id int,name varchar(20) unique);
运行效果展示:
外键约束即FOREIGN KEY常用于多张表之间的约束。基本语法如下:
-- 在创建数据表时语法如下:CONSTRAINT 外键名 FOREIGN KEY (从表外键字段) REFERENCES 主表 (主键字段)-- 将创建数据表创号后语法如下:ALTER TABLE 从表名 ADD CONSTRAINT 外键名 FOREIGN KEY (从表外键字段) REFERENCES 主表 (主键字段);
示例:创建一个学生表 MySQL命令:
create table student05(id int primary key,name varchar(20));
示例:创建一个班级表 MySQL命令:
create table class(classid int primary key,studentid int);
示例:学生表作为主表,班级表作为副表设置外键, MySQL命令:
alter table class add constraint fk_class_studentid foreign key(studentid) references student05(id);
运行效果展示:
大家知道:建立外键是为了保证数据的完整和统一性。但是,如果主表中的数据被删除或修改从表中对应的数据该怎么办呢?很明显,从表中对应的数据也应该被删除,否则数据库中会存在很多无意义的垃圾数据。
语法如下:
alter table 从表名 drop foreign key 外键名;
示例:删除外键 MySQL命令:
alter table class drop foreign key fk_class_studentid;
运行效果展示:
外键的那个字段不在了证明删除成功了
1、从表里的外键通常为主表的主键
2、从表里外键的数据类型必须与主表中主键的数据类型一致
3、主表发生变化时应注意主表与从表的数据一致性问题
在MySQL通过INSERT语句向数据表中插入数据。在此,我们先准备一张学生表,代码如下:
create table student( id int, name varchar(30), age int, gender varchar(30) );
每个字段与其值是严格一一对应的。也就是说:每个值、值的顺序、值的类型必须与对应的字段相匹配。但是,各字段也无须与其在表中定义的顺序一致,它们只要与 VALUES中值的顺序一致即可。
语法如下:
INSERT INTO 表名(字段名1,字段名2,...) VALUES (值 1,值 2,...);
示例:向学生表中插入一条学生信息 MySQL命令:
insert into student (id,name,age,gender) values (1,'bob',16,'male');
运行效果展示:
语法如下:
INSERT INTO 表名(字段名1,字段名2,...) VALUES (值 1,值 2,...);
插入数据的方法基本和为表中所有字段插入数据,一样,只是需要插入的字段由你自己指定
语法如下:
INSERT INTO 表名 [(字段名1,字段名2,...)]VALUES (值 1,值 2,…),(值 1,值 2,…),...;
在该方式中:(字段名1,字段名2,…)是可选的,它用于指定插人的字段名;(值 1,值 2,…),(值 1,值 2,…)表示要插人的记录,该记录可有多条并且每条记录之间用逗号隔开。
示例:向学生表中插入多条学生信息 MySQL命令:
insert into student (id,name,age,gender) values (2,'lucy',17,'female'),(3,'jack',19,'male'),(4,'tom',18,'male');
运行效果展示:
在MySQL通过UPDATE语句更新数据表中的数据。在此,我们将就用六中的student学生表
UPDATE 表名 SET 字段名1=值1[,字段名2 =值2,…] [WHERE 条件表达式];
在该语法中:字段名1、字段名2…用于指定要更新的字段名称;值1、值 2…用于表示字段的新数据;WHERE 条件表达式 是可选的,它用于指定更新数据需要满足的条件
示例:将name为tom的记录的age设置为20并将其gender设置为female MySQL命令:
update student set age=20,gender='female' where name='tom';
运行效果展示:
示例:将所有记录的age设置为18 MySQL命令:
update student set age=18;
运行效果展示:
在MySQL通过DELETE语句删除数据表中的数据。在此,我们先准备一张数据表,代码如下:
-- 创建学生表 create table student( id int, name varchar(30), age int, gender varchar(30) ); -- 插入数据 insert into student (id,name,age,gender) values (2,'lucy',17,'female'),(3,'jack',19,'male'),(4,'tom',18,'male'),(5,'sal',19,'female'),(6,'sun',20,'male'),(7,'sad',13,'female'),(8,'sam',14,'male');
在该语法中:表名用于指定要执行删除操作的表;[WHERE 条件表达式]为可选参数用于指定删除的条件。
DELETE FROM 表名 [WHERE 条件表达式];
示例:删除age等于14的所有记录 MySQL命令:
delete from student where age=14;
运行效果展示:
示例:删除student表中的所有记录 MySQL命令:
delete from student;
运行效果展示:
TRUNCATE和DETELE都能实现删除表中的所有数据的功能,但两者也是有区别的:
1、DELETE语句后可跟WHERE子句,可通过指定WHERE子句中的条件表达式只删除满足条件的部分记录;但是,TRUNCATE语句只能用于删除表中的所有记录。
2、使用TRUNCATE语句删除表中的数据后,再次向表中添加记录时自动增加字段的默认初始值重新由1开始;使用DELETE语句删除表中所有记录后,再次向表中添加记录时自动增加字段的值为删除时该字段的最大值加1
3、DELETE语句是DML语句,TRUNCATE语句通常被认为是DDL语句
简单查询即不含where的select语句。在此,我们讲解简单查询中最常用的两种查询:查询所有字段和查询指定字段。
在此,先准备测试数据,代码如下:
-- 创建数据库DROP DATABASE IF EXISTS mydb;CREATE DATABASE mydb;USE mydb;-- 创建student表CREATE TABLE student ( sid CHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(50) DEFAULT 'male');-- 向student表插入数据INSERT INTO student (sid,sname,age,gender) VALUES ('S_1001', 'lili', 14, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1002', 'wang', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1003', 'tywd', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1004', 'hfgs', 17, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1005', 'qwer', 18, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1006', 'zxsd', 19, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1007', 'hjop', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1008', 'tyop', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1009', 'nhmk', 13, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1010', 'xdfv', 17, 'female');
查询所有字段 MySQL命令:
select * from student;
运行效果展示:
查询指定字段(sid、sname) MySQL命令:
select sid,sname from student;
运行效果展示:
在SELECT中除了书写列名,还可以书写常数。可以用于标记
常数的查询日期标记 MySQL命令:
select sid,sname,'2021-03-02' from student;
运行效果展示:
在使用DISTINCT 时需要注意:
在SELECT查询语句中DISTINCT关键字只能用在第一个所查列名之前。
MySQL命令:
select distinct gender from student;
运行效果展示:
在SELECT查询语句中还可以使用加减乘除运算符。
查询学生10年后的年龄 MySQL命令:
select sname,age+10 from student;
运行效果展示:
在此,先准备测试数据,代码如下:
-- 创建数据库DROP DATABASE IF EXISTS mydb;CREATE DATABASE mydb;USE mydb;-- 创建student表CREATE TABLE student ( sid CHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(50) DEFAULT 'male');-- 向student表插入数据INSERT INTO student (sid,sname,age,gender) VALUES ('S_1001', 'lili', 14, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1002', 'wang', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1003', 'tywd', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1004', 'hfgs', 17, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1005', 'qwer', 18, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1006', 'zxsd', 19, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1007', 'hjop', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1008', 'tyop', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1009', 'nhmk', 13, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1010', 'xdfv', 17, 'female');
在开发中,我们常常有类似的需求:统计某个字段的最大值、最小值、 平均值等等。为此,MySQL中提供了聚合函数来实现这些功能。所谓聚合,就是将多行汇总成一行;其实,所有的聚合函数均如此——输入多行,输出一行。聚合函数具有自动滤空的功能,若某一个值为NULL,那么会自动将其过滤使其不参与运算。
聚合函数使用规则:
只有SELECT子句和HAVING子句、ORDER BY子句中能够使用聚合函数。例如,在WHERE子句中使用聚合函数是错误的。
接下来,我们学习常用聚合函数。
统计表中数据的行数或者统计指定列其值不为NULL的数据个数
查询有多少该表中有多少人
MySQL命令:
select count(*) from student;
运行效果展示:
计算指定列的最大值,如果指定列是字符串类型则使用字符串排序运算
查询该学生表中年纪最大的学生
MySQL命令:
select max(age) from student;
运行效果展示:
计算指定列的最小值,如果指定列是字符串类型则使用字符串排序运算
查询该学生表中年纪最小的学生 MySQL命令:
select sname,min(age) from student;
运行效果展示:
计算指定列的数值和,如果指定列类型不是数值类型则计算结果为0
查询该学生表中年纪的总和 MySQL命令:
select sum(age) from student;
运行效果展示:
计算指定列的平均值,如果指定列类型不是数值类型则计算结果为
查询该学生表中年纪的平均数 MySQL命令:
select avg(age) from student;
运行效果展示:
这里我就不一一举例了,基本混个眼熟,以后用到再细说
SELECT NOW();SELECT DAY (NOW());SELECT DATE (NOW());SELECT TIME (NOW());SELECT YEAR (NOW());SELECT MONTH (NOW());SELECT CURRENT_DATE();SELECT CURRENT_TIME();SELECT CURRENT_TIMESTAMP();SELECT ADDTIME('14:23:12','01:02:01');SELECT DATE_ADD(NOW(),INTERVAL 1 DAY);SELECT DATE_ADD(NOW(),INTERVAL 1 MONTH);SELECT DATE_SUB(NOW(),INTERVAL 1 DAY);SELECT DATE_SUB(NOW(),INTERVAL 1 MONTH);SELECT DATEDIFF('2019-07-22','2019-05-05');
--连接函数SELECT CONCAT ()--SELECT INSTR ();--统计长度SELECT LENGTH();
-- 绝对值SELECT ABS(-136);-- 向下取整SELECT FLOOR(3.14);-- 向上取整SELECT CEILING(3.14);
数据库中存有大量数据,我们可根据需求获取指定的数据。此时,我们可在查询语句中通过WHERE子句指定查询条件对查询结果进行过滤。
在开始学习条件查询之前,我们先准备测试数据,代码如下:
-- 创建数据库DROP DATABASE IF EXISTS mydb;CREATE DATABASE mydb;USE mydb;-- 创建student表CREATE TABLE student ( sid CHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(50) DEFAULT 'male');-- 向student表插入数据INSERT INTO student (sid,sname,age,gender) VALUES ('S_1001', 'lili', 14, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1002', 'wang', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1003', 'tywd', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1004', 'hfgs', 17, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1005', 'qwer', 18, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1006', 'zxsd', 19, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1007', 'hjop', 16, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1008', 'tyop', 15, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1009', 'nhmk', 13, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1010', 'xdfv', 17, 'female');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1012', 'lili', 14, 'male');INSERT INTO student (sid,sname,age,gender) VALUES ('S_1013', 'wang', 15, 'female');
在WHERE中可使用关系运算符进行条件查询,常用的关系运算符如下所示:
Relational operator | Description |
---|---|
equal to | |
is not equal to | |
is not equal to | is less than | is less than or equal to | ## > |
>= | |
The above is the detailed content of This article is enough to organize the 37k words of MySQL basics (organized and shared). For more information, please follow other related articles on the PHP Chinese website!