Basic introduction and common use of mysql database

  1. Relational Database

    ● Two-dimensional table
    ● A row in a table is called a record, and a column in a table is called a field (attribute)
    ● The value of a cell that crosses a row and column is called an attribute value or a field value.
    ● ORACLE DB2 Mysql SQL SERVER relational database

  2. Commonly used commands (can be executed on the command line, or you can create a new query execution in Navicat)

View version number

select version()

Show all databases

show databases;

Set the current database

use  数据库名

View all tables in the database

show tables

View table structure

desc 表名
  1. Storage engine
    Some rules for database storage, management and query operations.

Three types of data engines
1) innoDB storage engine
● default storage engine
● support foreign keys
● support transactions
2) MYISAM
● support full-text retrieval
● do not support foreign keys
3) MEMERY
● memory engine, create tables in memory
● fast access

  1. Character set
    ● The encoding format of the data stored in the database
    ● Chinese generally uses utf8
    ● The collation utf8_general_ci ci means upper and lower case letters are not sensitive
    ● MySQL is not sensitive to the case of English letters

  2. sql script
    ● .sql as a file with extension
    ● convenient for database deployment

  3. Database management
    Database creation

CREATE DATABASE 数据库名 CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE DATABASE  IF NOT EXISTS 数据库名 CHARACTER SET UTF8 COLLATE utf8_general_ci ;

Database deletion

DROP DATABASE 数据库名
  1. ER diagram
    ● ENTITY RELATIONSHIP Entity relationship diagram, a data model generated during the logical design phase of the database.
    ● Contains three elements:

    1. The entity entity actually corresponds to the table in the database
    2. The attributes actually correspond to the fields in the table
    3. Relationship between tables and tables
      ● One to one
      One to many
      ● Many to many
  2. Data type
    1) Numerical type
    ● BIT bit
    ● TINYINT small integer, -128~127 unsigned unsigned 0~255
    ● BOOLEAN is represented by TINYINT(1), 0 represents false, and non-zero value represents true
    ● SAMLLINT -32768~32767
    ● INT plus or minus more than 2.1 billion
    ● BIGINT many, many
    ● FLOAT
    ● DOUBLE
    ● DECIMAL
    2) String type
    ● CHAR(n) fixed-length string of n
    ● VARCHAR(n) variable-length string, n is the maximum length, actual The stored length is the actual length of the string.
    ● TEXT large text, such as the chapter content of a novel.
    ● ENUM enumeration
    3) Date type
    ● DATE
    ● DATETIME
    ● TIME
    ● YEAR
    ● TIMESTAMP Timestamp memory records the last update time. The default value of the field needs to be set to CURRENT_TIMESTAMP, and the time stamp is checked.

  3. Constraint type
    ● When inserting records into the table, certain rules are followed, called constraints, to ensure data integrity and consistency.
    Primary key constraint
    ● PRIMARY KEY
    ● One or more columns in the table, set the primary key constraint, which means that the value of the column is unique in the table.
    ● The value of the primary key cannot be NULL and must be unique.
    ● Syntax:

字段名  PRIMARY KEY  AUTO_INREMENT  

● AUTO_INREMENT indicates that the primary key increments
uniqueness constraint
● UNIQUE
● indicates that the value of the field is unique in the table, but the value is NULL
● multiple fields in the table can set unique constraints
● Syntax:

字段名  UNIQUE

Non-null constraint
● NOT NULL
● The value of the field cannot be null
● Syntax:

字段名  NOT NULL

Default value
● DEFUALT
● If no value is assigned to the field when inserting a record in the table, the default value is used.
● Syntax:

字段名  DEFAULT

Foreign key constraint
● FOREIGN KEY
● A field in a table is a value taken from a field in another table to form a foreign key constraint
● The data types of related fields in the two tables must be consistent.
● Strategy for foreign key constraints:
● restrict, noaction For records that have formed foreign key constraints, the associated records in the referenced table are not allowed to be deleted.
● set null For records that have formed a foreign key constraint, after the record in the referenced table is deleted, the corresponding field value of the associated record in the main table is set to null
● cascade For records that have formed a foreign key constraint, if you delete or Update the records in the referenced table, the associated records of the main table will also be deleted or updated.

CHECK constraint
● mysql does not support this constraint, it is generally implemented through enumeration or trigger.

  1. Table creation
    ● Syntax
CREATE TABLE   表名(
	字段名   数据类型   约束,
	字段名   数据类型   约束
	)
  1. Table deletion
DROP TABLE 表名
  1. Change table
    ● ALTER

  2. SQL statement
    SQL Structed Query Language
    DDL DATA DEFINITION LANGUAGE Data definition language CREATE ALTER DROP features
    for operations on the structure of the database or database components : rollback of transactions is not allowed. DML DATA MANIPULATION LANGUAGE data operation language table record additions, deletions and changes INSERT DELETE UPDATE Features: transaction rollback is possible DQL DATA QUERY LANGUAGE data query language SELECT DCL DATA CONTROL LANGUAGE data control language GRANT authorized REVOKE recovery authority TPL TRANSACTION PROCESS LANGUAGE COMMIT ROOLBACK















  3. DML
    INSERT insert records into the table
    ● Syntax

INSERT INTO 表名(字段列表) VALUES(值列表)

● Among them:
● 1. The field list and the value list are separated by commas.
● 2. The values ​​in the value list, string and date constants, need to be enclosed in single quotes.
● 3. The type of value must match the data type of the field.
● 4. INTO can be omitted.
● 5. If the field list is a field inserted into the entire table, it can be omitted, but the number and order of the value list must be consistent with the defined table The order of the fields is the same.
● 6. The field can also write a few of them, which corresponds to the number of the following values.
● 7. VALUES keyword can also use VALUE, but values ​​are generally used
● Insert multiple records in batches
● INSERT INTO table name (field name) VALUES (value list 1), (value list 2), …, (value list n)

UPDTAE
● Make changes to the records in the table
● Syntax

UPDATE 表名 SET 字段名1=1, 字段名2=2  WHERE子句

● The where clause is used to limit the modified records. If no where clause is added, all records in the table are modified by default.

DELETE
● Used to delete records in the table
● Syntax

DELETE FROMWHERE子句

Table truncation
● TRUNCATE TABLE table name
● Table truncation, clear all records in the table.

The difference between TRUNCATE and DELETE
● Both can delete records in the table. Delete can delete all the records in the table, or delete some records according to the where condition, and truncate can only clear all the records in the table.
● delete belongs to DML, transactions can be rolled back, truncate belongs to DDL, and transactions cannot be rolled back.

  1. The where clause is
    used to conditionally restrict the
    relational operator
    ● = equal to
    ● != <> not equal to
    ●> >=
    ● <<=
    ● Note: In addition to value comparison, the relational operator can also compare dates, hiredate>'1982-1 -1'
    Null value judgment
    ● IS NULL
    ● IS NOT NULL
    interval judgment
    ● BETWEEN… AND… means the value is in a continuous interval
    ● WHERE sal BETWEEN 3000 AND 5000 means to filter the records with salary between 3000 and 5000, and include Two boundaries of 3000 and 5000.
    ● NOT BETWEEN… AND… indicates that the value is not in a continuous interval
    set (discrete)
    ● IN indicates that the value is in the set
    ● WHERE deptno IN(10,20,40)
    ● NOT IN indicates that the value is not in the set
    logic Operator
    ● Concatenate multiple filter conditions in the where clause
    ● && AND and
    ● || OR or
    ●! NOT NOT

Source: Neusoft Java Training

Guess you like

Origin blog.csdn.net/weixin_44997802/article/details/108592372