Home>Article>Database> How to create a database in Mysql (code example)

How to create a database in Mysql (code example)

不言
不言 forward
2019-01-08 09:21:29 9750browse

The content of this article is about how to create a database in Mysql. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In order to work more clearly in Mysql, a set of specifications and several commonly used commands are customized. Next I will learn how to create and operate a database of my own.

Grammar specifications

  • Keywords and function names must be capitalized

  • Database name, table name , field names must be all lowercase

  • SQL statements must end with a semicolon

Commonly used commands

  • Display the current server version

  • SELECT VERSION();

  • Display the current date and time

  • SELECT NOW();

  • Display the current user

  • SELECT USER();

Note: All must end with a semicolon.

Create your own database

To create a database in MYSQL, let’s first understand the commands to create a database

Create a database:

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

In the above syntax, {} is one of the required ones, and [] can be written or not. Recombine this into one command to create the database

//创建一个命名为t1的数据库 CREATE DATABASE t1

Specify the database encoding:

[DEFAULT] CHARACTER SET [=] character_name

According to the previous syntax definition, we recombine the command.

CREATE DATABASE t1 CHARACTER SET utf8

After we create the database, how do we check whether it was created successfully? It is very simple to use the SHOW command to print out all our data

SHOW DATABASES

How to create a database in Mysql (code example)

You can see that the data list exists in the t1 and t2 databases.

Modify the specified database settings

ALTER { DATABASES|SCHEMA } [db_name] [DEFAULT] CHARACTER SET [=] character_name

Combined command to modify the t2 database to gbkB encoding:

ALTER DATABASE t2 CHARACTER SET gbk

Let’s check What is the current encoding of the t1 database:
Command:

SHOW CREATE DATABASE t1

How to create a database in Mysql (code example)

Now we can see that the encoding of the t1 database is already gbk.

Then t1 and t2 are used for testing, we need to delete them now. Just DROP the keyword.

Grammar rules:

DROP {DATABASE | SCHEMA } [IF EXISTS] db_name

Combined commands to delete data

DROP DATABASE t1;

How to create a database in Mysql (code example)

This article ends here. For more knowledge about MySQL, you can pay attention to theMySQL Tutorialcolumn on the php Chinese website! ! !

The above is the detailed content of How to create a database in Mysql (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete