当前位置: 移动技术网 > IT编程>数据库>Mysql > 入门MySQL——基础语句篇

入门MySQL——基础语句篇

2019年08月01日  | 移动技术网IT编程  | 我要评论

前言: 

前面几篇文章,我们介绍了mysql的基础概念及逻辑架构。相信你现在应该有了自己的一套mysql环境,接下来我们就可以开始练习mysql了。本文将从mysql最基础的语句出发,为你展示出创建及修改不同对象应该使用的标准语句。

1.创建数据库

创建数据库的官方标准语法为:

create {database | schema} [if not exists] db_name
    [create_specification] ...

create_specification:
    [default] character set [=] charset_name
  | [default] collate [=] collation_name

其中{}中的内容为多选一,[]中的内容可带可不带,后续若有此类符号也是同等意思。
一般工作中常常这样创建数据库:

create database if not exists `test_db` default character set utf8;

想要查看数据库的创建语句,可以这样查看:

mysql> show create database test_db;
+----------+------------------------------------------------------------------+
| database | create database                                                  |
+----------+------------------------------------------------------------------+
| test_db  | create database `test_db` /*!40100 default character set utf8 */ |
+----------+------------------------------------------------------------------+

2.修改数据库

一般情况下很少去修改数据库,官方给出的标准语法为:

alter {database | schema} [db_name]
    alter_specification ...

alter_specification:
    [default] character set [=] charset_name
  | [default] collate [=] collation_name

特殊情况下我们可能会修改数据库的字符集,这时候我们可以这样书写:

alter database `test_db` default character set utf8mb4;

3.删除数据库

删除数据库可要小心啊!千万不要删库跑路哦。此类需求一般极少,不过我们也要会呀,连库都不会删岂不是很没面子~~ 还是看下官方文档语法:

drop {database | schema} [if exists] db_name

相比创建和更改来讲,删除就显得简单粗暴很多。所有我们操作的时候要格外小心,删除前建议做下备份。
比如我们要删除test_db库,我们可以这样写:

drop database if exists `test_db`;

删除之后我们再执行show database就看不到test_db库了。

4.创建表

创建数据表是我们经常遇到的语句了,官方给出的参考语法比较长,这里先列出下,为了不占用太多空间,这里用代码图片代替。

createtable.png

对于我们学习及工作常用的选项归纳如下:

create table <表名> ([表定义选项])[表选项][分区选项];

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

对于临时表的创建及分区表的创建选项,日常学习及工作中用的不多,这里就不多介绍,下面列举出一个基础的创建表的语句:

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='用户信息表'

5.修改表

修改表可选选项同样是很多的,参考上面创建表的选项,这里就不一一列出了。
alter table用于更改表的结构,例如,可以添加或删除列,创建或删除索引,更改现有列的类型,或重命名列或表本身。还可以更改表的存储引擎或表注释。下面介绍下几个常用的修改表的示例:

修改表选项 

# 修改表的存储引擎
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;

字段(列)操作 

# 增加字段
# 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);

索引操作 

# 添加索引
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,也可理解为清空表,从逻辑上讲,truncate table类似于delete一个表的所有行,但它绕过了删除数据的dml方法,因此它不能回滚。
truncate语法很简单,官方文档示例:

truncate [table] tbl_name

7.删除表

删除表官方给出的参考语法为:

drop [temporary] table [if exists]
    tbl_name [, tbl_name] ...
    [restrict | cascade]

如果我们不需要这个表,可以考虑使用该语法,但是它将删除表定义和所有表数据,是不可回滚的操作,一定要小心执行哦。

总结: 

本文记录一些常用的基础语句,虽然看起来很简单,但回顾一下还是有收获的,特别是翻阅官方文档后,发现这些基础的语句也是有很多选项的,可能我们只是经常用到其中一种。自己也很少写这种系列的文章,打算多写几篇入门mysql的基础文章,可能这几篇先后顺序也不太严谨,其他同学有什么好的想法,欢迎与我沟通哦!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网