一、数据库的常用操作
1、创建数据库
语法:CREATE {DATABASE|SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] [CHARACTER SET=''] [DEFAULT] [COLLATE='']
说明:
DATABASE|SCHEMA:都表示为数据库
IF NOT EXISTS :判断数据库是否存在
CHARACTER SET='':指定默认字符集
COLLATE='':指定排序规则
DEFAULT:将创建数据库的选项指定为默认
创建数据库时若库名重复就会报错,而使用IF NOT EXISTS 选项就不会报错,但会有一个警告信息:
MariaDB [test]> CREATE DATABASE test; ERROR 1007 (HY000): Can't create database 'test'; database exists MariaDB [test]> CREATE DATABASE IF NOT EXISTS test; Query OK, 1 row affected, 1 warning (0.00 sec) MariaDB [test]> SHOW WARNINGS; +-------+------+-----------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------+ | Note | 1007 | Can't create database 'test'; database exists | +-------+------+-----------------------------------------------+ 1 row in set (0.00 sec)
2、删除数据库
语法:DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
3、修改数据库属性
语法:ALTER {DATABASE|SCHEMA} db_name [DEFAULT] [CHARACTER SET=''] [DEFAULT] [COLLATE='']
二、表的日常操作
1、表的创建第一种方式
语法:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
说明:
TEMPORARY :创建临时表,创建在内存中,占用内存空间。
create_definition,... 可定义字段:字段名、类型和类型修饰符。也可定义键、约束或索引:PRIMARY KEY, UNIQUE KEY, FOREIGN KEY, CHECK 或者索引:{INDEX|KEY}
举例:
注意PRIMARY KEY(Name,Age)定义了两个主键,而主键即可放在属性的后面,也可专门定义。
MariaDB [test]> CREATE TABLE t2 (Name VARCHAR(30) NOT NULL,Age TINYINT UNSIGNED NOT NULL, PRIMARY KEY(Name,Age)); Query OK, 0 rows affected (0.06 sec) MariaDB [test]> DESC t2; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | Name | varchar(30) | NO | PRI | NULL | | | Age | tinyint(3) unsigned | NO | PRI | NULL | | +-------+---------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
[table_options]:表的选项,下面列举了一些常用选项。
ENGINE [=] engine_name:指定表的存储引擎,在使用此选项前需要先查看当前系统能支持的存储引擎,查询语句为SHOW ENGINES;通过查看DEFAULT所在位置可以看到系统默认引擎,而我们在创建表时不想使用系统默认引擎则在创建表的最后加上此选项就行了。而创建后我们还可以通过SHOW TABLE STATUS LIKE 'table_name'\G;语句来查看该表所使用的引擎。
AUTO_INCREMENT [=] value :指定AUTO_INCREMENT的起始值。
[DEFAULT] CHARACTER SET [=] charset_name :设定字符集。
[DEFAULT] COLLATE [=] collation_name :指定排序规则。
COMMENT [=] 'string' :增加表的注释信息。
DELAY_KEY_WRITE [=] {0 | 1} :延迟键写入。
ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} :表格式。
TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}] :指定表空间。
2、表创建第二种方式(复制表数据,但会失去原表的属性)
语法:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
select_statement
举例:
MariaDB [test]> DESC t1; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | char(3) | YES | | NULL | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) MariaDB [test]> SELECT * FROM t1; +----+------+ | ID | Name | +----+------+ | 1 | bo | | 2 | lon | | 3 | lon | +----+------+ 3 rows in set (0.00 sec) MariaDB [test]> CREATE TABLE t4 SELECT * FROM t1; Query OK, 3 rows affected (0.07 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [test]> SELECT * FROM t4; +----+------+ | ID | Name | +----+------+ | 1 | bo | | 2 | lon | | 3 | lon | +----+------+ 3 rows in set (0.00 sec) MariaDB [test]> DESC t4; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | ID | int(10) unsigned | NO | | 0 | | | Name | char(3) | YES | | NULL | | +-------+------------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
3、表创建:第三种方式(复制表结构,但会没有原表的数据)
语法:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
举例:
MariaDB [test]> CREATE TABLE t5 LIKE t1; Query OK, 0 rows affected (0.04 sec) MariaDB [test]> DESC t5; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | char(3) | YES | | NULL | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) MariaDB [test]> SELECT * FROM t5; Empty set (0.00 sec)
上面两种方法创建表都有其优缺点,然而实际使用中还是一第三种,即先复制原表的结构然后在使用INSERT语句进行插入数据。
4、表删除
语法:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]:若表中有依赖则使用CASCADE解除依赖。
5、表属性修改
语法:
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
插入新字段:ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
举例:
MariaDB [test]> DESC t5; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | char(3) | YES | | NULL | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) MariaDB [test]> ALTER TABLE t5 ADD Age TINYINT UNSIGNED NOT NULL; Query OK, 0 rows affected (0.14 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> DESC t5; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | char(3) | YES | | NULL | | | Age | tinyint(3) unsigned | NO | | NULL | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
放在指定位置后面:
MariaDB [test]> ALTER TABLE t5 ADD Gender ENUM('M','F')NOT NULL DEFAULT 'M' AFTER Name; Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> DESC t5; +--------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Name | char(3) | YES | | NULL | | | Gender | enum('M','F') | NO | | M | | | Age | tinyint(3) unsigned | NO | | NULL | | +--------+---------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
删除字段:DROP [COLUMN] col_name
举例:
MariaDB [test]> ALTER TABLE t5 DROP Gender; Query OK, 0 rows affected (0.09 sec) Records: 0 Duplicates: 0 Warnings: 0
修改字段名称
语法:CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
举例:
MariaDB [test]> ALTER TABLE t5 CHANGE Name StuName CHAR(20) NOT NULL; Query OK, 0 rows affected (0.14 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> DESC t5; +---------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Age | tinyint(3) unsigned | YES | | NULL | | | StuName | char(20) | NO | | NULL | | +---------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
修改字段类型及属性等
语法:MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
举例:
MariaDB [test]> ALTER TABLE t5 MODIFY Age TINYINT UNSIGNED AFTER ID; Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> DESC t5; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | ID | int(10) unsigned | NO | PRI | NULL | auto_increment | | Age | tinyint(3) unsigned | YES | | NULL | | | Name | char(3) | YES | | NULL | | +-------+---------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
修改约束、键或索引
语法:ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option]
举例:
MariaDB [test]> ALTER TABLE t5 ADD INDEX (StuName); Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> SHOW INDEXES FROM t5; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | t5 | 0 | PRIMARY | 1 | ID | A | 0 | NULL | NULL | | BTREE | | | | t5 | 1 | StuName | 1 | StuName | A | 0 | NULL | NULL | | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec) MariaDB [test]> ALTER TABLE t5 DROP INDEX StuName; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> SHOW INDEXES FROM t5; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | t5 | 0 | PRIMARY | 1 | ID | A | 0 | NULL | NULL | | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 1 row in set (0.00 sec)
6、表改名
语法:RENAME [TO|AS] new_tbl_name
举例:
mysql> RENAME TABLE old_name TO new_name;
7、修改表的存储引擎
举例:
MariaDB [test]> SHOW TABLE STATUS LIKE 't5'\G; *************************** 1. row *************************** Name: t5 Engine: InnoDB Version: 10 Row_format: Compact ************************************************************** MariaDB [test]> ALTER TABLE t5 ENGINE=MyISAM; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [test]> SHOW TABLE STATUS LIKE 't5'\G; *************************** 1. row *************************** Name: t5 Engine: MyISAM Version: 10 Row_format: Fixed **************************************************************
注意:此操作很危险,一般不建议在生产环境中进行此操作。
8、指定排序标准的字段
语法:ORDER BY col_name [, col_name] ...
9、转换字符集及排序规则
语法:CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
在修改表选项时,其所可以使用的选项与创建时的选项通用。
三、MyISAM和InnoDB表的存储结构
1、MyISAM表,每表有三个文件,都位于数据库目录中
tb_name.frm: 表结构定义
tb_name.MYD: 数据文件
tb_name.MYI: 索引文件
2、InnoDB表,有两种存储方式
默认:每表有一个独立文件和一个多表共享的文件;
tb_name.frm: 表结构的定义,位于数据库目录中;
ibdata#: 共享的表空间文件,默认位于数据目录(datadir指向的目录)中。
独立的表空间:
tb_name.frm: 每表有一个表结构文件;
tb_name.ibd: 一个独有的表空间文件。
在InnoDB的表存储中,第一种方法由于ibdata#是多个表共享的文件,所以数据库使用时间一长就极容易造成该文件过大,从而导致不容易管理数据文件,而第二种方法即使用独立的表空间就不会出现此类状况,所以在系统初始化时还是使用第二种方法好,方便人们管理,下面就是介绍如何在InnoDB引擎中设置其存储方法为独立表空间。
首先产看数据库中关于innodb设置的选项:
MariaDB [test]> SHOW GLOBAL VARIABLES LIKE 'innodb%';
从输出的结果来看mariadb-10.0已经默认将其打开了。
| innodb_file_per_table | ON
若该选项是关闭的可以通过命令行进行设置,但是此种设置一旦系统重启就会失效。
MariaDB [test]> SET GLOBAL innodb_file_per_table=ON;
若想永久有效则在配置文件中[mysqld]模块后写入启动该选项。
[[email protected] ~]# vim /etc/my.cnf [mysqld] innodb_file_per_table = ON