标签:har ict mysql服务器 hone iter 规则 注意 结构 rename
操作对象:数据库
1)创建数据库
MariaDB [(none)]> help create database
Name: ‘CREATE DATABASE‘
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name(指定字符集)
| [DEFAULT] COLLATE [=] collation_name(指定排序规则)
查看系统默认支持的字符集和排序规则:
MariaDB [(none)]> show character set;
MariaDB [(none)]> show collation;
我们可以在创建数据库的时候,指定字符集和排序规则,如果不指定,则默认使用mysql服务器级别指定的字符集和排序规则
案例:
创建discuz数据库
MariaDB [(none)]> create database discuz default character set utf8;
Query OK, 1 row affected (0.00 sec)
2)修改数据库
MariaDB [(none)]> help alter database
Name: ‘ALTER DATABASE‘
Description:
Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...(修改数据库的字符集和排序规则)
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
(注意:一般工作中,很少用到)
3)删除数据库
MariaDB [(none)]> help drop database
Name: ‘DROP DATABASE‘
Description:
Syntax:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
案例:
MariaDB [(none)]> drop database discuz;
Query OK, 0 rows affected (0.00 sec)
操作对象:表
1)创建表
三种方式创建表
MariaDB [(none)]> help create table
Name: ‘CREATE TABLE‘
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
select_statement
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition:
col_name column_definition
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT ‘string‘]
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition]
reference_definition:
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
table_option:
ENGINE [=] engine_name
| AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] ‘string‘
| CONNECTION [=] ‘connect_string‘
| DATA DIRECTORY [=] ‘absolute path to directory‘
| DELAY_KEY_WRITE [=] {0 | 1}
| INDEX DIRECTORY [=] ‘absolute path to directory‘
| INSERT_METHOD [=] { NO | FIRST | LAST }
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| PACK_KEYS [=] {0 | 1 | DEFAULT}
| PASSWORD [=] ‘string‘
| ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}]
| UNION [=] (tbl_name[,tbl_name]...)
总之:
create table 表名 (字段1 datatype 修饰符,字段2 datatype 修饰符......)table_option
例1
MariaDB [testdb]> create table tbl1 (id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.09 sec)
例2
MariaDB [testdb]> create table products (prod_id char(10) not null,vend_id char(10) not null,prod_name varchar(254) not null,prod_price decimal(8,2) not null,prod_desc varchar(1000));
Query OK, 0 rows affected (0.07 sec)
查看表结构
MariaDB [testdb]> desc products;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| prod_id | char(10) | NO | | NULL | |
| vend_id | char(10) | NO | | NULL | |
| prod_name | varchar(254) | NO | | NULL | |
| prod_price | decimal(8,2) | NO | | NULL | |
| prod_desc | varchar(1000) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
2)删除表
MariaDB [testdb]> help drop table
Name: ‘DROP TABLE‘
Description:
Syntax:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
MariaDB [testdb]> drop table tbl1;
Query OK, 0 rows affected, 1 warning (0.03 sec)
3)修改表
MariaDB [testdb]> help alter table
Name: ‘ALTER TABLE‘
Description:
Syntax:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name ]
| ADD [COLUMN] (col_name column_definition,...)
| ADD {INDEX|KEY} [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD SPATIAL [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
| CHANGE [COLUMN] old_col_name new_col_name column_definition
[FIRST|AFTER col_name]
| MODIFY [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP {INDEX|KEY} index_name
| DROP FOREIGN KEY fk_symbol
| MAX_ROWS = rows
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO|AS] new_tbl_name
| ORDER BY col_name [, col_name] ...
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
| FORCE
| ADD PARTITION (partition_definition)
| DROP PARTITION partition_names
| TRUNCATE PARTITION {partition_names | ALL}
| COALESCE PARTITION number
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)]
| ANALYZE PARTITION {partition_names | ALL}
| CHECK PARTITION {partition_names | ALL}
| OPTIMIZE PARTITION {partition_names | ALL}
| REBUILD PARTITION {partition_names | ALL}
| REPAIR PARTITION {partition_names | ALL}
| PARTITION BY partitioning_expression
| REMOVE PARTITIONING
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT ‘string‘
table_options:
table_option [[,] table_option] ... (see CREATE TABLE options)
partition_options:
(see CREATE TABLE options)
为表增加一个字段
MariaDB [testdb]> ALTER TABLE products ADD vend_phone CHAR(20);
标签:har ict mysql服务器 hone iter 规则 注意 结构 rename
原文地址:http://www.cnblogs.com/51yuki/p/SQL01.html