码迷,mamicode.com
首页 > 数据库 > 详细

mysl 数据库 表 管理【创建 删除 修改 改名 引擎】

时间:2015-09-09 19:40:59      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:mysl 数据库 表 管理【创建 删除 修改 改名 引擎】

mysl 数据库 表 管理

MySQL中字符大小写:

1、SQL关键字及函数名不区分字符大小写;

2、数据库、表及视图名称的大小区分与否取决于低层OS及FS

3、存储过程、存储函数及事件调度器的名字不区分大小写,但触发器区分;

4、表别名区分大小写;

5、对字段中的数据,如果字段类型为Binary类型,则区分大小写;非Binary不区分大小写;


【库】:

mysql> 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


CREATE DATABASE creates a database with the given name. To use this

statement, you need the CREATE privilege for the database. CREATE

SCHEMA is a synonym for CREATE DATABASE.


URL: http://dev.mysql.com/doc/refman/5.5/en/create-database.html



mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mydb               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

【创建数据库,如果已经存在就会报错的】

mysql> create database mydb;

ERROR 1007 (HY000): Can‘t create database ‘mydb‘; database exists

【如果存在就不创建】

CREATE {DATABASE|SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] [CHARACTER SET=‘‘] [DEFAULT] [COLLATE=‘‘]

mysql> create database if not exists mydb;

Query OK, 1 row affected, 1 warning (0.03 sec)



【删除库】


mysql> help drop database;

Name: ‘DROP DATABASE‘

Description:

Syntax:

DROP {DATABASE | SCHEMA} [IF EXISTS] db_name


DROP DATABASE drops all tables in the database and deletes the

database. Be very careful with this statement! To use DROP DATABASE,

you need the DROP privilege on the database. DROP SCHEMA is a synonym

for DROP DATABASE.


*Important*: When a database is dropped, user privileges on the

database are not automatically dropped. See [HELP GRANT].


IF EXISTS is used to prevent an error from occurring if the database

does not exist.


URL: http://dev.mysql.com/doc/refman/5.5/en/drop-database.html


【删除一个不存在的数据库就会报错】

mysql> drop database haha;

ERROR 1008 (HY000): Can‘t drop database ‘haha‘; database doesn‘t exist


【如果存在就删除】

mysql> drop database if exist haha;

DROP {DATABASE | SCHEMA} [IF EXISTS] db_name


ALTER {DATABASE|SCHEMA} db_name [DEFAULT] [CHARACTER SET=‘‘] [DEFAULT] [COLLATE=‘‘]

【修改默认排序】





【表】

mysql> HELP CREATE TABLE;

【表创建】:第一种方式

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

临时表是创建在内存中,只有管理员权限才可以

(create_definition,...)

[table_options]


(create_definition,...):

字段的定义:字段名、类型和类型修饰符

键、约束或索引:

PRIMARY KEY, UNIQUE KEY, FOREIGN KEY, CHECK

{INDEX|KEY} 

把Name 设定为主键

mysql> CREATE TABLE t3 (Name VARCHAR(50) NOT NULL PRIMARY KEY,Age TINYINT UNSIGNED NOT NULL);

把Name 和 Age 都设定为主键

mysql> CREATE TABLE t3 (Name VARCHAR(50) NOT NULL ,Age TINYINT UNSIGNED NOT NULL , PRIMARY KEY(Name,Age));

mysql> DESC t3;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| Name  | varchar(50)         | NO   | PRI | NULL    |       |

| Age   | tinyint(3) unsigned | NO   | PRI | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

2 rows in set (0.15 sec)

[table_options]

ENGINE [=] engine_name

mysql> SHOW ENGINES;

AUTO_INCREMENT [=] value

[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}]

【存储引擎】

显示存储引擎,default 为默认存储引擎

mysql> mysql> SHOW ENGINES;

+--------------------+---------+

| Engine             | Support |

+--------------------+---------+

| MyISAM             | YES     |

| PERFORMANCE_SCHEMA | YES     |

| CSV                | YES     |

| BLACKHOLE          | YES     |

| MRG_MYISAM         | YES     |

| InnoDB             | DEFAULT |

| MEMORY             | YES     |

+--------------------+---------+


显示表状态 ,查看使用的存储引擎。 \G 纵向显示


mysql> SHOW TABLE STATUS LIKE ‘t3‘ \G

mysql> SHOW TABLE STATUS LIKE ‘t%‘ \G

*************************** 1. row ***************************

  Name: t3

Engine: InnoDB

Version: 10

Row_format: Compact

  Rows: 0

Avg_row_length: 0

Data_length: 16384

Max_data_length: 0

  Index_length: 0

 Data_free: 10485760

Auto_increment: NULL

Create_time: 2015-09-09 14:42:56

Update_time: NULL

Check_time: NULL

 Collation: utf8_general_ci

  Checksum: NULL

Create_options: 

Comment: 

1 row in set (0.00 sec)

【创建表时,指定存储引擎】

mysql> DROP TABLE t3;

Query OK, 0 rows affected (0.15 sec)

mysql> CREATE TABLE t3 (Name VARCHAR(50) NOT NULL ,Age TINYINT UNSIGNED NOT NULL , PRIMARY KEY(Name,Age)) ENGINE=‘MyISAM‘;

Query OK, 0 rows affected (0.07 sec)

mysql> SHOW TABLE STATUS LIKE ‘t3‘ \G

*************************** 1. row ***************************

  Name: t3

Engine: MyISAM

Version: 10

Row_format: Dynamic

  Rows: 0

Avg_row_length: 0

Data_length: 0

Max_data_length: 281474976710655

  Index_length: 1024

 Data_free: 0

Auto_increment: NULL

Create_time: 2015-09-09 15:00:20

Update_time: 2015-09-09 15:00:20

Check_time: NULL

 Collation: utf8_general_ci

  Checksum: NULL

Create_options: 

Comment: 

1 row in set (0.03 sec)


    MyISAM表,每表有三个文件,都位于数据库目录中:

    tb_name.frm: 表结构定义

    tb_name.MYD: 数据文件

    tb_name.MYI: 索引文件


    InnoDB表,有两种存储方式

    1、默认:每表有一个独立文件和一个多表共享的文件

    tb_name.frm: 表结构的定义,位于数据库目录中;

    ibdata#: 共享的表空间文件,默认位于数据目录(datadir指向的目录)中;


    2、独立的表空间:

    tb_name.frm: 每表有一个表结构文件

    tb_name.ibd: 一个独有的表空间文件

查看InnoDB表存储方式

mysql> SHOW GLOBAL VARIABLES LIKE ‘innodb%‘;

| innodb_file_per_table           | OFF                    |

【设定独立的表空间】,重启mysqld就失效

mysql> SET GLOBAL innodb_file_per_table=ON;

Query OK, 0 rows affected (0.00 sec)

mysql>  \q

mysql> SHOW GLOBAL VARIABLES LIKE ‘innodb%‘;

【修改配置文件,存储引擎永久有效】

# vim /etc/my.cnf +130

【插入】innodb_file_per_table = ON


  

表创建:第二种方式(复制表数据)

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

[(create_definition,...)]

[table_options]

select_statement


mysql> USE mydb;

Database changed

mysql> SHOW TABLES;

+----------------+

| Tables_in_mydb |

+----------------+

| t1             |

| t2             |

| t3             |

+----------------+

3 rows in set (0.01 sec)

mysql> CREATE TABLE t4 SELECT * FROM t1;

Query OK, 1 row affected (0.39 sec)

Records: 1  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+------------------+------+-----+---------+----------------+

| Field | Type             | Null | Key | Default | Extra          |

+-------+------------------+------+-----+---------+----------------+

| ID    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Name  | char(30)         | YES  |     | NULL    |                |

+-------+------------------+------+-----+---------+----------------+

2 rows in set (0.01 sec)


mysql> desc t4;

+-------+------------------+------+-----+---------+-------+

| Field | Type             | Null | Key | Default | Extra |

+-------+------------------+------+-----+---------+-------+

| ID    | int(10) unsigned | NO   |     | 0       |       |

| Name  | char(30)         | YES  |     | NULL    |       |

+-------+------------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

有问题!数据一致,结构不一样。

表创建:第三种方式(复制表结构)

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name

{ LIKE old_tbl_name | (LIKE old_tbl_name) }

mysql> CREATE TABLE t6 like t1;

Query OK, 0 rows affected (0.17 sec)


mysql> desc t6;

+-------+------------------+------+-----+---------+----------------+

| Field | Type             | Null | Key | Default | Extra          |

+-------+------------------+------+-----+---------+----------------+

| ID    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Name  | char(30)         | YES  |     | NULL    |                |

+-------+------------------+------+-----+---------+----------------+

2 rows in set (0.00 sec)



【表删除】:

    DROP [TEMPORARY] TABLE [IF EXISTS]

   tbl_name [, tbl_name] ...

   [RESTRICT | CASCADE]


【表修改】:help alter table


ALTER TABLE tbl_name

[alter_specification [, alter_specification] ...]

【插入新字段】

修改字段定义:插入新字段

mysql> desc t6;

+-------+------------------+------+-----+---------+----------------+

| Field | Type             | Null | Key | Default | Extra          |

+-------+------------------+------+-----+---------+----------------+

| ID    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Name  | char(30)         | YES  |     | NULL    |                |

+-------+------------------+------+-----+---------+----------------+

2 rows in set (0.00 sec)


mysql> ALTER TABLE t6 ADD Age TINYINT UNSIGNED NOT NULL;

Query OK, 0 rows affected (0.39 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t6;

+-------+---------------------+------+-----+---------+----------------+

| Field | Type                | Null | Key | Default | Extra          |

+-------+---------------------+------+-----+---------+----------------+

| ID    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |

| Name  | char(30)            | YES  |     | NULL    |                |

| Age   | tinyint(3) unsigned | NO   |     | NULL    |                |

+-------+---------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

插入字段指定【位置】

mysql> ALTER TABLE t6 ADD Gender ENUM(‘M‘,‘F‘) NOT NULL DEFAULT ‘M‘ FIRST; 插入在第一行

mysql> ALTER TABLE t6 ADD Gender ENUM(‘M‘,‘F‘) NOT NULL DEFAULT ‘M‘ AFTER Name ;

Query OK, 0 rows affected (0.28 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t6;

+--------+---------------------+------+-----+---------+----------------+

| Field  | Type                | Null | Key | Default | Extra          |

+--------+---------------------+------+-----+---------+----------------+

| ID     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |

| Name   | char(30)            | 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

mysql> ALTER TABLE t6 DROP Age;

Query OK, 0 rows affected (0.30 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t6;

+--------+------------------+------+-----+---------+----------------+

| Field  | Type             | Null | Key | Default | Extra          |

+--------+------------------+------+-----+---------+----------------+

| ID     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Name   | char(30)         | YES  |     | NULL    |                |

| Gender | enum(‘M‘,‘F‘)    | NO   |     | M       |                |

+--------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)



【修改字段位置】

mysql> ALTER TABLE t6 MODIFY Gender ENUM (‘M‘,‘F‘) NOT NULL AFTER ID;

Query OK, 0 rows affected (0.34 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> DESC t6;

+--------+------------------+------+-----+---------+----------------+

| Field  | Type             | Null | Key | Default | Extra          |

+--------+------------------+------+-----+---------+----------------+

| ID     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Gender | enum(‘M‘,‘F‘)    | NO   |     | NULL    |                |

| Name   | char(30)         | YES  |     | NULL    |                |

+--------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)


【修改字段名称】 CHANGE

mysql> ALTER TABLE t6 CHANGE Name StuName CHAR(30) NOT NULL;

Query OK, 0 rows affected (0.26 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> DESC t6;

+---------+------------------+------+-----+---------+----------------+

| Field   | Type             | Null | Key | Default | Extra          |

+---------+------------------+------+-----+---------+----------------+

| ID      | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| Gender  | enum(‘M‘,‘F‘)    | NO   |     | NULL    |                |

| StuName | char(30)         | NO   |     | NULL    |                |

+---------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)



添加单个【索引】,并未指定键

mysql> ALTER TABLE t6 ADD INDEX (StuName);

Query OK, 0 rows affected (0.23 sec)

Records: 0  Duplicates: 0  Warnings: 0


【显示索引】

mysql> mysql> show indexes from t6;

+-------+------------+----------+--------------+-------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name |

+-------+------------+----------+--------------+-------------+

| t6    |          0 | PRIMARY  |            1 | ID          |

| t6    |          1 | StuName  |            1 | StuName     |

+-------+------------+----------+--------------+-------------+

【删除索引】

mysql> alter table t6 drop index StuName;

Query OK, 0 rows affected (0.11 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> show indexes from t6;

mysql> show indexes from t6;

+-------+------------+----------+--------------+-------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name |

+-------+------------+----------+--------------+-------------+

| t6    |          0 | PRIMARY  |            1 | ID          |

+-------+------------+----------+--------------+-------------+



CHANGE [COLUMN] old_col_name new_col_name column_definition

[FIRST|AFTER col_name]

修改字段类型及属性等

MODIFY [COLUMN] col_name column_definition

[FIRST | AFTER col_name]

修改约束、键或索引:



【表改名】:

mysql> alter table t6 rename t7;

Query OK, 0 rows affected (0.04 sec)


mysql> show tables;

+----------------+

| Tables_in_mydb |

+----------------+

| t1             |

| t2             |

| t3             |

| t4             |

| t5             |

| t7             |

+----------------+

6 rows in set (0.00 sec)



RENAME [TO|AS] new_tbl_name


mysql> RENAME TABLE old_name TO new_name; 


mysql> rename table t7 to t6;

Query OK, 0 rows affected (0.04 sec)


mysql> show tables;

+----------------+

| Tables_in_mydb |

+----------------+

| t1             |

| t2             |

| t3             |

| t4             |

| t5             |

| t6             |

+----------------+

6 rows in set (0.00 sec)



    指定排序标准的字段:

    ORDER BY col_name [, col_name] ...


    转换字符集及排序规则:

    CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]


    表选项修改:

    [table_options]

    ENGINE [=] engine_name

    mysql> SHOW ENGINES;

    AUTO_INCREMENT [=] value

    [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}]


本文出自 “生命不息,折腾不止。” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1693197

mysl 数据库 表 管理【创建 删除 修改 改名 引擎】

标签:mysl 数据库 表 管理【创建 删除 修改 改名 引擎】

原文地址:http://990487026.blog.51cto.com/10133282/1693197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!