服務器端命令:mysql
DDL:數據定義語言,主要用於管理數據庫組件,例如表、索引、視圖、用戶、存儲過程sql
CREATE、ALTER、DROP數據庫
DML:數據操縱語言,主要用管理表中的數據,實現數據的增、刪、改、查;bash
INSERT, DELETE, UPDATE, SELECT服務器
獲取命令幫助:ide
mysql> help KEYWORDspa
數據庫管理:orm
建立:排序
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