Basic use of MySQL database (3)-------SQL language classification and usage specifications

1. SQL classification

The SQL language is mainly divided into the following three categories in terms of functions:

  • DDL (Data Definition Languages, Data Definition Language), these statements define different database objects such as databases, tables, views, indexes, etc., and can also be used to create, delete, and modify the structure of databases and data tables. The main statement keywords include CREATE, DROP, ALTER , etc.
  • DML (Data Manipulation Language, Data Manipulation Language), used to add, delete, update and query database records, and check data integrity. The main statement keywords include INSERT, DELETE, UPDATE, SELECT , etc. SELECT is the foundation of the SQL language and is the most important.
  • DCL (Data Control Language, Data Control Language), used to define databases, tables, fields, user access rights and
    security levels. The main statement keywords include GRANT, REVOKE, COMMIT, ROLLBACK, SAVEPOINT, etc.

2. The rules and specifications of the SQL language

  • SQL can be written on one or more lines. For readability, write clauses on separate lines, using indentation where necessary
  • Each command ends with ; or \g or \G
  • Keywords cannot be abbreviated and cannot be broken into lines

About punctuation:

  • It must be ensured that all (), single quotes, and double quotes end in pairs
  • The half-width input method in English must be used
  • Data of string type and date time type can be represented by single quotes (' ')
  • The alias of the column, try to use double quotes (" "), and it is not recommended to omit as

3. SQL case specification (recommended compliance)

  • MySQL is case insensitive on Windows
  • MySQL is case-sensitive in the Linux environment; database names, table names, table aliases, and variable names are strictly case-sensitive; keywords, function names, column names (or field names), column aliases (field Aliases) are case-insensitive.
  • It is recommended to adopt a unified writing standard; database names, table names, table aliases, field names, field aliases, etc. are all lowercase; SQL keywords, function names, bind variables, etc. are all capitalized.

4. Notes

单行注释:#注释文字(MySQL特有的方式)
单行注释:-- 注释文字(--后面必须包含一个空格。)
多行注释:/* 注释文字 */

5. Naming rules (for now)

  • Database and table names must not exceed 30 characters, and variable names are limited to 29 characters
  • Must only contain A–Z, a–z, 0–9, _ a total of 63 characters
  • Object names such as database names, table names, and field names should not contain spaces
  • In the same MySQL software, databases cannot have the same name; in the same library, tables cannot have the same name; in the same table, fields cannot have the same name
  • You must ensure that your fields do not conflict with reserved words, database systems, or common methods.
    If you insist on using it, please use ` (emphasis mark) in the SQL statement to enclose
  • Keep the field names and types consistent. Be sure to ensure consistency when naming fields and specifying data types for them. If the data
    type is an integer in one table, don't change it to a character in another table.

Example:

#以下两句是一样的,不区分大小写
show databases;
SHOW DATABASES;
#创建表格
#create table student info(...); #表名错误,因为表名有空格
create table student_info(...);
#其中order使用``飘号,因为order和系统关键字或系统函数名等预定义标识符重名了
CREATE TABLE `order`();
select id as "编号", `name` as "姓名" from t_stu; #起别名时,as都可以省略
select id as 编号, `name` as 姓名 from t_stu; #如果字段别名中没有空格,那么可以省略""
select id as 编 号, `name` as 姓 名 from t_stu; #错误,如果字段别名中有空格,那么不能省略""

6. Data import command

Log in to mysql on the command line client, and use the source command to import the sql file

mysql> source d:\mysqldb.sql

7. Show table structure

Use the DESCRIBE or DESC command to indicate the table structure.

DESCRIBE employees;
或
DESC employees;
mysql> desc employees;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| employee_id | int(6) | NO | PRI | 0 | |
| first_name | varchar(20) | YES | | NULL | |
| last_name | varchar(25) | NO | | NULL | |
| email | varchar(25) | NO | UNI | NULL | |
| phone_number | varchar(20) | YES | | NULL | |
| hire_date | date | NO | | NULL | |
| job_id | varchar(10) | NO | MUL | NULL | |
| salary | double(8,2) | YES | | NULL | |
| commission_pct | double(2,2) | YES | | NULL | |
| manager_id | int(6) | YES | MUL | NULL | |
| department_id | int(4) | YES | MUL | NULL | |
+----------------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)

Among them, the meaning of each field is explained as follows:

  • Field: Indicates the field name.
  • Type: Indicates the field type, where barcode and goodsname are of text type, and price is of integer type.
  • Null: Indicates whether the column can store NULL values.
  • Key: Indicates whether the column is indexed. PRI indicates that the column is part of the primary key of the table; UNI indicates that the column is part of the UNIQUE index
    ; MUL indicates that a given value in the column is allowed to appear multiple times.
  • Default: Indicates whether the column has a default value, and if so, what is the value.
  • Extra: Indicates additional information related to a given column that can be obtained, such as AUTO_INCREMENT, etc.

Guess you like

Origin blog.csdn.net/weixin_44860226/article/details/128297359