SQL語法基礎之DROP語句
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.查看DROP幫助信息
mysql> ? DROP Many help items for your request exist. To make a more specific request, please type 'help <item>', where <item> is one of the following topics: ALTER TABLE DEALLOCATE PREPARE DROP DATABASE DROP EVENT DROP FUNCTION DROP FUNCTION UDF DROP INDEX DROP PROCEDURE DROP RESOURCE GROUP DROP ROLE DROP SERVER DROP SPATIAL REFERENCE SYSTEM DROP TABLE DROP TABLESPACE DROP TRIGGER DROP USER DROP VIEW mysql> mysql>
二.DROP DATABASE
DROP DATABASE語句用來刪除數據庫操作,即刪除了數據庫也刪除數據庫里的所有表。
1>.查看DROP DATABASE的幫助信息

mysql> ? DROP DATABASE Name: 'DROP DATABASE' Description: Syntax: DROP {DATABASE | SCHEMA} [IF EXISTS] db_name DROP DATABASE drops all tables in the database and deletes the database. Be very careful with this statement! To use DROP DATABASE, you need the DROP privilege on the database. DROP SCHEMA is a synonym for DROP DATABASE. *Important*: When a database is dropped, privileges granted specifically for the database are not automatically dropped. They must be dropped manually. See [HELP GRANT]. IF EXISTS is used to prevent an error from occurring if the database does not exist. URL: http://dev.mysql.com/doc/refman/8.0/en/drop-database.html mysql>
2>.刪除數據庫的操作將刪除改數據庫所在文件夾乳腺癌格式的文件,以及db.opt文件
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | A1 | | A2 | | A3 | | course | | day03 | | devops | | information_schema | | mysql | | performance_schema | | sys | | yinzhengjie | +--------------------+ 11 rows in set (0.00 sec) mysql> mysql> DROP DATABASE A1; #刪除一個已經存在的庫,由於我知道該庫肯定存在,因此這里我就沒有使用判斷語句! Query OK, 1 row affected (0.00 sec) mysql> mysql> DROP DATABASE IF EXISTS A2; #刪除一個庫如果你不確定該庫是否存在時,你可以使用判斷語句,既然改庫不存在你也不怕報錯啦~ Query OK, 1 row affected (0.01 sec) mysql> mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | A3 | | course | | day03 | | devops | | information_schema | | mysql | | performance_schema | | sys | | yinzhengjie | +--------------------+ 9 rows in set (0.00 sec) mysql>
三.DROP INDEX語句
DROP INDEX 語句是用來刪除索引操作的
1>.查看DROP INDEX語句的幫助信息

mysql> ? DROP INDEX Name: 'DROP INDEX' Description: Syntax: DROP INDEX index_name ON tbl_name [algorithm_option | lock_option] ... algorithm_option: ALGORITHM [=] {DEFAULT|INPLACE|COPY} lock_option: LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE} DROP INDEX drops the index named index_name from the table tbl_name. This statement is mapped to an ALTER TABLE statement to drop the index. See [HELP ALTER TABLE]. To drop a primary key, the index name is always PRIMARY, which must be specified as a quoted identifier because PRIMARY is a reserved word: DROP INDEX `PRIMARY` ON t; URL: http://dev.mysql.com/doc/refman/8.0/en/drop-index.html mysql>
2>.案例展示
mysql> SHOW CREATE TABLE teacher; +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | teacher | CREATE TABLE `teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `course_id` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `index_teacher_id_unique` (`id`), UNIQUE KEY `index_teacher_id_comment` (`id`) COMMENT '這就是創建一個唯一索引而已!', KEY `teacher_course` (`course_id`), KEY `index_teacher_id` (`id`), KEY `index_teacher_id_and_name` (`id`,`name`), CONSTRAINT `teacher_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 | +---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> mysql> DROP INDEX index_teacher_id_and_name ON teacher; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> SHOW CREATE TABLE teacher; +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | teacher | CREATE TABLE `teacher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `course_id` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `index_teacher_id_unique` (`id`), UNIQUE KEY `index_teacher_id_comment` (`id`) COMMENT '這就是創建一個唯一索引而已!', KEY `teacher_course` (`course_id`), KEY `index_teacher_id` (`id`), CONSTRAINT `teacher_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 | +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
四.DROP TABLE語句
DROP TABLE 語句用來刪除一個或多個表操作,當然也可以刪除臨時表。
1>.查看幫助信息

mysql> ? DROP TABLE Name: 'DROP TABLE' Description: Syntax: DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE] DROP TABLE removes one or more tables. You must have the DROP privilege for each table. Be careful with this statement! It removes the table definition and all table data. For a partitioned table, it permanently removes the table definition, all its partitions, and all data stored in those partitions. It also removes partition definitions associated with the dropped table. DROP TABLE causes an implicit commit, except when used with the TEMPORARY keyword. See http://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html. *Important*: When a table is dropped, privileges granted specifically for the table are not automatically dropped. They must be dropped manually. See [HELP GRANT]. If any tables named in the argument list do not exist, the statement fails with an error indicating by name which nonexisting tables it was unable to drop, and no changes are made. Use IF EXISTS to prevent an error from occurring for tables that do not exist. Instead of an error, a NOTE is generated for each nonexistent table; these notes can be displayed with SHOW WARNINGS. See [HELP SHOW WARNINGS]. IF EXISTS can also be useful for dropping tables in unusual circumstances under which there is an entry in the data dictionary but no table managed by the storage engine. (For example, if an abnormal server exit occurs after removal of the table from the storage engine but before removal of the data dictionary entry.) The TEMPORARY keyword has the following effects: o The statement drops only TEMPORARY tables. o The statement does not cause an implicit commit. o No access rights are checked. A TEMPORARY table is visible only with the session that created it, so no check is necessary. Using TEMPORARY is a good way to ensure that you do not accidentally drop a non-TEMPORARY table. The RESTRICT and CASCADE keywords do nothing. They are permitted to make porting easier from other database systems. DROP TABLE is not supported with all innodb_force_recovery settings. See http://dev.mysql.com/doc/refman/8.0/en/forcing-innodb-recovery.html. URL: http://dev.mysql.com/doc/refman/8.0/en/drop-table.html mysql>
2>.RESTRICT/CASCADE兩個關鍵詞在5.7版本中沒用
mysql> SHOW TABLES; +--------------+ | Tables_in_A3 | +--------------+ | B3 | | v_B3 | +--------------+ 2 rows in set (0.00 sec) mysql> mysql> DROP TABLE B3; #刪除已經存在的表 Query OK, 0 rows affected (0.01 sec) mysql> mysql> DROP TABLE IF EXISTS B3; #刪除一張表,我這里加了判斷條件,意思是當這張表存在就刪除,若不存在就不執行刪除操作,這樣可以避免報錯~ Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> mysql> SHOW TABLES; +--------------+ | Tables_in_A3 | +--------------+ | v_B3 | +--------------+ 1 row in set (0.00 sec) mysql>
五.DROP VIEW語句
DROP VIEW語句用來刪除一個或多個視圖。
1>.查看DROP VIEW的幫助信息

mysql> ? DROP VIEW Name: 'DROP VIEW' Description: Syntax: DROP VIEW [IF EXISTS] view_name [, view_name] ... [RESTRICT | CASCADE] DROP VIEW removes one or more views. You must have the DROP privilege for each view. If any views named in the argument list do not exist, the statement fails with an error indicating by name which nonexisting views it was unable to drop, and no changes are made. *Note*: In MySQL 5.7 and earlier, DROP VIEW returns an error if any views named in the argument list do not exist, but also drops all views in the list that do exist. Due to the change in behavior in MySQL 8.0, a partially completed DROP VIEW operation on a MySQL 5.7 master fails when replicated on a MySQL 8.0 slave. To avoid this failure scenario, use IF EXISTS syntax in DROP VIEW statements to prevent an error from occurring for views that do not exist. For more information, see http://dev.mysql.com/doc/refman/8.0/en/atomic-ddl.html. The IF EXISTS clause prevents an error from occurring for views that don't exist. When this clause is given, a NOTE is generated for each nonexistent view. See [HELP SHOW WARNINGS]. RESTRICT and CASCADE, if given, are parsed and ignored. URL: http://dev.mysql.com/doc/refman/8.0/en/drop-view.html mysql> mysql>
2>.同樣,RESTRICT/CASCADE兩個關鍵詞在MySQL5.7依舊也是無效
mysql> SHOW TABLES; +--------------+ | Tables_in_A3 | +--------------+ | v_B3 | +--------------+ 1 row in set (0.00 sec) mysql> mysql> DROP VIEW v_B3; Query OK, 0 rows affected (0.00 sec) mysql> mysql> SHOW TABLES; Empty set (0.00 sec) mysql> mysql> DROP VIEW IF EXISTS v_B3; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> mysql> SHOW TABLES; Empty set (0.00 sec) mysql> mysql>