mysql [Basic Theory]

MYSQL basic theoretical knowledge

  1. What is a database: A
    database is a warehouse that organizes, stores, and manages data according to data structures.


  2. Type of database : relational database: mysql ---- mariaDB, oracle
    non-relational database: nosql

  3. RDBMS terminology
    Database: is a collection of related tables
    Data Table: Table is a matrix of data. A table in a database looks like a simple spreadsheet.
    Column: A column contains the same type of data, such as the name on the ID card. A
    row contains multiple different data, such as an order
    redundancy when shopping online : storing twice the data, redundancy reduces performance, but improves Data security.
    Primary key: The primary key is unique. At most one primary key in a data table, you can use the primary key to query the data.
    Foreign key: The foreign key is used to associate two tables. The
    composite key: The composite key (combined key) uses multiple columns as an index key. Generally used for compound indexes.
    Index: Use indexes to quickly access specific information in database tables. An index is a structure that sorts the values ​​of one or more columns in a database table. Catalogue similar to books

  4. SQL statements are not case sensitive, but developers usually capitalize all SQL keywords and use lowercase for all column and table names, which makes the code easier to read and debug.

  5. SQL statement
    query: SELECT * FROM 'db_cs'
    stands for all meanings. The
    quotes behind the FROM are the table name
    SELECT user, if passFROM db_cs
    will be
    replaced by a field, it is to query a column of this field.
    Where is the condition, for example, you can query both id 1user and pass Fields

    Insert: the INSERT INTO db_cs( id, user, pass) the VALUES (. 1, ADMIN, 123)
    insert statement, but in no FROM INTO table name behind the
    front is a field inside the parentheses, brackets behind the corresponding field, the value of the inserted

    Modification: UPDATE db_csSET user= 'ccc', pass= 'ccccc' WHERE id = 12The
    modification statement has neither FROM nor INTO. After UPDATE, write the table name directly after
    SET. Similar to key-value pairs, one field corresponds to one value, and the modification statement must be Add conditions, otherwise all data will be modified

    Delete: DELETE FROM db_csWHERE id = 12
    Delete the statement with the table name after FROM, and delete it must add conditions, otherwise the consequences can not be imagined

  6. Data types
    Commonly used data types, integer int, time data, timestamp timestamp, string, varchar and char (varchar must define the length but not char), floating-point decimal, text text

  7. What is a trigger? A
    trigger (TRIGGER) is one of MySQL's database objects. A trigger is very similar to a function in a programming language. It needs to be declared, executed, and so on. But the execution of the trigger is not called by the program, nor is it started manually, but triggered and activated by the event to realize the execution. A bit like the DOM event in js, but it is triggered by adding, deleting, and changing

  8. Trigger syntax
    CREATE TRIGGER trigger_name trigger_time trigger_event
    ON tb_name FOR EACH ROW trigger_stmt
    trigger_name: trigger name
    tirgger_time: trigger execution time
    BEFORE: trigger before event
    AFTER: trigger after event
    trigger_event: trigger event
    INSERT: activate trigger when inserting a row, INSERT , LOAD DATA, REPLACE statements can trigger
    UPDATE: triggers are activated when a row is changed, UPDATE statements can trigger
    DELETE: triggers are activated when a row is deleted, DELETE, REPLACE statements can trigger
    tb_name: which table the trigger will execute
    FOR EACH ROW: trigger frequency triggers once for each row
    trigger_stmt: the body of the trigger, which can be a SQL statement or multiple statements contained in BEGIN and END

  9. Advantages of
    indexing Indexing greatly reduces the amount of data that the server needs to scan.
    Index can help the server avoid sorting and temporary tables
    Index can change random IO to sequential IO

  10. ER diagram
    ER diagram is the entity relationship diagram, mainly completed the design of the data part of the system.

  11. Mysql transactions
    Transactions are mainly used to process data with large operations and high complexity.
    In MySQL, only databases or tables that use the Innodb database engine support transactions.
    The transaction must meet four conditions

    Atomicity: A transaction is the smallest execution unit and does not allow splitting. The atomicity of the transaction ensures that the actions are either complete or completely ineffective.

    Consistency: The data remains consistent before and after the transaction is executed, and the results of multiple transactions reading the same data are the same.

    Isolation: When accessing the database concurrently, a user's transaction is not interfered by other transactions, and the database is independent between concurrent transactions.

    Persistence: After a transaction is committed. The changes to the data in the database are persistent. Even if the database fails, it should not have any effect.

  12. Lock A
    lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently.
    Shared lock (read lock): Other transactions can be read, but not written.
    Exclusive lock (write lock): Other transactions cannot be read or written.

  13. Deadlock
    Deadlock means that two or more transactions occupy each other on the same resource and request to lock the resources occupied by the other party, resulting in a vicious circle.

This is the end. There is still a lot of theoretical knowledge about mysql. What I have summarized is only fur. I need to understand more myself. I hope my article can help everyone. Thank you! ! !

Published 4 original articles · won 10 · views 181

Guess you like

Origin blog.csdn.net/DarKer_LB/article/details/105452147