Getting MySQL-- basis of statements articles

Preface: 

Previous articles, we introduced the basic concepts of MySQL and logical architecture. I believe you should now have their own set of MySQL environment, then we can start practicing the MySQL. This article from the MySQL most basic statement of view, create and modify standard statements demonstrate different objects should be used for you.

1. Create a database

Create a database of official standard syntax is:

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

Wherein the content of {} is one of many contents in [] may be with or without subsequent symbols if such is the same meaning.
Generally such work often create the database:

CREATE DATABASE IF NOT EXISTS `test_db` DEFAULT CHARACTER SET utf8;

Want to see the creation of a database statement, so you can view:

mysql> show create database test_db;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| test_db  | CREATE DATABASE `test_db` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+

2. Modify the database

Little to modify the database under normal circumstances, given the official standard syntax is:

ALTER {DATABASE | SCHEMA} [db_name]
    alter_specification ...

alter_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

We may modify the database character set of exceptional circumstances, this time we can write:

ALTER DATABASE `test_db` DEFAULT CHARACTER SET utf8mb4;

3. Delete the database

Delete the database have to be careful ah! Do not delete the library on foot oh. Such requirements are generally very small, but we have to be that way, even the library will not be deleted would not be humiliated or look ~~ official document syntax:

DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

Compared create and change is concerned, it is simply deleted a lot of rude. All we operate to be more careful, do the next backup is recommended before deleting.
For example, we want to remove test_db library, we can write:

DROP DATABASE IF EXISTS `test_db`;

After you delete We then perform show database will not see test_db the library.

4. Create a table

Create a data table is a statement that we often encounter, the official reference grammar given relatively long, where the first listed in the following, in order not to take up too much space, where the picture is replaced by the code.

createtable.png

For our study and work commonly used options are summarized as follows:

CREATE TABLE <表名> ([表定义选项])[表选项][分区选项];

[表定义选项]的格式为:
<列名1> <类型1> [,…] <列名n> <类型n>

Options for creating the partition table and create a temporary table, with daily learning and not much work here is not introduced, include the following statement creates a table based on:

CREATE TABLE `user_info` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `user_id` bigint(11) NOT NULL COMMENT '用户id',
  `username` varchar(45) NOT NULL COMMENT '真实姓名',
  `email` varchar(30) NOT NULL COMMENT '用户邮箱',
  `nickname` varchar(45) NOT NULL COMMENT '昵称',
  `birthday` date NOT NULL COMMENT '生日',
  `sex` tinyint(4) DEFAULT '0' COMMENT '性别',
  `short_introduce` varchar(150) DEFAULT NULL COMMENT '一句话介绍自己,最多50个汉字',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_user_id` (`user_id`),
  KEY `idx_username`(`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息表'

Table 5. Modify

Modify the table is also a lot of available options, the option to create a reference to the above table, not to list here.
alter table to change the structure of the table, for example, you can add or delete columns, create or delete indexes, change the type of existing columns, or rename columns or the table itself. You can also change the storage engine tables or table comment. Here are several commonly used to modify the sample table:

Modify table options 

# 修改表的存储引擎
ALTER TABLE t1 ENGINE = InnoDB;

# 修改表的自增值
ALTER TABLE t1 AUTO_INCREMENT = 13;

# 修改表的字符集
ALTER TABLE t1 CHARACTER SET = utf8;

# 添加(或更改)表注释:
ALTER TABLE t1 COMMENT = 'New table comment';

# 修改表名称
ALTER TABLE t1 RENAME t2;

Fields (columns) Operation 

# 增加字段
# ALTER TABLE <表名> ADD COLUMN <新字段名> <数据类型> [约束条件] [FIRST|AFTER 已存在的字段名]
ALTER TABLE t1 ADD COLUMN col1 INT FIRST;

# 删除字段
ALTER TABLE t1 DROP COLUMN col1;

# 修改字段类型
ALTER TABLE t1 MODIFY col1 VARCHAR(30);

# 更改字段名称
ALTER TABLE t1 CHANGE col1 col2 VARCHAR(30);

Index Operations 

# 添加索引
alter table t1 add index index_name (column_list) ;
alter table t1 add unique (column_list) ;
alter table t1 add primary key (column_list) ;

# 删除索引
alter table t1 drop index index_name ;
alter table t1 drop primary key ;

6. truncate table

That table is truncated truncate table, the table can also be understood as a clear, logically, TRUNCATE TABLE DELETE like all the rows, but it bypasses the DML method of deleting data, it can not be rolled back.
truncate syntax is very simple, the official document example:

TRUNCATE [TABLE] tbl_name

7. Delete table

Delete table syntax official reference given to:

DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]

If we do not need this table, consider using this syntax, but it will delete table definitions and all table data, is not a rollback operation must be performed carefully oh.

to sum up: 

This article documents some common basis for the statement, although it looks very simple, but still recall rewarding, especially after read the official document, found that these statements are also based there are many options, but we may often use one of them. He is rarely write this series of articles, the foundation intends to write several articles Getting MySQL, and perhaps this is not very rigorous order of a few other students have any good ideas, and I welcome the communication Oh!

Guess you like

Origin www.cnblogs.com/kunjian/p/11285443.html