MySQL 15 DDL之刪除資料表

2021-09-24 20:20:25 字數 3763 閱讀 8041

drop [temporary] table [if exists] table_name [, table_name] ...[restrict | cascade]
1、drop table table_name : 刪除表全部資料和表結構,立刻釋放磁碟空間,不管是 innodb 和 myisam;

具體操作:

判斷表是否存在並刪除s1表

在某些情況下,您希望從表中刪除乙個或多個列。

示例演示:

我們在testdb資料庫中建立乙個posts資料表並刪除excerpt列並移除created_at和updated_at。

root@host# mysql -u root -p

enter password:*******

mysql> use testdb;

mysql> create table posts (id int auto_increment primary key,title varchar(255) not null,excerpt varchar(400),content text,created_at datetime,updated_at datetime);(建立posts資料表我這樣寫便於複製)

mysql>desc posts;(查詢post的表結構)

mysql>alter table posts drop column excerpt;(刪除excerpt列)

mysql>desc posts;(查詢post的表結構)

mysql>alter table posts drop column created_at,drop column updated_at;(移除created_at和updated_at這兩列)

mysql>desc posts;(查詢post的表結構)

--為表設定表模式和模式匹配(宣告兩個接受資料庫模式的變數和希望與表匹配的模式:)

set @schema = 'classicmodels';

set @pattern = 'test%';

-- 構建動態sql(droptable 1,tbl 2.)(構建乙個動態的drop table宣告:)

select concat('drop table ',group_concat(concat(@schema,'.',table_name)),';')

into @droplike

from information_schema.tables

where @schema = database()

and table_name like @pattern;

(查詢指示mysql轉到information_schema 表,它包含所有資料庫中所有表的資料,並連線資料庫中的所有表。@schema ( classicmodels ) 與模式相匹配的 @pattern ( test% ) 帶字首drop table . 這個群凹函式建立以逗號分隔的表列表。)

-- 顯示動態sql語句

select @droplike;

-- 執行動態sql

prepare stmt from @dropcmd;

execute stmt;

deallocate prepare stmt;

因此,如果要刪除資料庫中具有特定模式的多個表,只需使用上面的指令碼來節省時間。您所需要做的就是替換花紋而資料庫模式在……裡面@pattern和@schema變數。如果您經常需要處理此任務,則始終可以開發乙個儲存過程基於指令碼並重用此儲存過程。

在mysql>命令提示視窗中刪除資料表sql語句為 drop table :

例項以下例項刪除了資料表runoob_tbl:

php使用 mysqli_query 函式來刪除 mysql 資料表。

該函式有兩個引數,在執行成功時返回 true,否則返回 false。

語法

mysqli_query(connection,query,resultmode);
引數

描述connection

必需。規定要使用的 mysql 連線。

query

必需,規定查詢字串。

resultmode

可選。乙個常量。可以是下列值中的任意乙個:mysqli_use_result(如果需要檢索大量資料,請使用這個)mysqli_store_result(預設)

例項以下例項使用了php指令碼刪除資料表 runoob_tbl:

刪除資料庫

<?php 

$dbhost = 'localhost:3306'; // mysql伺服器主機位址

$dbuser = 'root'; // mysql使用者名稱

$dbpass = '123456'; // mysql使用者名稱密碼

$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

echo '連線成功

'; $sql = "drop table runoob_tbl";

mysqli_select_db( $conn, 'testdb' );

$retval = mysqli_query( $conn, $sql );

if(! $retval )

echo "資料表刪除成功\n";

mysqli_close($conn);

?>

執行成功後,我們使用以下命令,就看不到 runoob_tbl 表了:

mysql> show tables;

MySQL刪除資料

mysql通過delete從表中刪除 去掉 資料。可以從表中刪除特定的行或者從表中刪除所有的行。下面語句是從customer表中刪除一行 delete from customers where cust id 10006 先檢視表customers在刪除前的成員 select cust id,cus...

MySQL 刪除資料

從資料表中刪除資料使用 delete 語句,delete 語句允許用 where 子句指定刪除條件。語法格式 delete from table name where table name 要執行刪除操作的表 where 為可選引數,用於指定刪除條件,如果沒有 where 子句,將刪除表中的所有記錄...

MYSQL刪除資料

884 535 qq.com 一 mysql 刪除表的幾種情況 1 drop table table name 刪除表全部資料和表結構,立刻釋放磁碟空間,不管是 innodb 和 myisam 例項,刪除學生表 drop table student 2 truncate table table na...