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>

mysql> ? DROP DATABASE

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>

mysql> ? DROP INDEX

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>

mysql> ? DROP TABLE

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>

mysql> ? DROP VIEW

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>

SQL语法基础之DROP语句的更多相关文章

  1. SQL语法基础之ALTER语句

    SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...

  2. SQL语法基础之DELETE语句

    SQL语法基础之DELETE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看DELETE的帮助信息 mysql> ? DELETE Na ...

  3. SQL语法基础之CREATE语句

    SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...

  4. SQL语法基础之INSEART语句

    SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...

  5. SQL语法基础之UPDATE语句

    SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...

  6. SQL语法基础之高级应用

    SQL语法基础之高级应用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程与函数 1>.CREATE PROCEDURE 用来创建存储过程 mysql> ? ...

  7. SQL语法基础之SELECT

    SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...

  8. SQL——语法基础篇(上)

    用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...

  9. oracle PL/SQL语法基础

    目录 数据类型 定义变量 PL/SQL控制结构 参考资料 Oracle10g数据类型总结 PL/SQL之基础篇 数据类型 学习总结 字符类型 char.nchar.varchar.nvarchar:有 ...

随机推荐

  1. python基础数据类型--dict 字典

    字典 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必 ...

  2. 第五十六 css选择器和盒模型

    1.组合选择器 群组选择器 #每个选择为可以位三种基础选择器任意一个,用逗号隔开,控制多个. div,#div,.div{ color:red } 后代(子代)选择器 .sup .sub{ 后代 } ...

  3. AtcoderARC062F Painting Graphs with AtCoDeer 【双连通分量】【polya原理】

    题目分析: 如果一个双连通分量是简单环,那么用polya原理计数循环移位即可. 如果一个双连通分量不是简单环,那么它必然可以两两互换,不信你可以证明一下相邻的可以互换. 如果一条边是桥,那么直接乘以k ...

  4. BZOJ5296 [CQOI2018] 破解D-H协议 【数学】【BSGS】

    题目分析: 裸题. 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; #define mp ...

  5. 洛谷P1501 Tree II

    LCT 还是LCT的板子,下放标记和那道线段树2一样,先放乘..之前用char忘记getchar,调了好久... 注意开long long!! #include <bits/stdc++.h&g ...

  6. [USACO18DEC]Balance Beam

    题目链接:这里 或者这里 答案是很显然的,记\(g(i)\)为在\(i\)下平衡木时的期望收益 那么\(g(i)=max(f(i),\frac{g(i-1)+g(i+1)}{2})\) 好了做完了 T ...

  7. 【XSY2741】网格 分治 LCT 并查集

    题目描述 有一个\(n\times m\)的网格,线框的交点可以扭动,边不可伸缩.网格中有一些格子里面放了'x'形的支架,这些格子不会变形,但可以整体转动.如果所有格子都不能变形,那么称这个网格稳固. ...

  8. 【XSY1544】fixed 数学 强连通图计数

    题目描述 ​ 给你一个\(n\times n\)的方阵\(A\).定义方阵\(A\)的不动点\((i,j)\)为:\(\forall p,q\geq 0,(A^p)_{i,j}=(A^q)_{i,j} ...

  9. js-基本语法2

    类型转换 1.直接转换 parseInt() 与 parseFloat() alert('12'+7); //弹出127 alert( parseInt('12') + 7 ); //弹出19 ale ...

  10. GuGuFishtion HDU - 6390 (杭电多校7E)

    啊啊啊啊...全在纸上 字丑...算了算了 然后除法部分都用逆元就好了 还有逆元打表....学到了...牛逼 #include<map> #include<set> #incl ...