Summary of common commands for MySql

User Management:

1. Create a new user:
    >CREATE USER name IDENTIFIED BY 'ssapdrow';
  2. Change the password:
    >SET PASSWORD FOR name=PASSWORD('fdddfd');
  3. Rights management
    >SHOW GRANTS FOR name; //View name user rights
    > GRANT SELECT ON db_name.* TO name; //Give all permissions to the name user db_name database
    >REVOKE SELECT ON db_name.* TO name; //Reverse operation of GRANT, remove permissions;

1. Database operation:

1. View database:
    >SHOW DATABASES;
  2. Create database:
    >CREATE DATABASE db_name; //db_name is the database name
  3. Use database:
    >USE db_name;
  4. Delete database:
    >DROP DATABASE db_name;

Second, create a table:

1. Create a table:
    >CREATE TABLE table_name(
    >id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, //id value, unsigned, non-null, incremental—unique, can be used as a primary key. >
    name VARCHAR(60) NOT NULL
    >score TINYINT UNSIGNED NOT NULL DEFAULT 0, //Set the default column value
    >PRIMARY KEY(id)
    >)ENGINE=InnoDB //Set the storage engine of the table, generally InnoDB and MyISAM are commonly used; InnoDB is reliable and supports transactions; MyISAM is efficient and does not support full-text search
    > DEFAULT charset=utf8; //Set the default encoding to prevent Chinese garbled characters in the database.
    If conditions permit, you can also use >CREATE TABLE IF NOT EXISTS tb_name(...
  2. Copy the table:
    >CREATE TABLE tb_name2 SELECT * FROM tb_name;
    or Partial copy:
    >CREATE TABLE tb_name2 SELECT id,name FROM tb_name;
  3. Create a temporary table:
    >CREATE TEMPORARY TABLE tb_name (here is the same as creating a normal table);
  4. View available tables in the database:
    >SHOW TABLES;
  5. View the structure of the table:
    >DESCRIBE tb_name;
    can also be used:
    >SHOW COLUMNS in tb_name; //from can also be used
  6. Delete the table:
    >DROP [ TEMPORARY ] TABLE [ IF EXISTS ] tb_name[ ,tb_name2…];
    Example:
    >DROP TABLE IF EXISTS tb_name;
  7. Table renaming:
    >RENAME TABLE name_old TO name_new;
    You can also use:
    >ALTER TABLE name_old RENAME name_new;

3. Modify the table:

1. Change the table structure:
    >ALTER TABLE tb_name ADD[CHANGE,RENAME,DROP] ...content to be changed...
    Example:
    >ALTER TABLE tb_name ADD COLUMN address varchar(80) NOT NULL;
    >ALTER TABLE tb_name DROP address;
    >ALTER TABLE tb_name CHANGE score score SMALLINT(4) NOT NULL;

4. Insert data:

1. Insert data:
    >INSERT INTO tb_name(id,name,score)VALUES(NULL,'Zhang 3',140),(NULL,'Zhang 4',178),(NULL,'Zhang 5',134);
    To insert multiple pieces of data here, just add a comma after it, and write the inserted data directly; the primary key id is a self-incrementing column, so you don’t need to write it.
  2. Insert the retrieved data:
    >INSERT INTO tb_name(name,score) SELECT name,score FROM tb_name2;

5. Update data:

1. Specify update data:
    >UPDATE tb_name SET score=189 WHERE id=2;
    >UPDATE tablename SET columnName=NewValue [ WHERE condition ]

6. Delete data:

1. Delete data:
    >DELETE FROM tb_name WHERE id=3;

7. Condition control:

1. WHERE statement:
    >SELECT * FROM tb_name WHERE id=3;
  2. HAVING statement:
    >SELECT * FROM tb_name GROUP BY score HAVING count(*)>2
  3. Relevant condition control symbols:
    =, >, <, <> , IN(1,2,3…), BETWEEN a AND b, NOT
    AND, OR
    Linke() % in the usage to match any, _ to match a character (can be a Chinese character)
    IS NULL Null value detection

Eight, MySQL regular expression:

1. Mysql supports the regular expression of REGEXP:
    >SELECT * FROM tb_name WHERE name REGEXP ' 1 ' // Find the name starting with AD
  2. Special characters need to be escaped.

Nine, some functions of MySQL:

1. String link—CONCAT()
    >SELECT CONCAT(name,'=>',score) FROM tb_name
  2. Mathematical functions:
    AVG, SUM, MAX, MIN, COUNT;
  3. Text processing functions:
    TRIM, LOCATE, UPPER, LOWER, SUBSTRING
  4. Operators:
    +, -, *,
  5. Time functions:
    DATE(), CURTIME(), DAY(), YEAR(), NOW()…

10. Group query:

1. Group query can be grouped according to the specified column:
    >SELECT COUNT( ) FROM tb_name GROUP BY score HAVING COUNT( )>1;
  2. The condition uses Having;
  3. ORDER BY sorting:
    ORDER BY DESC|ASC => by data in descending and ascending order

11. UNION rules - two statements can be executed (duplicate rows can be removed)

12. Full text search - MATCH and AGAINST

1. SELECT MATCH(note_text)AGAINST('PICASO') FROM tb_name;
  2. InnoDB engine does not support full-text search, but MyISAM can;

13. View

1. Create a view
    >CREATE VIEW name AS SELECT * FROM tb_name WHERE ~~ ORDER BY ~~;
  2. Special functions of views:
      a. Simplify the connection between tables (write the connection in select);
      b. Reformat Output retrieved data (TRIM, CONCAT and other functions);
      c. Filter unwanted data (select part)
      d. Use views to calculate field values, such as summary values.

14. Use stored procedures:

In my understanding, a stored procedure is a custom function with local variable parameters, parameters can be passed in, and values ​​can be returned, but this syntax is sluggish~~~ 1. Create a stored procedure: >CREATE PROCEDURE pro( >IN
  num
    INT
    , OUT total INT)
    >BEGIN
    >SELECT SUM(score) INTO total FROM tb_name WHERE id=num;
    >END;
   *** Here IN (pass a value to the stored procedure), OUT (pass a value from the stored procedure), INOUT (pass in and out of the stored procedure), INTO (save variables)
  2. Call the stored procedure:
    >CALL pro(13,@total) //There are two stored procedure variables here, one is IN and the other is OUT, here The OUT also needs to be written, if you don’t write it, you will make an error
    >SELECT @total //You can see the result here;
  3. Other operations of the stored procedure:
    >SHOW PROCEDURE STATUS; //Display the current stored procedure
    >DROP PROCEDURE pro ; //Delete the specified stored procedure

15. Use cursors:

I don’t understand this very well, please give me some pointers~~~
   1. Cursor operation
    >CREATE PROCEDURE pro()
    >BEGIN
    >DECLARE ordername CURSOR FOR
    >SELECT order_num FROM orders;
    >END;
    
    >OPEN ordername; //Open the cursor

>CLOSE ordername; //Close the cursor

16. Trigger:

A trigger refers to triggering the specified operation in the trigger when a specified operation is performed;
  1. Statements that support triggers include DELETE, INSERT, and UPDATE, and others are not supported.
  2. Create a trigger:
    >CREATE TRIGGER trig AFTER INSERT ON ORDERS FOR EACH ROW SELECT NEW.orser_name;
    >INSERT statement, trigger statement, return a value
  3. Delete trigger
    >DROP TRIGGER trig;

Seventeen, grammar arrangement:

1. ALTER TABLE (modify table)
    ALTER TABLE table_name
    ( ADD column datatype [ NULL | NOT NULL ] [ CONSTRAINTS ]
       CHANGE column datatype COLUMNS [ NULL | NOT NULL ] [ CONSTRAINTS ]
       DROP column,
       ...
    )
  2, COMMIT (processing Transaction)
    >COMMIT;
  3. CREATE INDEX (create an index on one or more columns)
    CREATE INDEX index_name ON tb_name (column [ ASC | DESC ] , …);
  4. CREATE PROCEDURE (create a stored procedure)
    CREATE PROCEDURE pro([ parameters ])
    BEGIN
    ...
    END
  5. CREATE TABLE (create table)
    CREATE TABLE tb_name(
    column_name datetype [ NULL | NOT NULL ] [ condtraints] ,
    column_name datepe [null | not null] [condtraints],
    ...
    Primary Key (colorn_name)
    ) Engine = [Innodb | MyISAM] default charset = UTF8 Auto_INCREMENT = 1;
  6. Create User (Creation user)
    Create User User_name [@Hostname] [ IDENTIFIED BY [ PASSWORD ] 'pass_word' ];
  7. CREATE VIEW (create a view on one or more tables)
    CREATE [ OR REPLACE ] VIEW view_name AS SELECT. . . . . .
  8. DELETE (delete one or more rows from the table)
    DELETE FROM table_name [WHERE ...]
  9. DROP (permanently delete databases and objects, such as views, indexes, etc.)
    DROP DATEBASE | INDEX | PROCEDURE | TABLE | TRIGGER | USER | VIEW name
  10. INSERT (add rows to the table)
    INSERT INTO tb_name [ ( columns,… ) ] VALUES(value1,…);
    Insert using SELECT values:
    INSERT INTO tb_name [ ( columns,… ) ]
    SELECT columns , … FROM tb_name [ WHERE … ] ;
   11. ROLLBACK (undo a transaction processing block)
    ROLLBACK [ TO savapointname ];
   12. SAVEPOINT (set a reservation point for ROLLBACK)
    SAVEPOINT sp1;
   13. SELECT (retrieve data, display information)
    SELECT column_name,...FROM tb_name [ WHERE ] [ UNION ] [ RROUP BY ] [ HAVING ] [ ORDER BY ]
  14. START TRANSACTION (start of a new transaction processing block)
    START TRANSACTION
   15 , UPDATE (update one or more rows in a table)
    UPDATE tb_name SET column=value,...[ where ]


  1. A-D ↩︎

Guess you like

Origin blog.csdn.net/Czhenya/article/details/118855772