[For Newbies] Getting Started with MySQL (Detailed Summary)

3. Create database

Use create database database name; to create a database.

create database MyDB_one;
create database DBAliTest;

After the database is created successfully, the number of databases becomes 6, including the dbalitest 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.

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 dbalitest;

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

2. View the current database

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

show databases;

When MySQL is first installed, there are four databases by default, information_schema, mysql, performance_schema, 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.
 

7. Enter or switch databases

Use use database name to enter or switch databases.

mysql> use mysql

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.

use database name The semicolon after this 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; to see what tables are in the current database.

show tables;

3. Display table information

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

mysql> show create table user;

Use show create table table name; to display table field information, MySQL engine, 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.

7.2. Create table

grammar


create table 表名(
    字段名 数据类型,
    字段名 数据类型,
    ....
    字段名 数据类型
);

create table student(
    id int,
    name varchar(32),
    birthday date,
    money double(5,2)
);

Note: You do not need to write a comma in the last field definition, but you must write a comma in each of the remaining lines.

7.3. Query table

Query all tables in a database

show tables;

​​​​​​​

Query table structure

desc 表名;

1. Users in mysql are stored in the system database "mysql" of the mysql system.

1. Create user command

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

illustrate:

username: the username you will create

host: Specify the host on which the user can log in. If it is a local user, localhost can be used. If you want the user to log in from any remote host, you can use the wildcard character %.

Password: The user’s login password. The password can be empty. If it is empty, the user can log in to the server without a password.

CREATE USER 'tet_dk'@'localhost' IDENTIFIED BY '1234';--启用这个

CREATE USER 'tet_dk'@'192.168.1.101_' IDENDIFIED BY '1234';

CREATE USER 'tet_dk'@'%' IDENTIFIED BY '1234';

CREATE USER 'tet_dk'@'%' IDENTIFIED BY '';

CREATE USER 'tet_dk'@'%';

2. Authorization

命令:GRANT privileges ON databasename.tablename TO 'username'@'host'

illustrate:

privileges: user's operation permissions, such as SELECT, INSERT, UPDATE, etc. If you want to grant all permissions, use ALL

databasename: database name

tablename: table name. If you want to grant the user corresponding operation permissions on all databases and tables, you can use * to indicate it, such as *.*

GRANT SELECT, INSERT ON test.user TO 'test_db'@'%';
GRANT ALL ON *.* TO 'test_db'@'%';
GRANT ALL ON maindataplus.* TO 'test_db'@'%';

GRANT ALL ON *.* TO 'test_db'@'localhost';--启用这个

Guess you like

Origin blog.csdn.net/zgscwxd/article/details/134876171