Some things, even though they have been read thousands of times, but as long as they are not recorded in my own notebook, I feel a little flustered in my heart for some reason!
Common database operations
Create database
mysql> create database test_db;
Query OK, 1 row affected (0.00 sec)
Delete database
mysql> drop database test_db;
Query OK, 0 rows affected (0.03 sec)
Select database
mysql> use test_db;
Database changed
Data sheet common operations
Create data table
mysql> CREATE TABLE IF NOT EXISTS `runoob_tbl`(
-> `runoob_id` INT UNSIGNED AUTO_INCREMENT,
-> `runoob_title` VARCHAR(100) NOT NULL,
-> `runoob_author` VARCHAR(40) NOT NULL,
-> `submission_date` DATE,
-> PRIMARY KEY ( `runoob_id` )
-> );
Query OK, 0 rows affected (0.01 sec)
# 如果数据库有中文的话最好还是设置字符集编码。
mysql> CREATE TABLE IF NOT EXISTS `runoob_tbl`(
-> `runoob_id` INT UNSIGNED AUTO_INCREMENT,
-> `runoob_title` VARCHAR(100) NOT NULL,
-> `runoob_author` VARCHAR(40) NOT NULL,
-> `submission_date` DATE,
-> PRIMARY KEY ( `runoob_id` )
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)
Delete data table
mysql> drop table runoob_tbl;
Query OK, 0 rows affected (0.02 sec)
Insert data
mysql> INSERT INTO runoob_tbl
-> (runoob_title, runoob_author, submission_date)
-> VALUES
-> ("学习 PHP", "菜鸟教程", NOW());
(runoob_title, runoob_author, submission_date)
VALUES
("JAVA 教程", "RUNOOB.COM", '2016-05-06');Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> INSERT INTO runoob_tbl
-> (runoob_title, runoob_author, submission_date)
-> VALUES
-> ("学习 MySQL", "菜鸟教程", NOW());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> INSERT INTO runoob_tbl
-> (runoob_title, runoob_author, submission_date)
-> VALUES
-> ("JAVA 教程", "RUNOOB.COM", '2016-05-06');
Query OK, 1 row affected (0.00 sec)
mysql> select * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 1 | 学习 PHP | 菜鸟教程 | 2020-12-09 |
| 2 | 学习 MySQL | 菜鸟教程 | 2020-12-09 |
| 3 | JAVA 教程 | RUNOOB.COM | 2016-05-06 |
+-----------+--------------+---------------+-----------------+
3 rows in set (0.00 sec)
Query data
SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT N][ OFFSET M]
mysql> SELECT runoob_id, runoob_title, runoob_author, submission_date FROM runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 1 | 学习 PHP | 菜鸟教程 | 2020-12-09 |
| 2 | 学习 MySQL | 菜鸟教程 | 2020-12-09 |
| 3 | JAVA 教程 | RUNOOB.COM | 2016-05-06 |
+-----------+--------------+---------------+-----------------+
3 rows in set (0.00 sec)
WHERE statement
SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE condition1 [AND [OR]] condition2.....
mysql> SELECT * from runoob_tbl WHERE runoob_author='RUNOOB.COM';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 3 | JAVA 教程 | RUNOOB.COM | 2016-05-06 |
+-----------+--------------+---------------+-----------------+
1 row in set (0.00 sec)
UPDATE statement
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
mysql> UPDATE runoob_tbl SET runoob_title='学习 C++' WHERE runoob_id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
DELETE statement
DELETE FROM table_name [WHERE Clause]
mysql> DELETE FROM runoob_tbl WHERE runoob_id=3;
Query OK, 1 row affected (0.01 sec)
LIKE statement
SELECT field1, field2,...fieldN FROM table_name WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'
- %: Represents any 0 or more characters. It can match characters of any type and length. In some cases, if it is Chinese, please use two percent signs (%%) to indicate it.
- _: Represents any single character. Match a single arbitrary character, it is often used to limit the character length of the expression.
- []: Represents one of the characters listed in the brackets (similar to regular expressions). Specify a character, string, or range, and require the matched object to be any of them.
- [^]: Indicates a single character not included in the brackets. Its value is the same as [], but it requires that the matched object is any character other than the specified character.
mysql> SELECT * from runoob_tbl WHERE runoob_title LIKE '%SQL';
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 2 | 学习 MySQL | 菜鸟教程 | 2020-12-09 |
+-----------+--------------+---------------+-----------------+
1 row in set (0.00 sec)
Common operations of data table fields
Modify the table name of the data table
alter table old_table rename to new_table
mysql> show tables;
+-------------------+
| Tables_in_looking |
+-------------------+
| comm_config |
| runoob_tbl |
| tb_person |
| tb_person_copy |
| test1 |
+-------------------+
5 rows in set (0.00 sec)
mysql> alter table runoob_tbl rename to runoob_tbl_new;
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-------------------+
| Tables_in_looking |
+-------------------+
| comm_config |
| runoob_tbl_new |
| tb_person |
| tb_person_copy |
| test1 |
+-------------------+
5 rows in set (0.00 sec)
Create a table with the same structure as the original table
create table new_table like old_table
mysql> show tables;
+-------------------+
| Tables_in_looking |
+-------------------+
| comm_config |
| runoob_tbl_new |
| tb_person |
| tb_person_copy |
| test1 |
+-------------------+
5 rows in set (0.00 sec)
mysql> create table runoob_tbl_new2 like runoob_tbl_new;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-------------------+
| Tables_in_looking |
+-------------------+
| comm_config |
| runoob_tbl_new |
| runoob_tbl_new2 |
| tb_person |
| tb_person_copy |
| test1 |
+-------------------+
6 rows in set (0.00 sec)
Add a field to the specified position of the table
alter table test_tb add i int after runoob_id
If there is no keyword such as after, it will be added at the end by default.
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
mysql> alter table test_tb add i int after runoob_id;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| i | int(11) | YES | | NULL | |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table test_tb add i int;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
| i | int(11) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
Delete table field
alter table test_tb drop i
mysql> show columns from test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| i | int(11) | YES | | NULL | |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> alter table test_tb drop i;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
Modify field type and name
alter table test_tb change runoob_id id int
mysql> desc test_tb;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> alter table test_tb change runoob_id id int;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test_tb;
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Modify field default value
ALTER TABLE tableName MODIFY j BIGINT NOT NULL DEFAULT 100
ALTER TABLE tableName ALTER i SET DEFAULT 1000
Delete field foreign key constraints
alter table tableName drop foreign key keyName
Copy of data table
Get the complete structure of the data table
mysql> show create table test_tb \G;
*************************** 1. row ***************************
Table: test_tb
Create Table: CREATE TABLE `test_tb` (
`id` int(11) NOT NULL,
`runoob_title` varchar(100) NOT NULL,
`runoob_author` varchar(40) NOT NULL,
`submission_date` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
CREATE TABLE targetTable LIKE sourceTable;
INSERT INTO targetTable SELECT * FROM sourceTable;
Copy table structure and data
create table targetTable select * from sourceTable
mysql> create table new_table select * from runoob_tbl_new;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from new_table;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 1 | 学习 PHP | 菜鸟教程 | 2020-12-09 |
| 2 | 学习 MySQL | 菜鸟教程 | 2020-12-09 |
+-----------+--------------+---------------+-----------------+
2 rows in set (0.00 sec)
Copy table structure
create table targetTable select * from sourceTable where 1=2
mysql> create table new_table2 select * from runoob_tbl_new where 1=2;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create table new_table3 like runoob_tbl_new;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from new_table2;
Empty set (0.00 sec)
mysql> select * from new_table3;
Empty set (0.00 sec)