SQL basic grammar - other statements

1 alter database statement

alter databaseStatement is used to modify the properties of a database

Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
    alter_specification ...

alter_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

db_nameCan not specify, if not specify a description is to modify the properties of the current database;

character setRepresentatives modify the default character set of the database;

collateIt represents the default collation of the database changes;

If you modify the default character set or collation of the database, all stored procedures and functions that the database will need to re-create it again.

2 alter view statement

alter viewStatement is used to modify the definition of the view, grammatical structure itself and create viewthe same, and the role of statements create or replace viewstatements same.

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]

Example:

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 statement

drop databaseStatement is used to delete database operations, the database is deleted also delete all the tables library:

DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

Deleting a database deletes the database where the folder .bak .dat .hsh .mrg .myd .myi .trg .trn .cfg .db .frm .ibd .ndb .parand db.optfile.

Example:

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 statement

drop indexStatement is used to delete the index operation:

DROP INDEX index_name ON tbl_name

Delete tbl_namethe table name index_nameindex.

Example:

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 statement

drop tableStatement is used to delete one or more operating table, of course, you can also delete temporary table:

Syntax:
DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]

RESTRICT/CASCADETwo key words useless in 5.7 version.

Example:

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 statement

drop viewStatement is used to delete one or more views

Syntax:
DROP VIEW [IF EXISTS]
    view_name [, view_name] ...
    [RESTRICT | CASCADE]

RESTRICT/CASCADETwo key words useless in 5.7 version.

Example:

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 statement

rename tableStatement is used to rename one or more tables:

Syntax:
RENAME TABLE
    tbl_name TO new_tbl_name
    [, tbl_name2 TO new_tbl_name2] ...

When you want two table names mutual exchange, which executes the statement:

RENAME TABLE old_table TO tmp_table,
    new_table TO old_table,
    tmp_table TO new_table;

rename tableData can tables, indexes, primary key definition are automatically converted to the new table, but the view of the original table and assign permissions automatically converted to the new table does not need to be performed manually.

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 statement

truncate tableStatement is used to delete all data / truncate table.

TRUNCATE [TABLE] tbl_name

And deletedelete all data the same meaning in logic, but faster performance. Similar executed drop tableand create tabletwo statements.

Example:

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 statements

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]

Guess you like

Origin www.cnblogs.com/dabric/p/12389521.html