Home > Database > Mysql Tutorial > Summary of the basics of DDL, DML and DQL for MySQL learning

Summary of the basics of DDL, DML and DQL for MySQL learning

WBOY
Release: 2022-05-19 21:11:09
forward
2405 people have browsed it

This article brings you relevant knowledge about mysql, which mainly introduces related content about DDL, DML, and DQL, including operating data tables, operating databases, simple query data, etc. Let’s take a look at the content below, I hope it will be helpful to everyone.

Summary of the basics of DDL, DML and DQL for MySQL learning

Recommended learning: mysql video tutorial

Database related concepts

What is a database

The warehouse that saves data. It is embodied in our computer as a software or file system. Then save the data in these special files, and you need to use a fixed language (SQL language/statement) to operate the data in the file.

  • A warehouse that stores data. The data is stored in an organized manner.
  • English name: DataBase, referred to as DB

Advantages of the database

The database stores data in files according to a specific format, and can easily increase large amounts of data through SQL statements , Delete, Change, Check operations, the database is an efficient solution for managing large amounts of information.

Common databases

Common relational database management systems

When we develop applications, the All data ultimately needs to be saved to professional software. These professional software for saving data are called databases. When we learn databases, we do not learn how to develop a database software. What we learn is how to use databases and the operations of data records in the database. The database software is developed by third-party companies.

Summary of the basics of DDL, DML and DQL for MySQL learning

  • Oracle: It is a large relational database of Oracle Corporation. The system has good portability, easy use and strong functions, and is suitable for various large, medium, small and microcomputer environments. It is efficient, safe and reliable. But it comes with a fee.
  • MYSQL: It was developed by a Swedish company called MySQL AB in the early days. Later it was acquired by Sun Company and then by Oracle. Due to its small size, fast speed, and low total cost of ownership, especially the characteristics of open source, MySQL is generally chosen as the website database for the development of small and medium-sized websites. MySQL6.x version also starts charging.
  • DB2: IBM's database product, chargeable. Often used in banking systems.
  • SQLServer: A medium-sized database charged by Microsoft. C#, .net and other languages ​​are often used.
  • SyBase: From Sybase. has faded from the stage of history. Provides a very professional data modeling tool PowerDesigner.

Commonly used databases: Mainly used databases for Java development applications: MySQL (5.6), Oracle, DB2. (Reason: open source, free, and powerful enough to cope with web development)

relational database

When developing software, there must be certain differences between the data in the software relationship exists. For example, in the relationship between products and customers, one customer can buy multiple products, and one product can be purchased by multiple customers.

It is necessary to save these data in the database and maintain the relationship between the data. In this case, the above-mentioned databases can be used directly. All the above databases are relational databases.

Relational data: When designing a database, it needs to be described using an E-R entity relationship diagram.

E-R are the first letters of the two words, E represents Entity entity and R represents Relationship relationship.

  • 1. There must be a certain relationship between the data in the data table, such as the relationship between products and customers. A customer can buy multiple products, and one product can Purchased by multiple customers.
  • 2. When designing a database, you can use the ER entity relationship diagram to describe the relationship between tables. E represents Entity and R represents Relationship.
  • 3. Entity: can be understood as us An object in a Java program. For example, products, customers, etc. are all entity objects. It is represented by a rectangle (rectangle) in the E-R diagram.
  • 4. Attributes: Entity objects contain attributes, such as product name, price, etc. For the attributes in an entity, we call this entity's data, which is represented by an ellipse in the E-R diagram.
  • 5. Relationship: entity and relationship between entities: represented by a diamond in the E-R diagram.

Requirements: Use E-R diagrams to describe the relationship between customers, products, and orders
Summary of the basics of DDL, DML and DQL for MySQL learning

MySQL database

SQL Introduction

What is SQL

Structured Query LanguageStructured Query Language. SQL statements do not depend on any platform and are common to all databases. After learning how to use SQL statements, you can use them in any database, but they all have unique content. SQL statements are powerful, easy to learn, and easy to use.

SQL features

SQL statement is a non-procedural language, and a specific result will appear after each SQL statement is executed. There is no impact between multiple statements

SQL function

SQL statements are mainly used to operate databases, data tables, Data tables Data record

SQL general syntax

  1. SQL statements can be written in a single line or multiple lines and end with a semicolon.
  2. You can use spaces and indentation to enhance the readability of statements.
  3. The SQL statements of the MySQL database are not case-sensitive, and keyword development is generally capitalized.
  4. Three kinds of comments
    - Single-line comments: --Comment content
    - Multi-line comments: /* Comment content*/
    - # Comment content: (mysql-specific single-line comment)

SQL Classification
SQL is a language used to access relational databases. It has four functions: defining, manipulating, controlling and querying relational databases. Therefore, we classify SQL based on four functions.

  1. DDL (Data Definition Language) Data Definition Language
    is used to define database objects: databases, tables, columns, etc. Keywords: create drop alter truncate (clear data records) show etc.

  2. DML (Data Manipulation Language) data manipulation language ★★★

    is updated in the database table, Add and delete records. Such as update (update), insert (insert), delete (delete) does not include query

  3. DQL (Data Query Language) Data Query Language ★★★★★
    Data table records query. Keywordselect.

  4. DCL (Data Control Language) Data Control Language (understand)

    is a statement used to set or change database user or role permissions. Such as grant (set permissions), revoke (revoke permissions), begin transaction, etc. This is relatively rarely used.

Summary of the basics of DDL, DML and DQL for MySQL learning

##MySQL directory structure

Summary of the basics of DDL, DML and DQL for MySQL learning

MySQL database connection

MySQL is a database that requires an account name and password to log in. You can use it after logging in. It provides a default root account. You can log in using the password set during installation.

Start the database service: Enter the net start MySQL command in the open dos window
Summary of the basics of DDL, DML and DQL for MySQL learning
Close the database service : Enter the net stop MySQL command in the open dos window
Summary of the basics of DDL, DML and DQL for MySQL learning
Connect to MySQL: Login format 1: mysql -u username -p password

mysql -uroot -p123456
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning Login format 2:
mysql[-h connecting host ip address-P port number 3306] -u username-p password

mysql -h 127.0.0.1 -P 3306 -u root -p 123456
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
If you are connecting to the local machine: you can omit -h -P host IP and port. In this way, you can log in to the mysql database

Exit:exitCommand
Summary of the basics of DDL, DML and DQL for MySQL learning
Display database: show databases;
Summary of the basics of DDL, DML and DQL for MySQL learning##SQL_DDL_Operate database

DDL_Create and view database

Create database:

1. Create the database directly

create database 数据库名;
Copy after login

2. Determine whether the database exists and create it (if it does not exist, create it)

create database if not exists 数据库名;
Copy after login

3. Create the database and specify the characters Set (encoding table)

create database 数据库名 character set 字符集;
Copy after login

Description: The character set is the name of the encoding table. In mysql, utf8

View the database:

1. View all databases

show databases;
Copy after login

2. View the definition information of a database

show create database 数据库名;
Copy after login

3. View the currently used database

select database();
Copy after login

Use and switch Database:

use 数据库名;
Copy after login
DDL_Modify and delete database

Modify database:

1. Modify database character set

-- alter 表示修改alter database 数据库名 default character set 新字符集;
Copy after login

Note: If the encoding table specified in the modified database is utf8, remember not to write utf-8

Java中的常用编码对应mysql数据库中的编码

Java MySQL
UTF-8 utf8
GBK gbk
GB2312 gb2312
ISO-8859-1 latin1

删除数据库:

1.直接删除

-- drop 删除数据库drop database 数据库名;
Copy after login

2.删除数据库时判断是否存在(如果存在,则删除)

drop database if exists 数据库名;
Copy after login

SQL_DDL_操作数据表

DDL_数据库约束

约束的概念:

  • 约束是作用于表中列上的规则,用于限制加入表的数据

  • 约束的存在保证了数据库中数据的正确性、有效性和完整性

约束的分类:

约束名称 关键字 描述
非空约束 NOT NULL 保证列中所有数据不能有null空值
唯一约束 UNIQUE 保证列中所有数据各不相同
主键约束 PRIMARY KEY 主键是一行数据的唯一标识,要求非空且唯一
检查约束 CHECK 保证列中的值满足某一条件
默认约束 DEFAULT 保存数据时,未指定值则采用默认值
外键约束 FOREIGN KEY 外键用来让两个表的数据之间建立链接,保证数据的一致性和完整性

MySQL5.7不支持检查约束,但写入语句不会报错,MySQL8.0版本支持检查约束

非空约束

非空约束用于保证列中所有数据不能有NULL值

1.建表时添加约束

-- 创建表时添加非空约束create table 表名(
    列名 数据类型 not null,
    ...);
Copy after login

2.建完表之后添加约束

-- 建完表之后添加约束alter table 表名 modify 字段名 数据类型 not null;
Copy after login

3.删除约束

alter table 表名 modify 字段名 数据类型;
Copy after login

唯一约束

唯一约束用于保证列中所有数据各不相同

1.创建表时添加唯一约束

-- 方式1create table 表名(
    字段名 数据类型 UNIQUE,
    ...);-- 方式2create table 表名(
    字段名 数据类型,
    ...
    [CONSTRAINT] [约束名称] UNIQUE(列名));
Copy after login

2.建完表之后添加唯一约束

-- 建完表后添加唯一约束alter table 表名 modify 字段名 数据类型 UNIQUE;
Copy after login

3.删除唯一约束

alter table 表名 drop index 字段名;
Copy after login

主键约束

  • 主键是一行数据的唯一标识,要求非空且唯一
  • 一张表只能有一个主键

1.创建表时添加主键约束

create table 表名(
    字段名 数据类型 PRIMARY KEY [AUTO_INCREMENT],
    -- [AUTO_INCREMENT] 当不指定值时自动增长
    ...);create table 表名(
    列名 数据类型,
    [CONSTRAINT] [约束名称] PRIMARY KEY(列名))
Copy after login

2.建完表之后添加主键约束

alter table 表名 add PRIMARY KEY(字段名);
Copy after login

3.删除主键约束

alter table 表名 drop PRIMARY KEY;
Copy after login

默认约束

保存数据时,未指定值则采用默认值

1.创建表时添加默认约束

create table 表名(
    字段名 数据类型 default 默认值,
    ...);
Copy after login

2.建完表后添加默认约束

alter table 表名 alter 列名 set DEFAULT 默认值;
Copy after login

3.删除约束

alter table 表名 alter 列名 drop DEFAULT;
Copy after login

DDL_创建和查看和表

前提 :创建数据库db1并使用这个数据库

-- 创建数据库create database db1;-- 使用数据库use db1;
Copy after login

创建表:

create table 表名(
    字段名1 字段类型 约束条件,
    字段名2 字段类型 约束条件,
    ...
    字段名n 字段类型 约束条件);-- 注意:最后一个字段不加逗号
Copy after login

创建一个表结构和其他表结构相同的表

create table 表名 like 其他表名;
Copy after login

MySQL中常用的数据类型
Summary of the basics of DDL, DML and DQL for MySQL learning
案列需求:

设计一张学生表,要求如下:

  • 学号,要求唯一主键,自增
  • 姓名,不能为空,且唯一
  • 性别,只有男和女,默认值为null
  • 班级,字符串类型
  • 入学时间,取值为年、月、日
  • 数学成绩,double类型,默认为60分
  • 英语成绩,double类型,没有默认值
create table students(
    id int primary key auto_increment,
    name varchar(10) not null unique,
    sex enum('男','女') default null,
    class varchar(10),
    starttime date,
    math int default 60,
    english int);
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
查看表:

1.查看某个数据库中所有的表

show tables;
Copy after login

2.查看表结构

desc 表名;
Copy after login

3.查看创建表的SQL语句

show create table 表名;-- 根据该语句查看上面案列的建表sql语句show create table students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning

DDL_删除表和修改表的结构

删除表:

1.直接删除

drop table 表名;
Copy after login

2.删除表时判断表是否存在(如果存在,则删除)

drop table if exists 表名;
Copy after login

修改表:

1.修改表名

alter table 旧表名 rename to 新表名;
Copy after login

2.向表中添加一个字段(一列)

alter table 表名 add 字段名 数据类型;
Copy after login

3.修改表中字段数据类型

alter table 表名 modify 字段名 新的数据类型;
Copy after login

4.修改表中字段名(列名)和数据类型

alter table 表名 change 字段名 新的字段名 新的数据类型;
Copy after login

5.删除表中字段(列)

alter table 表名 drop 字段名;
Copy after login

SQL_DML_操作数据库

DML_插入表数据

1.插入全部字段

-- 全部字段写出来insert into 表名(字段1,字段2,...) values(值1,值2,...);
-- 插入全部不写字段名insert into 表名 values(值1,值2,...);
-- 给案例中的表插入数据insert into students(id,name,sex,class,starttime,math,english) values(1,'张三','男','高三1班','2022-03-02',80,69);
insert into students values(2,'李四','女','高三2班','2022-03-01',70,80);
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
2.插入部分数据

-- 插入姓名,班级,入学时间,英语成绩-- id默认增长,性别默认null,数学默认60
insert into students(name,class,starttime,english) values('王五','高三3班','2022-03-02',78);
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
说明:插入部分数据的时候,要求列名一定书写出来。

3.批量插入数据

insert into 表名 values(字段值1, 字段值2...),(字段值1, 字段值2...),(字段值1, 字段值2...);
Copy after login

没有添加数据的字段会使用NULL

注意:

  • 值与列一一对应。有多少个列,就需要写多少个值。如果某一个列没有值,可以使用null,表示插入空。

  • 值的数据类型,与列被定义的数据类型要相匹配,并且值的长度,不能够超过定义的列的长度。

  • 字符串:插入字符类型的数据,建议写英文单引号括起来。在mysql中,使用单引号表示字符串

  • date 时间类型的数据也得使用英文单引号括起来: 如yyyy-MM-dd

DML_更新表数据

1.不带条件修改数据

update 表名 set 字段名=新的值,字段名=新的值,...;
-- 注意:不带条件的修改是将数据表中的整列都做修改
-- 修改students表中math的值为90update students set math=90;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
2.带条件修改数据

update 表名 set 字段名=新的值,字段名=新的值,... where 条件;
-- 修改students表中王五的性别为男,数学成绩设置为70update students set sex='男',math=70 where name='王五';
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
3.关键字说明

UPDATE: 表示修改记录 
SET: 要改哪个字段WHERE: 设置条件
Copy after login

4.注意

  • 不带条件的更新数据库记录:UPDATE 表名 SET 字段名=新的值;是将整个表中修改的列修改
  • 带条件:UPDATE 表名 SET 字段名=新的值 WHERE 条件

DML_删除表记录

1.不带条件删除

DELETE -- 删除记录DELETE FROM 表名;表还在,可以操作,只是删除数据。
Copy after login

2.带条件删除

DELETE FROM 表名 WHERE 条件;-- 删除学生表中的王五的信息DELETE FROM students WHERE name='王五';
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
3.truncate删除表记录(属于DDL)

truncate table 表名;
Copy after login

4.truncate和delete区别

  • delete是将表中的数据一条一条删除
  • truncate是将整个表摧毁,重新创建一个新的表,新的表结构和原来表结构一模一样

SQL_DQL_ 简单查询数据

准备一张学生表,在这张表上进行查询操作
Summary of the basics of DDL, DML and DQL for MySQL learning

DQL_基础查询

1.查询所有数据

select * from 表名;
-- 查询学生表中所有的数据select * from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
2.查询指定列的数据

select 字段名1,字段名2,... from 表名;
-- 查询姓名和班级这两个字段select name,class from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
3.查询到的字段设置别名

select 字段名1 as 别名1,字段名2 as 别名2 from 表名;
-- 查询students表中的字段并设置别名select id as 学号,name as 姓名,sex as 性别,class as 班级 , starttime as 入学时间 from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
4.查询到的数据去重

-- DISTINCT  去重复-- 查询班级字段结果不出现重复的select DISTINCT class from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning

DQL_条件查询

1.条件查询语法

select 字段名1,字段名2,... where 条件列表;
Copy after login

2.条件运算符

符号 功能
> 大于
小于
>= 大于等于
小于等于
= 等于
或!= 不等于
BETWEEN…AND… 在某个范围内(都包括)
IN(…) 多选一
LIKE 模糊查询,_单个任意字符,%多个任意字符
IS NULL 为空
IS NOT NULL 不为空
AND 或 && 与,并且
OR 或 || 或,或者
NOT 或 ! 非,不是

3.查询数学成绩大于80并且性别为男的学生

-- 两个条件同时满足select * from students where math > 80 and sex='男';
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
4.查询英语成绩在60-80之间的学生

-- BETWEEN 值1 AND 值2 -- 表示从值1到值2范围,包头又包尾
select * from students where english between 60 and 80;
select * from students where english>=60 && english<p><img src="https://img.php.cn/upload/article/000/000/067/01283e47128a89592860b85cda10b160-24.png" alt="Summary of the basics of DDL, DML and DQL for MySQL learning"><br><strong>5.查询学号为1或者2或者3的学生</strong></p><pre class="brush:php;toolbar:false">-- in里面的每个数据都会作为一次条件,只要满足条件的就会显示select * from students where id in (1,2,3);
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning

DQL_模糊查询

LIKE:表示模糊查询

select * from 表名 where 字段名 like '通配字符';
Copy after login

MySQL通配符有两个:

  • %:表示0个或多个字符(任意字符)
  • _:表示一个字符

1.查找名字中以开头的学生

-- '孙%'表示孙后面有任意个字符select * from students where name like '孙%';
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
2.查找名字中以开头的两个字的学习

-- '孙_'表示孙后面只能有一个字符select * from students where name like '孙_';
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning

DQL_查询排序

通过ORDER BY子句,可以将查询出的结果进行排序(排序只是显示方式,不会影响数据库中数据的顺序)

-- ASC:升序排序(默认)-- DESC:降序排序select 字段 from 表名 order by 排序字段 [ASC|DESC];
Copy after login

1.单列排序

-- 查询学生的数学成绩按照升序排序select * from students order by math ASC;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
2.组合排序

-- 查询数学成绩升序的基础上,英语成绩降序-- 组合排序就是先按第一个字段进行排序,如果第一个字段相同,才按第二个字段进行排序,依次类推。
select * from students order by math ASC,english DESC;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning

DQL_ 聚合函数

之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个结果值;另外聚合函数会忽略空值,对于null不作为统计。

1.五个聚合函数

函数名 功能
count(列名) 统计数量(一般选用不为null的列)
max(列名) 最大值
min(列名) 最小值
sum(列名) 求和
avg(列名) 平均值

2.聚合函数语法

select 聚合函数名(列名) from 表名;
Copy after login

注意:null 值不参与所有聚合函数运算

3.查询学生总数

select count(id) from students;-- 通常使用select count(*) from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
4.查询最高分和最低分

-- 查询数学最高分和英语最低分select max(math),min(english) from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
5.求和求平均值

-- 求该表数学总分和平均值select sum(math),avg(math) from students;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
6.ifnull()函数

-- ifnull(列名,默认值)函数表示判断该列是否为空值,如果为null,返回默认值,如果不为空,返回实际值ifnull(math,60); 
-- 如果数学成绩为null时,返回60,如果不为null,就返回实际值
Copy after login

DQL_分组查询

分组: 按照某一列或者某几列。把相同的数据,进行合并输出。

1.注意

  • 按照某一列进行分组,目的为了统计使用。
  • 聚合函数:分组之后进行计算
  • 通常select后面的内容是被分组的列,以及聚合函数
  • sql语句中的where后面不允许添加聚合函数
  • 可以使用having条件,表示分组之后的条件,在having后面可以书写聚合函数

2.查询各个班级的数学成绩总和

-- 查询每个班的数学成绩总和select class,sum(math) from students group by class;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
3.having用法

having必须和group by 一起使用,having和where的用法一模一样,where怎么使用having就怎么使用,where不能使用的,having也可以使用,比如说where后面不可以使用聚合函数,但是在having后面是可以使用聚合函数的。

-- 查询每个班数学总成绩大于300分的班级并显示总成绩
select class,sum(math) from students group by class having sum(math)>300;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
4.where和having的区别

  • having 通常与group by 分组结合使用。 where 和分组无关。

  • having 可以书写聚合函数 (聚合函数出现的位置: having 之后),例如having中的 聚合函数(count,sum,avg,max,min),是不可以出现where条件中。

  • where 是在分组之前进行过滤的,having 是在分组之后进行过滤的。

DQL_分页查询

1.应用和概念

比如我们登录京东,淘宝,返回的商品信息可能有几万条,不是一次全部显示出来。是一页显示固定的条数。假设我们一每页显示5条记录的方式来分页。
Summary of the basics of DDL, DML and DQL for MySQL learning

-- 起始索引:从0开始,索引是0表示数据表第一行数据select 字段列表 from 表名 limit 起始索引,查询条目数;
Copy after login

计算公式:起始索引=(当前页码-1)* 每页显示的条数

注意:

  • 分页查询limit是MySQL数据库的方言
  • Oracle分页查询使用rownumber
  • SQLServer分页查询使用top

2.分页查询

<span style="font-family: " microsoft yahei sans gb helvetica neue tahoma arial sans-serif>-- 查询学生表中数据,每四条数据为一页<br>select * from students limit 0,4;<br>select * from students limit 4,4;<br>select * from students limit 8,4;<br>select * from students limit 12,4;<br>...<br>-- 注意:最后一行不够查询条目数,有多少就显示多少</span><br>
Copy after login

3.返回前几条或者中间某几行数据

-- 2表示分页查询的索引,对应数据表是第3行数据,4表示每页显示4条数据
-- 查询从第三行数据开始查询之后的四条数据
select * from students limit 2,4;
Copy after login

Summary of the basics of DDL, DML and DQL for MySQL learning
4.SQL执行顺序

SELECT 字段名(5) FROM 表名(1) WHERE 条件(2) GROUP BY 分组列名(3) HAVING 条件(4) ORDER BY 排序列名(6) LIMIT 跳过行数, 返回行数(7);
执行顺序:1234567
Copy after login

顺序:1234567

推荐学习:mysql视频教程

The above is the detailed content of Summary of the basics of DDL, DML and DQL for MySQL learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template