Sql表数据操作_MySQL
表数据操作包括数据的插入、修改和删除。
一、插入数据
在向表中添加数据时应该注意两点:第一是用户权限,只有sysadmin角包成员、数据库和数据库对象所有者及其授权用户才有权限向表中添加数据;第二是数据格式,对于不同的数据类型,插入数据的格式也不一样,应严格遵守它们各自的格式要求。
Transact-SQL语言中用INSERT语句向表或视图中插入新的数据行。INSERT语句的语法格式为:
INSERT [INTO] table_source
{[column_list]
VALUES ({DEFAULT | constant_expression} [,…n])
|DEFAULT VALUES
|select_statement
|execute_statement
}
}
其中,column_list参数为新插入数据行中一列或多列列名列表,它说明INSERT 语句只为指定列插入数据。在给表或视中部分列插入数据时,必须使用列名列表方式指出这部分列名。其余未指定列的列值要根据它们的默认值和空值属性情况而定,它们有以下几种可能取值:
(1)对于timestamp列或具有IDENTITY属性列,它们的列值由SQL Server计算后自动赋值。
(2)如果这些列有默认值或关联有默认数据库对象,插入新列时,它们的值为默认值。
(3)当这些列没有默认值设置时,但它们允许空值时,该列值为空。
(4)当这些列既没有默认值设置,也不允许空值时,SQL Server在执行INSERT 语句时将产生错误,
导致插入操作失败。
当未指定column_list 参数时,为各列所提供的数据顺序应严格按照表中各列的定义顺序,而使用column_list参数则可以调整向表中所插入数据的列顺序,只要VALUES子句所提供的数据顺序与column_list参数中指定的列顺序相同即可。
VALUES子句为新插入行中column_list 参数所指定列提供数据,这些数据可以以常量表达式形式提供,或使用DEFAULT关键字说明向列中插入其默认值。
DEFAULT VALUES说明向表中所有列插入其默认值。对于具有INDENTITY 属性或timestamp 数据类型列,系统将自动插入下一个适当值。对于没有设置默认值的列,如果它们允许空值,SQL Server将插入null,否则返回一错误消息。
select_statement是标准的数据库查询语句,它是SQL Server为INSERT语句所提供的又一种数据插入方式。INSERT语句将select_statement子句所返回的结果集合数据插入到指定表中。查询语句结果集合每行中的数据数量、 数据类型和排列顺序也必须与表中所定义列或 column_list 参数中指定列的数量、数据类型和排列顺序完全相同。
SQL Server为INSERT语句提供的第四种数据插入方式是通过执行系统存储过程,其数据来自于过程执行后所产生的结果集合。所执行的过程可以为存储过程、系统存储过程或扩展存储过程,它们既可以为本地存储过程,又可以是远程服务器上的存储过程,只要用户具有它们的执行权限即可。有关存储过程请参阅对应的内容。
table_source说明INSERT语句插入数据时所操作的表或视图,其语法格式可简单书写为:
{table_name [[AS] table_alias]
| view_name [[AS] table_alias]
}
table_name和view_name说明被插入数据的表或视图名称,table_alias参数为表或视图设置别名。
使用别名有两方面原因:第一、当表或视图名称较长时,使用别名可以简化书写工作;第二,在自连接或子查询中,使用别名可以区别同一个表或视图。
在向表中插入数据时, 如果所插入的数据与约束或规则的要求冲突, 或是它们的数据类型不兼容时,将导致INSERT 语句执行失败。当使用SELECT或EXECUTE子句向表中一次插入多行数据时,如果其中有任一行数据有误,它将导致整个插入操作失败,使SQL Server停止所有数据行的插入操作。
例一、使用数值列表方式( 假定 usertable 表中只定义了name、age和sex字段,且name、sex均为
char类型,age为int类型)。
INSERT usertable
VALUES ('张三','女',18)
例二、使用列名列表方式
INSERT usertable (age,name)
VALUES (18,'张三')
例三、在数值列表中,还可以将变量的值插入到表中。在使用变量为列提供数据时,应保证变量的数据类型与列数据类型相同,或是可以自动将它们转换为相同的数据类型。例如:
DECLARE @name char(16)
SET @name='张三'
INSERT usertable
VALUES (@name,DEFAULT,20)
本例中Asp中是这样的:
dim name
name="张三"
sqlstr="INSERT usertable VALUES ('"&name&"','女',20)"
……
例四、将SELECT子句的所返回的结果集合插入到表中。例如:
INSERT usertable (name,sex,age)
SELECT 's' name,sex,age
FROM usertable
WHERE name like '张%%'
二、修改数据
Transact-SQL中的UPDATE语句用于修改表中数据,该语句的语法格式为:
UPDATE ()
SET (
column_name={expression | DEFAULT }
| @variable = expression
} [,…n]
[FROM
{
| (select_statement) [AS] table_alias [,…m]) ]
}
[,…n]
]
[WHERE
| CURRENT OF ({[GLOBAL] cursor_name } | cursor_variable_name} }
]
别看写了一大堆,最常用的只是下列格式:
UPDATE table_name
SET column_name1=variable1,column_name2=variable2
WHERE search_conditions
其中table_or_view参数指出待修改的表或视图名称,其格式与INSERT语句中该参数的格式相同。
SET子句指出表中被修改的列或变量,以及它们的新值。column_name为被修改的列名,@variable为一个已经声明的局部变量名称,它们修改后的值由expression表达式提供,或使用DEFAULT关键字将默认值赋给指定列。
FROM子句引出另一个表,它为UPDATE语句的数据修改操作提供条件。
WHERE子句中的search_conditions 参数说明UPDATE语句的修改条件,它指出表或视图中的哪些行需要修改。省略WHERE子句时,说明对指定的表或视图中的所有行进行修改!!!!
WHERE子句中的CURRENT OF说明在游标的当前位置处执行修改操作,游标由curror_name 或游标变量cursor_variable_name指定。
UPDATE不能修改具有IDENTITY属性列的列值。
例一、将usertable表中所有人员的性别改为'男'
UPDATE usertable
SET sex='男'
例二、将性别为null的所有人员的性别改成'男'
UPDATE usertable
SET sex='男'
WHERE sex IS NULL
例三、将所有姓名为null的人员的姓名改为'张三'、性别改为'女',年龄改为18
UPDATE usertable
SET name='张三',sex='女',age=18
WHERE name IS NULL
三、删除数据
Transact-SQL中,DELETE和TRUNCATE TABLE语句均可以删除表中的数据。DELETE语句的语法格式为:
DELETE
{table_name | view_name}
FROM
{
| (select_statement) [AS] table_alias [(column_alias [,…m])]
}[,…n]
[WHERE
{
|{ [CURRENT OF {{[global] cursor_name}
|cursor_variable_name
}
DELETE语句的结构与UPDATE语句有些类似,其中也包含FROM子句和WHERE子句。WHERE子句为数据删
除指定条件。不使用WHERE子句时,DELETE语句将把有或视图中所有的数据删除。FROM子句是Transact-SQL在ANSI基础上对DELETE语句的扩展,它指定要连接的表名,提供与相关子查询相似的功能。
TRUNCATE TABLE语句语法格式为:
TRUNCATE TABLE table_name
TRUNCATE TABLE语句删除指定表中的所有数据行,但表结构及其所有索引继续保留,为该表所定义约束、规则、默认和触发器仍然有效。如果所删除表中包含有IDENTITY列,则该列将被复位到其原始基值。使用不带WHERE子句的DELETE语句也可以删除表中所有行,但它不复位IDENTITY列。
与DELETE语句相比,TRUNCATE TABLE语句的删除速度更快。因为DELETE语句在每删除一行时都要把删除操作记录到日志中,而TRUNCATE TABLE语句则是通过释放表数据页面的方法来删除表中数据,它只在释放页面做一次事务日志记录。所以使用TRUNCATE TABLE语句删除数据后,这些行是不可恢复的,而DELETE操作则可回滚,能够恢复原来数据。
因为TRUNCATE TABLE语句不做操作日志,它不能激活触发器,所以TRUNCATE TABLE语句不能删除一个被其它表通过FOREIGN KEY约束所参照的表。
例一、使用DELETE语句删除usertable表中name为'张三'的数据行
DELETE usertable
WHERE name='张三'
例二、下面使用FROM子句和WHERE子句指定条件,然后从TB_update表中删除数据
DELETE TB_update
FROM TB_constraint AS a,TB_update AS b
WHERE a.name=b.name
AND a.country='China'
DELETE TB_update
FROM (SELECT * FROM TB_constraint
WHERE country='USA') AS a
WHERE a.name=TB_update.name

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.
