1. sql清空表数据的方法
一、sql清空表数据的三种方式:
1、truncate--删除所有数据,保留表结构,不能撤销还原
2、delete--是逐行删除速度极慢,不适合大量数据删除
3、drop--删除表,数据和表结构一起删除,快速
二、语法
truncate table 表名
delete from 表名
delete from 表名 where 列名="value "
drop form 表名
2. SQL语句中删除表中数据的语句是什么
删除数据 delete Table where 字段=''更新数据 update Table set 字段='' where 字段=''truncate drop都可删除
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作。
语法 TRUNCATE TABLE table_name 参数 table_name是要截断的表的名称或要删除其全部行的表的名。
3. 清空某表的全部数据的SQL语句是不是这样写
是,也可以用DELETE FROM [表名] 。
以下是搜到的它们的区别:
TRUNCATE和DELETE有以下几点区别
1、TRUNCATE在各种表上无论是大的还是小的都非常快。如果有ROLLBACK命令DELETE将被撤销,而TRUNCATE则不会被撤销。
2、TRUNCATE是一个DDL语言,向其他所有的DDL语言一样,他将被隐式提交,不能对TRUNCATE使用ROLLBACK命令。
3、TRUNCATE将重新设置高水平线和所有的索引。在对整个表和索引进行完全浏览时,经过TRUNCATE操作后的表比DELETE操作后的表要快得多。
4、TRUNCATE不能触发任何DELETE触发器。
5、不能授予任何人清空他人的表的权限。
6、当表被清空后表和表的索引讲重新设置成初始大小,而delete则不能。
7、不能清空父表。
4. 清空表应该怎么写 SQL
清空所有数据:Truncate Table stu或者Delete stu。
清空第10条以后数据:Delete From Stu Where id Not In、
Select Top 10 id From stu Order By id。
5. sql怎么删除一个表中的所有数据
删除表数据有两种方法:delete和truncate。具体语句如下:
一、RUNCATE TABLE name :
删除表中的所有行,而不记录单个行删除操作。在这个指令之下,表格中的资料会完全消失,可是表格本身会继续存在。
TRUNCATE TABLE 的语法:TRUNCATE TABLE name ,参数 name 是要截断的表的名称或要删除其全部行的表的名称。
二、Delete from tablename where 1=1
1、delete语法:
DELETE FROM 表名称 WHERE 列名称 = 值。
2、删除所有行:
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:DELETE FROM table_name。
(5)sql清空数据的语句扩展阅读:
truncate和delete的共同点及区别:
1、 truncate和 delete只删除数据不删除表的结构(定义) 。
2、delete语句是dml,这个操作会放到rollback segement中,事务提交之后才生效;如果有相应的trigger,执行的时候将被触发。
truncate是ddl, 操作立即生效,原数据不放到rollback segment中,不能回滚. 操作不触发trigger。
3、delete语句不影响表所占用的extent, 高水线(high watermark)保持原位置不动 。truncate 语句缺省情况下见空间释放到 minextents个 extent,除非使用reuse storage; truncate会将高水线复位(回到最开始)。
4、速度,一般来说: truncate >delete 。
6. 如何一个SQL语句就删除表中所有的数据
使用 TRUNCATE TABLE 删除所有行
若要删除表中的所有行,则 TRUNCATE TABLE 语句是一种快速、无日志记录的方法。该语句总是比不带条件的 DELETE 语句要快,因为 DELETE 语句要记录对每行的删除操作,而 TRUNCATE TABLE 语句只记录整个数据页的释放。TRUNCATE TABLE 语句立即释放由该表的数据和索引占用的所有空间。所有索引的分发页也将释放。
与 DELETE 语句相同,使用 TRUNCATE TABLE 清空的表的定义,同其索引和其它相关的对象一起仍保留在数据库中。必须使用 DROP TABLE 语句才能除去表的定义。
TRUNCATE TABLE tablename
----------------------
TRUNCATE TABLE
删除表中的所有行,而不记录单个行删除操作。语法TRUNCATE TABLE name参数name
是要截断的表的名称或要删除其全部行的表的名称。注释TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行。但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少。
DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项。TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。
TRUNCATE TABLE 删除表中的所有行,但表结构及其列、约束、索引等保持不变。新行标识所用的计数值重置为该列的种子。如果想保留标识计数值,请改用 DELETE。如果要删除表定义及其数据,请使用 DROP TABLE 语句。
对于由 FOREIGN KEY 约束引用的表,不能使用 TRUNCATE TABLE,而应使用不带 WHERE 子句的 DELETE 语句。由于 TRUNCATE TABLE 不记录在日志中,所以它不能激活触发器。
TRUNCATE TABLE 不能用于参与了索引视图的表。示例下例删除 authors 表中的所有数据。
7. 删除数据库的sql语句如何写
1、drop database:数据库名--删除数据库的。
2、drop table:表名--删除表的。
3、delete from:表名--where条件--删除数据的。
4、truncate table:表名--也是删除数据库的。
拓展资料
1、SQL即结构化查询语言(Structured Query Language),是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
2、SQL语句无论是种类还是数量都是繁多的,很多语句也是经常要用到的,SQL查询语句就是一个典型的例子,无论是高级查询还是低级查询,SQL查询语句的需求是最频繁的。
8. 用SQL语句怎么删除表中的所有数据
从一个表中删除数据,使用DELETE语句。从表中删除所有行
DELETE FROMtable_name;
或DELETE * FROMtable_name;
或DELETE FROM Customers
WHERE cust_id = '10000006';
DELETE不需要列名和通配符,它是删除整行而不是删除列,要删除指定的列,请使用update语句;并且DELETE语句从表中删除行,甚至是删除表中所有行,而不是删除表本身。
如果想删除表中的所有行,可以使用TRUNCATE TABLE语句,完成相同的工作,而速度更快。
(8)sql清空数据的语句扩展阅读
drop直接删掉表。
truncate删除的是表中的数据,再插入数据时自增长的数据id又重新从1开始。
delete删除表中数据,可以在后面添加where字句
(1)DELETE语句执行删除操作的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作。TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把单独的删除操作记录记入日志保存,删除行是不能恢复的。并且在删除的过程中不会激活与表有关的删除触发器。执行速度快。
(2) 表和索引所占空间。当表被TRUNCATE 后,这个表和索引所占用的空间会恢复到初始大小,而DELETE操作不会减少表或索引所占用的空间。drop语句将表所占用的空间全释放掉。
(3) 一般而言,drop > truncate > delete
(4) 应用范围。TRUNCATE 只能对TABLE;DELETE可以是table和view
(5) TRUNCATE 和DELETE只删除数据,而DROP则删除整个表(结构和数据)。
(6) truncate与不带where的delete :只删除数据,而不删除表的结构(定义)drop语句将删除表的结构被依赖的约束(constrain),触发器(trigger)索引(index);依赖于该表的存储过程/函数将被保留,但其状态会变为:invalid。
9. 如何一个SQL语句就删除表中所有的数据
用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL SERVER.(表之间不能有外键关系)
1.搜索出所有表名,构造为一条SQL语句
declare@trun_namevarchar(8000)
set@trun_name=''
select@trun_name=@trun_name+'truncatetable'+[name]+''fromsysobjectswherextype='U'andstatus>0
exec(@trun_name)
该方法适合表不是非常多的情况,否则表数量过多,超过字符串的长度,不能进行完全清理.
2.利用游标清理所有表
declare@trun_namevarchar(50)
declarename_cursorcursorfor
select'truncatetable'+namefromsysobjectswherextype='U'andstatus>0
openname_cursor
fetchnextfromname_cursorinto@trun_name
while@@FETCH_STATUS=0
begin
exec(@trun_name)
print'truncatedtable'+@trun_name
fetchnextfromname_cursorinto@trun_name
end
closename_cursor
deallocatename_cursor
这是我自己构造的,可以做为存储过程调用, 能够一次清空所有表的数据,并且还可以进行有选择的清空表.
3.利用微软未公开的存储过程(前提是使用sql server 2005以上版本)
execsp_msforeachtable"truncatetable?"
--删除当前数据库所有表中的数据sp_MSforeachtable@command1='Deletefrom?'sp_MSforeachtable@command1="TRUNCATETABLE?"
该方法可以一次清空所有表,但不能加过滤条件.
引:csdn,blog.csdn.net/feiji323/article/details/5669775
10. SQL清除语句
CREATE SNAPSHOT [schema.]snapshot
[ [PCTFREE integer] [PCTUSED integer]
[INITRANS integer] [MAXTRANS integer]
[TABLESPACE tablespace]
[STORAGE storage_clause]
[ USING INDEX [ PCTFREE integer | TABLESPACE tablespace
| INITTRANS integer | MAXTRANS integer
| STORAGE storage_clause ] ...
| [CLUSTER cluster (column [, column]...)] ]
[ REFRESH [FAST | COMPLETE | FORCE] [START WITH date] [NEXT date]]
AS subquery
schema
is the schema to contain the snapshot. If you omit schema, Oracle
creates the snapshot in your schema.
snapshot
is the name of the snapshot to be created.
Oracle chooses names for the table, views, and index used to
maintain the snapshot by prefixing the snapshot name. To limit
these names to 30 bytes and allow them to contain the entire
snapshot name, Oracle Corporation recommends that you limit your
snapshot names to 23 bytes.
PCTFREE
PCTUSED
INITRANS
MAXTRANS
establishes values for these parameters for the internal table
Oracle uses to maintain the snapshot's data.
TABLESPACE
specifies the tablespace in which the snapshot is to be created. If
you omit this option, Oracle creates the snapshot in the default
tablespace of the owner of the snapshot's schema.
STORAGE
establishes storage characteristics for the table Oracle uses to
maintain the snapshot's data.
USING INDEX
specifies the storage characteristics for the index on a simple
snapshot. If the USING INDEX clause not specified, the index is
create with the same tablespace and storage parameters as the
snapshot.
CLUSTER
creates the snapshot as part of the specified cluster. Since a
clustered snapshot uses the cluster's space allocation, do not use
the PCTFREE, PCTUSED, INITRANS, or MAXTRANS parameters, the
TABLESPACE option, or the STORAGE clause in conjunction with the
CLUSTER option.
REFRESH
specifies how and when Oracle automatically refreshes the snapshot:
FAST
specifies a fast refresh, or a refresh using only the
updated data stored in the snapshot log associated
with the master table.
COMPLETE
specifies a complete refresh, or a refresh that re-
executes the snapshot's query.
FORCE
specifies a fast refresh if one is possible or
complete refresh if a fast refresh is not possible.
Oracle decides whether a fast refresh is possible at
refresh time.
If you omit the FAST, COMPLETE, and FORCE options,
Oracle uses FORCE by default.
START WITH
specifies a date expression for the first automatic
refresh time.
NEXT
specifies a date expression for calculating the
interval between automatic refreshes.
Both the START WITH and NEXT values must evaluate to a time in the
future. If you omit the START WITH value, Oracle determines the
first automatic refresh time by evaluating the NEXT expression when
you create the snapshot. If you specify a START WITH value but omit
the NEXT value, Oracle refreshes the snapshot only once. If you
omit both the START WITH and NEXT values or if you omit the REFRESH
clause entirely, Oracle does not automatically refresh the snapshot.
AS subquery
specifies the snapshot query. When you create the snapshot, Oracle
executes this query and places the results in the snapshot. The
select list can contain up to 253 expressions. A snapshot query is
subject to the same restrictions as a view query.
PREREQUISITES:
To create a snapshot in your own schema, you must have CREATE
SNAPSHOT system privilege. To create a snapshot in another user's
schema, you must have CREATE ANY SNAPSHOT system privilege.
Before a snapshot can be created, the user SYS must run the SQL
script DBMSSNAP.SQL on both the database to contain the snapshot and
the database(s) containing the tables and views of the snapshot's
query. This script creates the package SNAPSHOT which contains both
public and private stored proceres used for refreshing the
snapshot and purging the snapshot log. The exact name and location
of this script may vary depending on your operating system.
When you create a snapshot, Oracle creates a table, two views, and
an index in the schema of the snapshot. Oracle uses these objects
to maintain the snapshot's data. You must have the privileges
necessary to create these objects. For information on these
privileges, see the CREATE TABLE, CREATE VIEW, and CREATE INDEX
commands.
The owner of the schema containing the snapshot must have either
space quota on the tablespace to contain the snapshot or UNLIMITED
TABLESPACE system privilege. Also, both you (the creator) and the
owner must also have the privileges necessary to issue the
snapshot's query.
To create a snapshot, you must be using Oracle with the proceral
option. To create a snapshot on a remote table or view, you must
also be using the distributed option.