MySQL: the basic operation of the database and the three major data types of the database

Table of contents

1. Create a database

2. Delete the database

3. Query data

4. Use Linux commands in the database

5. Database encoding method and verification rules

1. View all encoding methods supported by MySQL

2. View all verification rules supported by a certain encoding method

3. View all encoding methods and verification rules

4. View the encoding method and verification rules used by the local database

4.1 View the current encoding method of the local database

4.2 View the current verification rules of the local database

4.3 When creating a new database, you can also set the encoding and verification rules of the database:

Six, the three major data types in the database

1. Text type:

2. Number type:

3. Date type:

4. Create a new table named all_type based on all these data types:


1. Create a database

CREATE DATABASE IF NOT EXISTS DB_1;

Here, a database named DB_1 is created and there is a conditional judgment in front: the database will be created when the database does not exist, because the created database name is different from the existing database.

2. Delete the database

DROP DATABASE IF EXISTS  DB_1;

A previously created database named DB_1 is deleted here, and conditional judgment is also used here, that is, when the data exists, the delete operation is performed, and it will not be operated if it does not exist, so as to prevent error reporting

3. Query data

USE 'database name'

The role of USE is to specify the database that will be used to manipulate the data

USE DB_1;

 After the execution of this statement is completed, all data operation commands are performed on the database DB_1

For example:

mysql> SELECT DATABASE();  #查看当前连接的数据库
mysql> SELECT VERSION();   #查看数据库的版本
mysql> SELECT USER();      #查看当前用户

 

4. Use Linux commands in the database

You can use system to execute commands in Linux1 in the database:

Format: system Linux command

For example:

mysql> system pwd
/root
mysql> system ls | wc -l
10

5. Database encoding method and verification rules

1. View all encoding methods supported by MySQL

show character set; --方法1
show charset; -- 方法2
show char set; -- 方法3

2. View all verification rules supported by a certain encoding method

show collation where charset ='utf8mb4';

3. View all encoding methods and verification rules

show collation;
使用该命令可以查看MySQL数据库支持的所有编码方式和校验规则:

4. View the encoding method and verification rules used by the local database

4.1 View the current encoding method of the local database

show variables like 'character_set_server'

 

4.2 View the current verification rules of the local database

show variables like 'collation_server';

4.3 When creating a new database, you can also set the encoding and verification rules of the database:

For example: Create a new database named DB_2 whose encoding method is GBK checksum check rule gbk_chinese_ci

CREATE DATABASE IF NOT EXISTS DB_2 CHARACTER SET GBK COLLATE  GBK_CHINESE_CI;

Six, the three major data types in the database

1. Text type:

Note: The usage scenarios of char are: char(1) is used in the scenario of choosing one of the two
ENUM: enumeration: that is, any number is obtained in a certain interval
SET: set: that is, any subset within an interval

2. Number type:

Note: These integer types have the additional option UNSIGNED. In general, integers can be negative or positive. If you add the UNSIGNED attribute, then the range will start at 0, not some negative number.

3. Date type:

4. Create a new table named all_type based on all these data types:

create table all_type(
		id int(11) comment '编号', 
        company_name char(6) comment '公司名称',
        introduce varchar(100) comment '介绍',
        content1 tinytext comment '内容1',
        content2 text comment '内容2',
        content3 mediumtext comment '内容3',
        content4 longtext comment '内容4',
        description1 blob comment '描述1',
        description2 mediumblob comment '描述2',
        description3 longblob comment '描述3',
		iq tinyint comment '0705数据库IQ',
		salary smallint comment '薪资',
		five_plan mediumint comment '五年计划',
		code_num bigint comment '代码量',
		desposit float(10,2) comment '存款',
		score_math double(3,1) comment '数学成绩',
		score_English decimal(3,1) comment '英语成绩',
		time_birth DATE comment '出生日期',
		time_homework_begin datetime comment '作业开始时间',
		time_homework_end time comment '作业完成时间',
		graduation_year year comment '毕业年份',
		find_job_time timestamp comment '找到工作的时间'
)engine=InnoDB default character set utf8mb4 collate  utf8mb4_0900_ai_ci;

View the created table:

show  tables ;

View all columns (attributes) in a table:

show columns from all_type;

Guess you like

Origin blog.csdn.net/qq_68163788/article/details/131582243
Recommended