MySQL creates database and creates data table

MySQL creates a database and creates a data table.
MySQL is the most commonly used database. In database operations, they are basically add, delete, modify, and query operations, referred to as CRUD.

Before doing this, you need to install MySQL first, and then create a database, data table, and operating user.

1. Database operating language

When operating the database, you need to use special database operation rules and syntax. This syntax is SQL (Structured Query Language).

The main function of SQL is to establish a connection with the database and perform addition, deletion, modification and query operations. SQL is the standard language for relational database management systems.

The role of SQL language:

1. Data Definition Language DDL (Data Definition Language). Used to create databases, data tables.

2. Data Manipulation Language DML (Data Manipulation Language). Used to insert, modify, and delete data from the data table.

3. Data Query Language DQL (Data Query Language). Used to query data from a data table.

4. Data Control Language DCL (Data Control Language). Used to set or modify the permissions of database users or roles.

When using SQL to operate the database, all SQL statements end with a semicolon. (You don’t need a semicolon when switching databases)

In SQL statements, there is no case sensitivity. When writing SQL statements, you can use case distinction according to the situation to increase readability.

2. Create database

1. Connect to MySQL

Enter the mysql -u root -p command, press Enter, then enter the MySQL password (don't forget the password), press Enter again, and you will be connected to MySQL.

mysql -u root -p


Initially, the root user is used to log in. If you always log in as the root user at work, the risk is great because the permissions are too great. Therefore, after you create a user with suitable permissions, do not log in as the root user frequently.

2. View the current database

Use show databases; to see which databases are currently installed in MySQL.

show databases;


When MySQL is first installed, there are four databases by default, information_schema, mysql, performance_schema, and sys. Normally, we will not use these four databases directly, but do not delete these four databases, otherwise it will cause a lot of unnecessary trouble. If you delete it accidentally, it is recommended to reinstall MySQL, migrate your own data and back it up before reinstalling, or migrate an identical database from another server.

3. Create database

Use create database database name; to create a database.

create database MyDB_one;


After the database is created successfully, the number of databases becomes 5, including the MyDB_one just created.

4. Set character encoding when creating database

Use create database database name character set utf8; to create a database and set the character encoding of the database.

create database MyDB_two character set utf8;


For databases created directly, the encoding method of the database is MySQL's default encoding method latin1 (single-byte encoding). Usually we store Chinese data in the database, so it is best to set the encoding method of the database to utf-8 so that Chinese can normal display.

create database MyDB_three charset utf8;

character set can be abbreviated to charset, the effect is the same.

5. View and display the encoding method of the database

Use show create database database name; display the creation information of the database.

show create database MyDB_one;
show create database MyDB_two;


If you don't know what the encoding method of a database is, you can use show create database database name to view the encoding method of the database. You can see that the encoding method of MyDB_one just created is MySQL's default encoding latin1, and the encoding method of MyDB_two is utf-8.

Of course, this method cannot be displayed at the same time as creation, and can only view the encoding method of an existing database.

6. Use alter database database name character set utf8; modify the database encoding

alter database MyDB_one character set utf8;


If the encoding method of a database does not meet the usage requirements, it can be modified. After the MyDB_one created just now has been modified, the encoding method has also changed to utf-8.

7. Enter or switch databases

Use the use database name to enter or switch databases.

use MyDB_ones
use MyDB_two;


When you first connect to MySQL, you are not in any database. If you want to use a certain database, you need to enter this database.

The semicolon after the use database name command can be omitted. This is the only statement in the SQL statement that can omit the semicolon.

8. Display the current database select database();

select database();


After entering the database, you can use select database(); to check which database you are currently in. When operating a database for a long time, check the current database after switching back and forth among many databases to avoid operating the wrong database.

3. Create data table

1. View the tables in the current database

Use show tables; see which tables are in the current database.

show tables;


In the database MyDB_one just created, no tables have been created yet, so it is currently empty.

2. Create table

Use create table table name (field 1 field type, field 2 field type, field 3 field type,...); to create a table.

create table Phone_table(pid INT, name CHAR(20), price INT);


A data table called Phone_table is created in MyDB_one. This table has three fields: pid, name, and price. In order to increase the readability of SQL, I use lowercase for field names and uppercase for field types.

3. Display table information

Use show create table table name; to display information about the created table.

show create table Phone_table;


Use show create table table name; you can display table field information, MySQL engine, and default character encoding and other information. Like displaying database information, show can only display information about data tables that have been created, and cannot display information at the same time as it is being created.

If you want to better display the field information of the table, you can use desc table name; to display the field information of the table.

4. Add fields to the table

Use alter table table name add field name data type; add a new field to an existing table.

alter table Phone_table add color CHAR(20);


After adding it, there is one more field in the table just now, and it was added successfully.

5. Delete table fields

Use alter table table name drop field name; delete existing fields in a table.

alter table Phone_table drop price;


After you delete a field, the field no longer exists in the table.

6. Modify the data type of the field

Use alter table table name modify field name data type; modify the data type of existing fields in the table.

alter table Phone_table modify name VARCHAR(12);


After modification, the data type of this field changes.

7. Modify the data type of the field and rename it

Use alter table table name change original field name new field name data type; modify the field name and type of existing fields in the table.

alter table Phone_table change name pname CHAR(18);


Now, change the name of the table to pname and modify the data type of pname.

4. MySQL common field types

A data table is composed of several fields. It is normal for a table to have more than a dozen fields. Each field represents different information and requires the use of different types of data.

Therefore, when creating a table, you must specify the appropriate data type for each field.

Commonly used field types in MySQL include the following:

1. Integer type

type of data data range
TINYINT -128 -- 127
SMALLINT -32768 -- 32767
MEDIUMINT -2^23 -- 2^23-1
INT -2^31 -- 2^31-1
BIGINT -2^63 -- 2^63-1

    
    
    
    
    
    

2. String type

type of data Byte range use
CHAR(n) 0 -- 255 bytes Fixed length string
VARCHAR(n) 0 -- 65535 bytes variable length string
TEXT 0 -- 65535 bytes long text data
LONGTEXT  0 -- 2^32-1 bytes Very large text data
BLOB 0 -- 65535 bytes Binary long text data
LUNGBLOB 0 -- 2^32-1 bytes Binary extremely large text data

       
        
        
        
       
       
        

3. Decimal type

m represents the total length of the floating point number, and n represents the number of significant digits after the decimal point.

type of data  Data usage data range
Float Float(m,n)  7 significant digits
Double Double(m,n) 15 significant digits
Decimal Decimal(m,n) 28 significant digits

4. Time type

type of data Format use
DATE YYYY-MM-DD date
TIME HH:MM:SS time
YEAR YYYY years
DATETIME YYYY-MM-DD HH:MM:SS date and time
TIMESTAMP 10 or 13 digit integer (number of seconds) Timestamp


        
        
        
        
        

5. Enumeration type

enum(enumeration value 1, enumeration value 2,...)

Enumeration types can only select one of the listed values, such as gender.

Guess you like

Origin blog.csdn.net/qq_27981847/article/details/132759004