search
HomeBackend DevelopmentPHP ProblemWhat are the basic knowledge points of databases in PHP? Basic statement? basic concept?

The previous article introduced you to "What are the types of scopes in PHP? What area can be accessed by the scope? 》, this article continues to introduce to you what are the basic knowledge points of databases in PHP? Basic statement? basic concept? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What are the basic knowledge points of databases in PHP? Basic statement? basic concept?

Basic concepts of database

Command terminator: "\g" or ";"

Mysql Supported data types:

  • Numeric type

  • Integer type

Tinyint, smallint, mediumint, int and bigint

Ddl statement (commands can be case-sensitive)

Create database

 Create database 数据库名

Select the database to be operated: use database; for We need to use use to select the operating database

View all data tables in the database show tables

Delete the database

 Drop database 数据库名称

Create a table (in a certain To create a table in a database, you need to first use use to select the database to be operated)

Create table table name (

Field 1 name Field 1 type Column constraints,

Field 2 name Field 2 type Column constraints,

After creating the table, you can view the form definition

Desc 表名;

View the SQL statement that created the table

 Show create table 表名\G(\G选项使得记录能按照字段竖向排列,一遍更好展示内容较长的记录,\G之后不需要加分号)

Delete table

Drop table 表名;

Modify table field type

 Alter table 表名 modify [colimn] 字段定义 [first|after字段名];

Add table field

Alter table 表名 add [colimn] 字段定义 [first|after字段名];

Delete table fields

Alter table 表名 change [colimn] 旧的字段名 字段定义 [first|after字段名];

Note: Both change and modify can modify the definition of the table. The difference is that change requires two column names after change, which is not particularly convenient. The advantage is changeYou can modify the field name

Modify the field arrangement and sorting

[first|after field name] This selection can be used to modify the position of the field in the table. New fields are loaded at the last position in the table by default, and change/modifywill not change the field position by default

Note: change/first|after field names these Keywords are extensions of mysql to standard sql and may not be applicable to other databases

Change table name

After table 表名 rename [to] 新的表名
Dml语句

Insert record

Insert into table name (field 1, field 2, ..., field n) values ​​(value 1, value 2, ..., value n);

You don’t need to specify fields name, but the order after values should be consistent with the field sorting

Insert multiple records at one time

Insert into table name (field 1, field 2, ...,field n)

Values

(value 1,value 2,... ,value n),

(value 1,value 2,.. . , value n)

;

Query records

select * from 表名 where 条件;(*代表你查询表里的所有字段,如果我们查询某一字段,只需要将*改成那一字段即可。)

Query unique records

Select distinct 字段1,字段2 from 表名;(只要字段1,字段2任何一个字段有不同就会被选择,一般用于distinct,只筛选一个字段)

Conditional query

Note: Conditional query comparison symbols: =,,>=,

Sort and limit

Sort:

  • asc: from low to high, It is also the default value select * from table name order by field name asc;

  • desc: from high to bottom select * from table name order by field name desc;

  • Multiple fields sort select * from table name order by field name desc,id desc;

  • The number 1 represents the record from which to start (starting from 0 ), the number 2 represents how many to take!

Aggregation

Users need to perform some summary operations, which requires sql aggregation operations.

①sum sum select sum(field name) from table name;

②count total number of recordsselect count(*|field name) from table name;

③maxmax select max(field name) from table name;

④min minimum valueselect min(field name) from table name;

⑤GROUP BY classification aggregation select department,sum(salary) from employee group by department ;

⑥WITH ROLLUP The results after classification and aggregation are summarized again select sum(salary) from employee group by department with rollup;

⑦HAVING

Note: ## The difference between #having and where is that having is conditional filtering of the aggregation results, while where is filtering the records before aggregation. You should filter the records first as much as possible!

select sum(salary) from employee group by department having sum(salary)>1000;
在一起使用:select sum(id),max(id),min(id),count(*) from a1;

Table connection (can be used when displaying fields in multiple tables)

Connection classification

Inner join:Select Records that match each other in the two tables (select table.field,.... from table 1 name, table 2 name,... where [matching conditions such as table 1. field = table 2. field];)

select statements can give aliases to fields! Just write them directly after the fields that need to be displayed in the query. You can also give aliases to the tables.

Outer joins: Not only select two matching fields Records, other unmatched records will also be queried

Left join

  • 包含左边表中的所有记录(包括右表中没有和它匹配的记录)select * from 表1 left join 表2 on 表1.字段=表2.字段;

  • 包含右边表中的所有记录(包括左表中没有和它匹配的记录)

  • 左连接和右连接是可以相互转换的!

  • 子查询(一个查询需要另外一个查询的结果参与的时候)

用于子查询的关键字:

in在..里面(注意点 in后面的子语句必须只返回一个字段,若查询结果唯一(只有一条)可以使用=代替in,not in与in相反)

语法:select * from 表名1 where 字段1 in(select 字段2 from 表2);

 Exists(后面那个子语句有没有查询出记录来,如果查询出记录来返回true,否则就是false,并且查询出来的记录的具体的值是NULL也可以,也是返回true.)

语法:select语句 where exists(select 语句);

 not exits(与exists相反)

记录联合(我们常常会碰到需要将两个表或者多个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示这是就需要用到记录联合)

 多个select 语句用UNION或者UNION ALL隔开即可实现

区别: 前者会将多个查询结果合并后并且进行去除重复后返回,后者 则直接合并并不去除重复

联合的条件:查询的列个数要相等 

更新记录

更新一个表

Update 表名 set 字段1=值1,字段2=值2,...,字段n=值n[where条件];

更新多个表中数据

Update 表1,表2,...表n  set 表1.字段1=表达式1,...,表n.字段n=表达式n[where条件];

注:多表更新更多是用在根据一个标的字段来动态更新另一表的字段

推荐学习:php视频教程

 

The above is the detailed content of What are the basic knowledge points of databases in PHP? Basic statement? basic concept?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
深入理解MySQL索引优化器工作原理深入理解MySQL索引优化器工作原理Nov 09, 2022 pm 02:05 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于索引优化器工作原理的相关内容,其中包括了MySQL Server的组成,MySQL优化器选择索引额原理以及SQL成本分析,最后通过 select 查询总结整个查询过程,下面一起来看一下,希望对大家有帮助。

sybase是什么数据库sybase是什么数据库Sep 22, 2021 am 11:39 AM

sybase是基于客户/服务器体系结构的数据库,是一个开放的、高性能的、可编程的数据库,可使用事件驱动的触发器、多线索化等来提高性能。

visual foxpro数据库文件是什么visual foxpro数据库文件是什么Jul 23, 2021 pm 04:53 PM

visual foxpro数据库文件是管理数据库对象的系统文件。在VFP中,用户数据是存放在“.DBF”表文件中;VFP的数据库文件(“.DBC”)中不存放用户数据,它只起将属于某一数据库的 数据库表与视图、连接、存储过程等关联起来的作用。

数据库系统的构成包括哪些数据库系统的构成包括哪些Jul 15, 2022 am 11:58 AM

数据库系统由4个部分构成:1、数据库,是指长期存储在计算机内的,有组织,可共享的数据的集合;2、硬件,是指构成计算机系统的各种物理设备,包括存储所需的外部设备;3、软件,包括操作系统、数据库管理系统及应用程序;4、人员,包括系统分析员和数据库设计人员、应用程序员(负责编写使用数据库的应用程序)、最终用户(利用接口或查询语言访问数据库)、数据库管理员(负责数据库的总体信息控制)。

microsoft sql server是什么软件microsoft sql server是什么软件Feb 28, 2023 pm 03:00 PM

microsoft sql server是Microsoft公司推出的关系型数据库管理系统,是一个全面的数据库平台,使用集成的商业智能(BI)工具提供了企业级的数据管理,具有使用方便可伸缩性好与相关软件集成程度高等优点。SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使用户可以构建和管理用于业务的高可用和高性能的数据应用程序。

go语言可以写数据库么go语言可以写数据库么Jan 06, 2023 am 10:35 AM

go语言可以写数据库。Go语言和其他语言不同的地方是,Go官方没有提供数据库驱动,而是编写了开发数据库驱动的标准接口,开发者可以根据定义的接口来开发相应的数据库驱动;这样做的好处在于,只要是按照标准接口开发的代码,以后迁移数据库时,不需要做任何修改,极大方便了后期的架构调整。

mysql查询慢的因素除了索引,还有什么?mysql查询慢的因素除了索引,还有什么?Jul 19, 2022 pm 08:22 PM

mysql查询为什么会慢,关于这个问题,在实际开发经常会遇到,而面试中,也是个高频题。遇到这种问题,我们一般也会想到是因为索引。那除开索引之外,还有哪些因素会导致数据库查询变慢呢?

access数据库的结构层次是什么access数据库的结构层次是什么Aug 26, 2022 pm 04:45 PM

结构层次是“数据库→数据表→记录→字段”;字段构成记录,记录构成数据表,数据表构成了数据库。数据库是一个完整的数据的记录的整体,一个数据库包含0到N个表,一个表包含0到N个字段,记录是表中的行。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.