Mysql database common operations summary

The MySQL database is a very lightweight database management system. Compared with large database management systems such as Oracle, MySQL is more lightweight and flexible, has a faster development speed, and is more suitable for storage and architecture of small and medium-sized data. Since version 5, advanced applications such as cursors , triggers , transactions , and stored procedures have been supported successively , which also adds an important weight to the ease of use of MySQL and the development of enterprise services. The foundation of the database is very small, but the performance optimization of the database is the most important, so it will be beneficial to optimize more.


After a period of database study, I read "SQL Must Know and Know", and now after some summaries, they are sorted as follows.

1. Database operation

1. View the database
SHOW DARABASE;
2. Create a database
CREATE DATABASE db_name; #db_name为表名
3. Use a database
USE db_name;
4. Delete the database
DROP DATABASE db_name;

2. Create the table

1. Create a table
CREATE TABLE table_name
(
    id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name char(60)       NOT NULL,
    score int           NOT NULL,
    PRIMARY KEY(id)     #设置主键
)ENGINE=InnoDB; #存储引擎是InnoDB
2. Copy the table
CREATE TABLE tb_name2 SELECT * FROM tb_name;
3. Create a temporary table
CREATE TEMPORARY TABLE tb_name; #(这里和创建普通表一样)
4. View the tables available in the database
SHOW TABLES;
5. View the structure of the table
DESCRIBE tb_name;
6. Delete table
DROP TABLE tb_name;
7. Table renaming
RENAME TABLE name_old TO name_new;

3. Modification table

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;

Fourth, insert data

1. Insert data
INSERT INTO tb_name(id,name,score) VALUES(NULL,'张三',140),(NULL,'张四',178), (NULL,'张五',134);
2. Insert the retrieved data
INSERT INTO tb_name(name,score) SELECT name,score FROM tb_name2;

5. Update data

UPDATE tb_name SET score=189 WHERE id=2;
UPDATE tablename SET columnName=NewValue [ WHERE condition ]

6. Use wildcard filtering

SELECT prod_id, prod_name
FROM tb_name
WHERE prod_name LIKE 'jet%';    #%匹配任何字符出现任何次数
SELECT prod_id, prod_name
FROM tb_name
WHERE prod_name LIKE '_ jet';   #_ 匹配一个字符

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718443&siteId=291194637