服务器端命令:
DDL:数据定义语言,主要用于管理数据库组件,例如表、索引、视图、用户、存储过程
CREATE、ALTER、DROP
DML:数据操纵语言,主要用管理表中的数据,实现数据的增、删、改、查;
INSERT, DELETE, UPDATE, SELECT
获取命令帮助:
mysql> help KEYWORD
数据库管理:
创建:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name;
[DEFAULT] CHARACTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
创建表:
CREATE TABLE [IF NOT EXISTS] tb_name (col1_def, col2_def, PRIMARY KEY(col_name,...), UNIQUE (col1,...), INDEX (col1,...)) [table_option]
使用示例:
MariaDB [datetype]> CREATE DATABASE data1 CHARSET 'UTF8';
查看支持的所有字符集:SHOW CHARACTER SET
查看支持的所有排序规则:SHOW COLLATION
修改:
ALTER {DATABASE | SCHEMA} [db_name]
[DEFAULT] CHARACTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
示例:
MariaDB [datetype]> ALTER DATABASE data1 CHARSET 'latin2' COLLATE 'latin2_bin';
删除:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
MariaDB [datetype]> DROP DATABASE data1;
Query OK, 0 rows affected (0.00 sec)
查看:
SHOW DATABASES LIKE ’‘;
MariaDB [datetype]> SHOW DATABASES LIKE 'data%'; # %任意长度的任意字符
+------------------+
| Database (data%) |
+------------------+
| data1 |
+------------------+
1 row in set (0.00 sec)
MariaDB [datetype]> SHOW DATABASES LIKE 'data_'; # _代表单个字符
表管理:
创建:
CREATE TABLE [IF NOT EXISTS] tbl_name (create_defination) [table_options]
create_defination:
字段:col_name data_type
键:
PRIMARY KEY (col1, col2, ...)
UNIQUE KEY (col1, col2,...)
FOREIGN KEY (column)
索引:
KEY|INDEX [index_name] (col1, col2,...)
CREATE TABLE t1 (id int unsigned auto_increment primary key not null,name char(5) not null);
table_options:
ENGINE [=] engine_name
查看数据库支持的所有存储引擎类型:
mysql> SHOW ENGINES;
查看某表的存储引擎类型:
mysql> SHOW TABLES STATUS [LIKE 'tbl_name']
第二种创建方式:
复制表结构;
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
注意:Storage Engine是指表类型,也即在表创建时指明其使用的存储引擎;
同一个库中表要使用同一种存储引擎类型;
修改:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...]
alter_specification:
字段:
添加:ADD [COLUMN] col_name data_type [FIRST | AFTER col_name ]
删除:DROP [COLUMN] col_name
修改:
CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
键:
添加:ADD {PRIMARY|UNIQUE|FOREIGN} KEY (col1, col2,...)
删除:
主键:DROP PRIMARY KEY
外键:DROP FOREIGN KEY fk_symbol
索引:
添加:ADD {INDEX|KEY} [index_name] (col1, col2,...)
删除:DROP {INDEX|KEY} index_name
表选项:
ENGINE [=] engine_name
查看表上的索引的信息:
mysql> SHOW INDEXES FROM tbl_name;
删除:
DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
表的引用方式:
tbl_name
db_name.tbl_name
索引:要有索引名称;
创建:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [BTREE|HASH] ON tbl_name (col1, col2,,...)
删除:
DROP INDEX index_name ON tbl_name