search
HomeDatabaseMysql Tutorial用SQL语句操作数据库_MySQL

—―有一天,当你发觉日子特别的艰难,那可能是这次的收获将特别的巨大。—―致那些懈怠的岁月

 

本章任务:

 

学生数据库中数据的增加、修改和删除

 

目标:

 

1:使用T-SQL向表中插入数据

 

2:使用T-SQL更新表中数据

 

3:使用T-SQL删除表中数据

 

首先我们来简单的介绍一下SQL及它的作用是什么

 

在进行数据库管理时,如果每次创建数据库、表或者从数据库中读取数据,都需要手动在SQL Server Management Studio中进行的话,不但管理不方便,而且存储在数据库中的数据也根本无法提供给程序使用。所以,数据库也需要一套指令集,能够识别指令、执行相应的操作为程序提供数据,目前表中的指令集及时SQL语言。

 

SQL语言是真的数据库而言的一门语言,它可以创建数据库、数据表,可以针对数据库的数据进行增、删、改、查等操作,可以常见视图、存储过程,可以赋予用户权限等。

 

SQL中的运算符

 

运算符是一种符号,是用来进行列间或者变量之间的比较和数学运算的。在SQL中,常用的运算符有算数运算符、赋值运算符、比较运算符和逻辑运算符。

 

1:算数运算符

 

算数运算符包括:+(加),—(减),*(乘),/(除),%(模)五个。算数运算符用来在两个数或表达式上执行数学运算,这两个表达式可以是任意两个数字数据类型的表达式

 

运算符 说明

+ 加运算,求两个数或表达式相加的和

减运算,求两个数或表达式相减的差

* 乘运算,求两个数或表达式相乘的积

/ 除运算,求两个数或表达式相除的商,例如,5/5的值为1,5.7/3的值为1.900000

% 取模运算,求两个数或表达式相除的余数,例如,5%3的值为2

 

2:赋值运算符

 

SQL有一个赋值运算符,即“=”(等号),用于将一个数或变量或表达式赋值给另一个变量

 

运算符 说明

= 吧一个数或变量或表达式赋值给另一个变量,例如:Name=‘张三’

 

3:比较运算符

 

比较运算符用来判断两个表达式的大小关系,除text、ntext或Image数据类型的表达式外,比较运算符几乎可以用于其他所有的表达式。

 

运算符 说明

= 等于,例如:age=23

> 大于,例如:price>100

小于

不等于

>= 大于等于

小于等于

!= 不等于(非SQL-92标准)

 

4:逻辑运算符

 

逻辑运算符用来对某个条件进行判断,以获得判断条件的真假,返回带有TRUE或FALSE值的布尔数据类型

 

运算符 说明

AMD 当且仅当两个布尔表达式都为TRUE时,返回TRUE

OR 当且仅当两个布尔表达式都为FALSE时,返回FALSE

NOT 对布尔表达式的值取反,有限级别最好

 

——————————————————————————————————————————————————————————————————————

 

使用T-SQL插入数据

 

1.使用INSERT插入数据(使用INSERT语句一行一行的插入数据是最常用的方法)

 

语法如下:

 

INSERT [INTO] 表名 [(列名列表)] VALUES(值类表);

 

 

其中

 

1.[INTO]是可选的,也可以省略。

 

2.表名是必须的。

 

3.表的列名是可选的,如果省略,将依次插入所有列。

 

4.多个列名和多个值列表用逗号分隔。

 

5.分号(;)是T-SQL语句终止符,分好不是必须的。

 

一次插入多行数据

 

1.通过INSERT SELECT语句向表中添加数据

 

例如,创建一张新表AddressList来存储本班的通讯信息,则可以从学生表中提取相关的数据插入建好的AddressList表中,语句如下:

 

INSERT INTO Addresslist(姓名,地址,电子邮件)

SELECT SName,SAdd热身赛,SEmail

FROM Students

注意:

 

查询的多的数据个数,顺序,数据类型等,必须与插入的项保持一致。

 

AddressList表必须预先创建好,并且具有姓名,地址和电子邮件三个列。

 

2.通过SELECT INTO语句将现有表中的数据添加到新表中

 

与上面的INSERT INTO 类似,SELECT INTO语句也是从一个表中选择一些数据插入新表中,所不同的是,这个新表是执行查询语句的时候创建的,不能够预先存在。

 

 例:

 

SELECT Students.Name,Students.SAddress,Students.SEMmail

INTO AddressList

FROM Students

 

 

 将创建新表的AddressList,把Students表中的SName,SAddress,SEmail作为AddressList表的新列,并且把查询到的数据全部插入新表中。

 

3.通过UNION关键字合并数据进行插入

 

UNION语句用于将两个不同的数据或查询结果组合成一个新的结果集。当然,不同的数据或查询结果,也要求数据个数,顺序,数据类型都一致,因此,当向表中多次插入数据的时候,可以使用SELECT...UNION来简化操作

 

 例:

 

INSERT Student(SName,SGrade,SSex)

SELECT '张三',7,1 UNION

SELECT '李四',4,0 UNION

SELECT '王五',2,0 UNION

SELECT '朱六',3,0 UNION

SELECT '王二麻子',7,1 UNION

 

 

 这样的效果其实与上面INSERT...SELECT的效果是一样的,只不过多行数据是后写的,然后用UNION合并组成多行数据记录,最后把这些多行数据记录一起插入

 

——————————————————————————————————————————————————————————————————————

 

使用T-SQL更新数据

 

语法:

 

UPDATE 表名 SET 列名 = 更新值 [WHERE 更新条件]

 

 

其中:

 

1.SET后面可以紧随多个”列名=更新值“,修改东哥数据列的值,不限一个,使用逗号分隔。

 

2.WHERE子句是可选的,用来限制更新数据的条件。若不限制,则整个表的数据行将被更新。

 

使用T-SQL删除数据

 

语法:

 

DELETE [FROM] 表名 [WHERE ]

 

 

 例:

 

在学生信息表中删除姓名为”张三“的数据

 

DELETE FROM Students

WHERE SName = '张三'

 

 

使用TTRUNCATE TABLE删除数据

 

TTRUNCATE TABLE用来删除表中说有行,功能上它类似于没有WHERE子句的DELETE语句。

 

 例如,要删除学生信息表中的所有记录行,可以使用以下语句

 

TRNCATE TABLE Students

但TRUNCATE TABLE 比DELETE 执行速度更快,使用的系统资源和事务日志资源更少,并且删除数据后表的标识列会重新开始编号。

 

 实际工作中,不建议使用TRUNCATE TABLE 语句,因为使用它删除的数据不能恢复还原。

 

友情提示:删除前问自己三遍是不是真的要删除,问下令删除的人三遍是否要删除,确定没有遗漏后执行操作。

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
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment