如何刪除數據庫中所有數據
要刪除MySQL數據庫中的所有數據,有幾種方法:1、刪除數據庫里所有內容,包括表:可以刪除數據庫然后新建數據庫就好了。
方法:drop database if exists 'dataBaseName'; CREATE DATABASE IF NOT EXISTS `dataBaseName`這種方式快捷,一步到位。還可以用如下方法刪除整張表:truncate tableName;delete from tableName;上面兩種方法里,第一個直接刪除表,不放到回收站;第二種方法刪除表之后會放到回收站;2、連同數據庫用戶也刪除,以后不用了,那可以只做:drop database if exists 'dataBaseName'; 即可;3、只刪除數據庫里的數據:delete from tableName;刪除表里的全部數據。
刪除表內所有數據效率最高的語句
truncate table 表名 速度快,而且效率高,因為:
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 不能用于參與了索引視圖的表。
sql刪除數據庫所有表
1.搜索出所有表名,構造為一條SQL語句 declare @trun_name varchar(8000)
set @trun_name=''
select @trun_name=@trun_name + 'truncate table ' + [name] + ' ' from sysobjects where xtype='U' and status > 0
exec (@trun_name)該方法適合表不是非常多的情況,否則表數量過多,超過字符串的長度,不能進行完全清理. 2.利用游標清理所有表 declare @trun_name varchar(50)
declare name_cursor cursor for
select 'truncate table ' + name from sysobjects where xtype='U' and status > 0
open name_cursor
fetch next from name_cursor into @trun_name
while @@FETCH_STATUS = 0
begin
exec (@trun_name)
print 'truncated table ' + @trun_name
fetch next from name_cursor into @trun_name
end
close name_cursor
deallocate name_cursor
這是我自己構造的,可以做為存儲過程調用, 能夠一次清空所有表的數據,并且還可以進行有選擇的清空表. 3.利用微軟未公開的存儲過程 exec sp_msforeachtable "truncate table ?"
mysql數據庫刪除數據語句怎么寫
方法/步驟
查詢數據:select * from xxx;
例子:
(1)select id,username,password from t_user;
(2)select id,username,password,gender from t_user where gender = '男';
(3)select id,username,password,gender from t_user where gender is null;
添加數據:insert xxx(id, username) values(xx, "xxx");
例子:
insert into t_user(id, username) values(10, "hehehe");
insert into t_user(id, gender, username, age, password) values(15, '男', 'shihu', 18, '123456');
insert into t_user values(16, 'benladeng', '123456', '拉登', 'nan', 18);
修改數據:update tablename set xx=xx,xxx=xx where xxx=xxx and xxx=xxx;
刪除數據:delete from tablename where xx=xxx and xxx = xxx or xxx = xxx;
DQL數據查詢語言
連接查詢
左外連接
select
* ename,
*,
* dname
from
t_employee e left outer join t_department d
on
*_id = *
6
設置數據庫可以被其他計算機連接
打開名為mysql的數據庫 --> user表 -->; 將root的host改為% -->; 提交 -->; 重啟mysql服務即可。