当前位置:网站首页>MySQL - library operation

MySQL - library operation

2022-06-21 10:48:00 Longbow dog learning C

Catalog

Create database

The impact of different validation rules on the database

Manipulating databases


Create database

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

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
  • Uppercase means keywords
  • [] Optional.
  • CHARACTER SET: Specifies the character set used by the database
  • COLLATE: Specifies the verification rules for the database character set

Let's take a look at the database created in practice

// Create a db1 The database of 
create database db1;

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

Next, let's create two specific databases ,① Use utf8 Of the character set db2 Database and ② Use utf Character set , With proofreading rules db3 database .

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 use show collation View the validation rules contained in the database

show collation;

  among utf8_ general_ ci and utf8_ bin, The former is not case sensitive , The latter is case sensitive .

// Use validation rules utf8_general_ci( Case insensitive )
create database db1 collate utf8_general_ci;
use db1;
create table tb1(name varchar(20));// Create table tb1
insert into tb1 values('a');
insert into tb1 values('A');
insert into tb1 values('b');


// Use validation rules utf8_bin( Case sensitive )
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.

 

Manipulating databases

// view the database 
show databases;

// Show create statement 
show create database  Database name ;

 // Modify the database Syntax 
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
// take db1 The database character set is changed to gbk
alter database db1  charset=gbk;

// Database delete 
DROP DATABASE [IF EXISTS] db_ name;

原网站

版权声明
本文为[Longbow dog learning C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221438095140.html