The main purpose of this article: How to add, delete, query and modify the database
Article Directory
Create database create
The main details are in the option question, the encoding option
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
illustrate:
Uppercase indicates that the keyword
[] is optional
CHARACTER SET: specifies the character set used by the database
COLLATE: specifies the validation rules for the database character set
- View the list of current user databases show databases;
- Create database create database db_name;
When we create data without specifying character set and verification rules, the system uses the default character set: utf8, verification rules: utf_general_ci;
Simply verify: create a database create database d1, and then go to /var/lib/mysql/d1/db.opt to view:,
- Delete database drop database db_name;
Create a database: create database db_name (essentially, Linux creates a directory in /var/lib/mysql), delete the database: drop database db_name; (delete the directory)
For example, we create a directory youcanseeme under /var/lib/mysql, and use the mysql command show databases; it can also be seen naturally. (But it is very unreasonable to manually create a directory with mkdir in /var/lib/mysql)
- Create a database that does not exist (if not exists) create database if not exists database1;
- database encoding problem
创建数据库的时候,有两个编码集:1.数据库编码集 2.数据库校验集
Database encoding set - the database will store data in the future
Database check set ——supports the encoding used by the database for field comparison, and is essentially an encoding format used to read data in the database
No matter what operation the database performs on the data, it must ensure that the operation and encoding must be consistent! **Prevent garbled characters
- View the system default character set and verification rules
Encoding set: show variables like 'character_set_database';
Validation set: show variables like 'collation_database';
In addition to the database, what about others: show variables like 'collation_%';
If you want to view the character set supported by the database: show charset;
If you want to view the character set verification rules supported by the database: show collation;
- Create a database with the specified encoding set
If not specified, the configuration file is the default
There are two ways to create a table whose character set is utf8:
create database d2 charset=utf8;
create database d3 character set utf8;
Set the character set and checksum at the same time:
- The impact of validation rules on the database
To illustrate this situation, we now create two databases:
The test1 database verification set is set to utf8_general_ci; the default character set is utf8; the verification rule uses utf8_ general_ ci [case-insensitive]
The test2 database verification set is set to utf8_bin; the character set defaults to utf8; the verification rule uses utf8_ bin [case-sensitive]
- test1 database
Now look at the database test1 first, insert data into the database test1, first use the database use test1; create a table person
create table if not exists person (name varchar(20));
Insert data:
mysql> insert into person (name) values ('a');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('b');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('A');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('B');
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (name) values ('c');
Query OK, 1 row affected (0.01 sec)
mysql> insert into person (name) values ('D');
Query OK, 1 row affected (0.00 sec)
At this time, the table person, query table person:
Query a in the person table: the result is that both uppercase and lowercase can be found, but utf8_general_ci is not case sensitive:
Take a look at the sorted results:
The verification rule in test1 is that when utf8_general_ci is compared, case is not distinguished when performing verification, and the verification set will affect the result. Generally, we follow the default
- test2 database
Now look at the database test2, use the database use test2;, create a table person:
create table if not exists person (name varchar(20));
Insert data, view table person:
Query a in the person table: the result is only lowercase: this is utf8_bin case-sensitive:
For sorting, the person in the database test2 is in ascending order by default, according to the ascii value from small to large:
view databaseshow
show databases;
- use database use db_name;
After checking, you can’t use the database directly. If you want to use the database, use use+database name; such as using the database helloworld;
use helloworld;
- Confirm the currently used database select database();
select database();
Currently using the test1 database
delete database drop
DROP DATABASE [IF EXISTS] db_ name;
The result after performing the delete:
The database folder corresponding to the corresponding database cannot be seen inside the database, and
the corresponding database folder is deleted, cascading deletion, and all the data tables in it are deletedNote: Do not delete the database at will
//删除数据d5\d4\d3;
mysql> drop database d5;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database d4;
Query OK, 0 rows affected (0.00 sec)
mysql> drop database d3;
Query OK, 0 rows affected (0.00 sec)
Modify the database alter
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
Use alter to modify the database
The modification of the database mainly refers to modifying the character set of the database, and the verification rules
alter database test2 charset=gbk collate gbk_chinese_ci;
After modification, view the information of database test2:
/*!40100 DEFAULT CHARACTER SET gbk */: This is not a comment, indicating that the current mysql version is greater than version 4.01, just execute this sentence