存儲在系統中的數據是數據庫管理系統(DBMS)的核心,數據庫被設計用來管理數據的存儲、訪問和維護數據的完整性,MariaDB中提供了功能豐富的數據庫管理語句,包括有效地向數據庫中插入數據的INSERT語句,更新數據的UPDATE語句以及當數據不再使用時刪除數據的DELETE語句,本小結將依次來介紹這些命令的使用方法和技巧.
MariaDB 插入數據
MariaDB中使用INSERT語句插入數據,可以插入的方式有:插入完整記錄,插入記錄的部分,插入多條記錄,插入另一個查詢的結果,廢話不多說,老樣子先來看一下插入語句的寫法吧:
INSERT INTO 表名稱(字段1,字段2,字段3,.....) VALUES(數值1,數值2,數值3....)
為了方便后續的練習,我們先來創建一個表結構,SQL語句如下:
MariaDB [lyshark]> create table person
-> (
-> id int unsigned not null auto_increment,
-> name char(50) not null default '',
-> age int not null default 0,
-> info char(50) null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.00 sec)
◆在所有字段插入數據◆
在person
表中,插入一條新記錄id=1,name=LyShark,age=22,info=Lawyer
,SQL語句如下:
MariaDB [lyshark]> select * from person;
Empty set (0.00 sec)
MariaDB [lyshark]> insert into person(id,name,age,info) values(1,'LyShark',22,'Lawyer');
Query OK, 1 row affected (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------+
| id | name | age | info |
+----+---------+-----+--------+
| 1 | LyShark | 22 | Lawyer |
+----+---------+-----+--------+
1 row in set (0.00 sec)
MariaDB [lyshark]>
◆在指定字段插入數據◆
在person
表中,插入一條新記錄,name=Willam,age=18,info=sports
,我們不給其指定ID,SQL語句如下:
MariaDB [lyshark]> desc person;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(50) | NO | | | |
| age | int(11) | NO | | 0 | |
| info | char(50) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(name,age,info) values('Willam',18,'sports man');
Query OK, 1 row affected (0.04 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
+----+---------+-----+------------+
2 rows in set (0.00 sec)
MariaDB [lyshark]>
◆同時為表插入多條記錄◆
在person
表中,同時插入3條新記錄,有多條只需要在每一條的后面加,
即可,SQL語句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
+----+---------+-----+------------+
2 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(name,age,info) values('Evans',27,'secretary'),
-> ('Dale',22,'cook'),
-> ('Edison',28,'singer');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
+----+---------+-----+------------+
5 rows in set (0.00 sec)
◆將查詢結果插入到表中◆
為了實現將另一個表中的記錄插入到本表中,我們新建一個person_old
表,其表結構和person相同,我們將person_old
表中的內容全部遷移到person
中去,SQL語句如下:
1.創建一個person_old
表,並插入測試字段:
MariaDB [lyshark]> create table person_old
-> (
-> id int unsigned not null auto_increment,
-> name char(50) not null default '',
-> age int not null default 0,
-> info char(50) null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [lyshark]> insert into person_old
-> values(11,'harry',20,'student'),(12,'Beckham',33,'police');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
2.接下來我們將person_old
表中的內容遷移到person
中去
MariaDB [lyshark]> select * from person_old;
+----+---------+-----+---------+
| id | name | age | info |
+----+---------+-----+---------+
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+---------+
2 rows in set (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
+----+---------+-----+------------+
5 rows in set (0.00 sec)
MariaDB [lyshark]> insert into person(id,name,age,info)
-> select id,name,age,info from person_old;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
## MariaDB 更新數據
表中有數據之后,接下來我們可以對數據進行更新操作,MariaDB中使用UPDATE
語句更新表中的記錄,可以更新特定的行或同時更新所有的行,基本語句結構如下:
UPDATE 表名稱
SET 字段1=修改值,字段2=修改值,字段3=修改值
where (限定條件);
◆更新表中指定字段◆
修改person
表中數據,將id=11
的name字段
的值改為xxxx
,age字段改為200
,SQL語句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | harry | 20 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> update person set age=200,name='xxxx' where id=11; #更新單個字段
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | xxxx | 200 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
◆更新表的一個范圍◆
更新person表中的記錄,將1-12
的info字段
全部改為lyshark blog
,SQL語句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+------------+
| id | name | age | info |
+----+---------+-----+------------+
| 1 | LyShark | 22 | Lawyer |
| 2 | Willam | 18 | sports man |
| 3 | Evans | 27 | secretary |
| 4 | Dale | 22 | cook |
| 5 | Edison | 28 | singer |
| 11 | xxxx | 200 | student |
| 12 | Beckham | 33 | police |
+----+---------+-----+------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> update person set info='lyshark blog' where age between 1 and 200; #指定修改的字段
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
| 12 | Beckham | 33 | lyshark blog |
+----+---------+-----+--------------+
7 rows in set (0.00 sec)
## MariaDB 刪除數據
◆刪除表中指定記錄◆
通過id號,刪除表中指定列,此處刪除第id=12
號,這條記錄,SQL語句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
| 12 | Beckham | 33 | lyshark blog |
+----+---------+-----+--------------+
7 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person where id=12; #通過id號,刪除表中指定列
Query OK, 1 row affected (0.05 sec)
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+---------+-----+--------------+
6 rows in set (0.00 sec)
◆刪除表的一個范圍◆
在person表
中,刪除age字段值
在19-22
的記錄,SQL語句如下:
MariaDB [lyshark]> select * from person;
+----+---------+-----+--------------+
| id | name | age | info |
+----+---------+-----+--------------+
| 1 | LyShark | 22 | lyshark blog |
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 4 | Dale | 22 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+---------+-----+--------------+
6 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person where age between 19 and 22; #指定范圍刪除
Query OK, 2 rows affected (0.00 sec)
MariaDB [lyshark]> select * from person;
+----+--------+-----+--------------+
| id | name | age | info |
+----+--------+-----+--------------+
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+--------+-----+--------------+
4 rows in set (0.00 sec)
◆清空表中所有記錄◆
MariaDB [lyshark]> select * from person;
+----+--------+-----+--------------+
| id | name | age | info |
+----+--------+-----+--------------+
| 2 | Willam | 18 | lyshark blog |
| 3 | Evans | 27 | lyshark blog |
| 5 | Edison | 28 | lyshark blog |
| 11 | xxxx | 200 | lyshark blog |
+----+--------+-----+--------------+
4 rows in set (0.00 sec)
MariaDB [lyshark]> delete from person; #清空表中所有記錄
Query OK, 4 rows affected (0.00 sec)
MariaDB [lyshark]> select * from person;
Empty set (0.00 sec)
參考文獻:mysql5.7從入門到精通