1 alter database语句
alter database
语句用来修改数据库的属性
Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
db_name
可以不指定,如果不指定说明是修改当前数据库的属性;
character set
代表修改数据库的默认字符集;
collate
代表修改数据库的默认排序规则;
如果修改了数据库的默认字符集或排序规则,那数据库中的所有存储过程和函数都需要重新创建一遍。
2 alter view语句
alter view
语句用来修改视图的定义,本身的语法结构和create view
相同,语句所起到的作用和create or replace view
语句相同。
Syntax:
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = { user | CURRENT_USER }]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW view_name [(column_list)]
AS select_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
示例:
mysql> alter view v_students_male as select sid,sname from students where sex=0;
mysql> select * from v_students_male;
+------+-------+
| sid | sname |
+------+-------+
| 1 | aaa |
3 drop database语句
drop database
语句用来删除数据库操作,即删除了数据库也删除了库里的所有表:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
删除数据库的操作将删除该数据库所在文件夹下的.bak .dat .hsh .mrg .myd .myi .trg .trn .cfg .db .frm .ibd .ndb .par
以及db.opt
文件。
示例:
mysql> drop database test2;
Query OK, 1 row affected (0.07 sec)
mysql> drop database if exists test4;
Query OK, 0 rows affected (0.00 sec)
4 drop index语句
drop index
语句用来删除索引操作:
DROP INDEX index_name ON tbl_name
删除tbl_name
表中的名字叫index_name
的索引。
示例:
mysql> drop index idx_st_sname on students;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
5 drop table语句
drop table
语句用来删除一个或多个表操作,当然也可以删除临时表 :
Syntax:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
RESTRICT/CASCADE
两个关键词在5.7版本中没用。
示例:
mysql> drop table students2;
Query OK, 0 rows affected (0.01 sec)
mysql> drop table if exists students2;
Query OK, 0 rows affected, 1 warning (0.00 sec)
6 drop view语句
drop view
语句用来删除一个或多个视图
Syntax:
DROP VIEW [IF EXISTS]
view_name [, view_name] ...
[RESTRICT | CASCADE]
RESTRICT/CASCADE
两个关键词在5.7版本中没用。
示例:
mysql> drop view v_students_male;
Query OK, 0 rows affected (0.00 sec)
mysql> drop view if exists v_students_male;
Query OK, 0 rows affected, 1 warning (0.00 sec)
7 rename table语句
rename table
语句用来重命名一个或多个表:
Syntax:
RENAME TABLE
tbl_name TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...
当想让两个表名相互调换时,可以执行语句:
RENAME TABLE old_table TO tmp_table,
new_table TO old_table,
tmp_table TO new_table;
rename table
能将表中的数据,索引,主键定义都自动转换到新表下,但视图和对原表分配的权限不能自动转换到新表,需要手动执行。
mysql> rename table students to students_test;
Query OK, 0 rows affected (0.03 sec)
mysql> show create table students_test;
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students_test | CREATE TABLE `students_test` (
`sid` int(11) DEFAULT NULL,
`sname` varchar(20) DEFAULT NULL,
`sex` int(11) DEFAULT NULL,
UNIQUE KEY `idx_st_sid` (`sid`),
KEY `idx_st_union` (`sname`,`sex`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
mysql> select * from v_students_female; ##原有视图查询失败
ERROR 1356 (HY000): View ‘test.v_students_female‘ references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
8 truncate table语句
truncate table
语句用来删除/截断表里的所有数据。
TRUNCATE [TABLE] tbl_name
和delete
删除所有数据在逻辑上含义相同,但性能更快。类似执行了drop table
和create table
两个语句。
示例:
mysql> truncate table students_test;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from students_test; ##查询结果为空
Empty set (0.00 sec)
9 alter table语句
Alter table … add [column_name]
Alter table … add constraint [name] unique [index/key] [name]
Alter table … add constraint [name] foreign key (column_name) references table_name(column_name)
Alter table … drop column [column_name]
Alter table … drop [index/key] [index_name]