MySQL - operation of the library

content

create database

The impact of different validation rules on the database

manipulate database


create database

Let's first look at the syntax for database creation:

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
  • capitalize keywords
  • [] is optional
  • CHARACTER SET: Specifies the character set used by the database
  • COLLATE: Specifies the validation rule for the database character set

Let's take a look at the actual database created

//创建名为db1的数据库
create database db1;

This creation does not specify the character set and validation rules used. The default character set is: utf8, and the validation rule is: utf8_general_ci.

Next, let's create two specific databases, ① a db2 database using the utf8 character set and ② a db3 database using the utf character set with collation rules.

create database db2 charset=utf8;
create database db3 charset=utf8 collate utf8_general_ci;

The impact of different validation rules on the database

We can first use show collation to view the validation rules contained in the database

show collation;

 Among them, utf8_general_ci and utf8_bin, the former is case-insensitive, and the latter is case-sensitive.

//使用校验规则utf8_general_ci(不区分大小写)
create database db1 collate utf8_general_ci;
use db1;
create table tb1(name varchar(20));//创建表tb1
insert into tb1 values('a');
insert into tb1 values('A');
insert into tb1 values('b');


//使用校验规则utf8_bin(区分大小写)
create database db2 collate utf8_bin;
use db2;
create table tb2(name varchar(20));
insert into tb2 values('a');
insert into tb2 values('A');

Let's take a look at the two tables created above, tb1 and tb2.

 

manipulate database

//查看数据库
show databases;

//显示创建语句
show create database 数据库名;

 //修改数据库语法
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
//将db1数据库字符集改成gbk
alter database db1  charset=gbk;

//数据库删除
DROP DATABASE [IF EXISTS] db_ name;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324305622&siteId=291194637