SQL statement overview-DDL-data type

1. What is SQL

SQL (Structured Query Language) is "Structured Query Language", which is an operating language for relational databases. It can be applied to all relational databases, such as MySQL, Oracle, SQL Server, etc.

SQ standards (ANSI/ISO) are:

  • SQL-92: SQL language standard released in 1992;
  • SQL:1999: SQL language label released in 1999;
  • SQL: 2003: SQL language label released in 2003;

These standards are the same as the JDK version, and there are always some grammatical changes in the new version. Databases in different periods have implemented different standards.

Although SQL can be used in all relational databases, many databases still have some syntax after the standard, which we can call "dialects". For example, the LIMIT statement in MySQL is a dialect unique to MySQL, which is not supported by other databases! Of course, Oracle or SQL Server has its own dialect.

2. Grammar requirements

  • SQL statements can be written in single or multiple lines, ending with a semicolon;
  • Spaces and indentation can be used to enhance the readability of sentences;
  • The keywords are not case-sensitive, it is recommended to use uppercase;

3. Classification

  • DDL (Data Definition Language): Data Definition Language, used to define database objects: libraries, tables, columns, etc.;
  • DML (Data Manipulation Language): data manipulation language used to define database records (data);
  • DCL (Data Control Language): Data Control Language, used to define access rights and security levels;
  • DQL (Data Query Language): Data query language, used to query records (data).

4. DDL

  • Basic operation

    • View all database names: SHOW DATABASES;
    • Switch database: USE mydb1, switch to mydb1 database;
  • Operating database

    • Create a database: CREATE DATABASE [IF NOT EXISTS] mydb1;

      Create a database, for example: CREATE DATABASE mydb1, create a database named mydb1. If this data already exists, an error will be reported. For example, CREATE DATABASE IF NOT EXISTS mydb1, create the database when the database named mydb1 does not exist, so as to avoid errors.

    • Delete the database: DROP DATABASE [IF EXISTS] mydb1;

      Delete the database, for example: DROP DATABASE mydb1, delete the database named mydb1. If this database does not exist, an error will be reported. DROP DATABASE IF EXISTS mydb1, even if mydb1 does not exist, it will not report an error.

    • Modify the database encoding: ALTER DATABASE mydb1 CHARACTER SET utf8

      Modify the encoding of the database mydb1 to utf8. Note that all UTF-8 encoding in MySQL cannot use the "-" in the middle, that is, UTF-8 should be written as UTF8.

View mysql encoding:show variables like 'character_set_database'

5. Data Type

MySQL, like Java, has data types. Data types in MySQL are mainly used on columns.

Common types:

  • int: Integer. If the field is of type int, you do not need to specify the length. If the field is incremented, it must be of type int.
  • double: floating point type, for example, double(5,2) means up to 5 digits, of which there must be 2 decimal places, that is, the maximum value is 999.99;
  • decimal: Generic type, use this type in the form of money, because there will be no lack of precision; decimal(5,2)
  • char: fixed-length character string type; char(10)
  • varchar: variable-length character string type; varchar(10)
  • text: string type; String
  • blob: byte type;
  • date: date type, format: yyyy-MM-dd;
  • time: time type, format: hh:mm:ss
  • timestamp: timestamp type;

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/109370851
Recommended