Home > Database > Mysql Tutorial > body text

How to create a database in Mysql (code example)

不言
Release: 2019-01-08 09:23:43
forward
9809 people have browsed it

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
Copy after login

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
Copy after login

Specify the database encoding:

 [DEFAULT] CHARACTER SET [=] character_name
Copy after login

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

CREATE DATABASE t1 CHARACTER SET utf8
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Combined commands to delete data

DROP DATABASE t1;

<img src="https://img.php.cn//upload/image/302/419/311/1546910119173968.png" title="1546910119173968.png" alt="How to create a database in Mysql (code example)">

This article ends here. For more knowledge about MySQL, you can pay attention to the MySQL Tutorial column 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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!