目錄
1,表的創建
create tab <表名>(<字段>, <類型>, ...);
如:插入以下數據
id | name | sex | age | interest | brithday |
10001 | 小明 | boy | 16 | football | 2004-3-5 |
10002 | 小紅 | girl | 14 | music | 2006-6-18 |
10003 | 小張 | boy | 15 | painting | 2005-08-14 |
10004 | 露絲 | girl | 13 | dance | 2007-07-19 |
MariaDB [mydb]> create table students( id int(10), name varchar(10), sex varchar(10), age int(3), interest varchar(100), birthday date );
Query OK, 0 rows affected (0.043 sec)
查看錶結構
desc <表名>;
MariaDB [mydb]> desc students;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| sex | varchar(10) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
| interest | varchar(100) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
6 rows in set (0.005 sec)
2,插入數據
inster into <table_name> ( field1, field2,...fieldN )
values
( value1, value2,...valueN );
方式一:在添加數據的時候(如果我們指定了添加的列名,那麼需要根據指定列名的順序去添加對應的值) students中的key和value要一一對應
MariaDB [mydb]> insert into students(id,name,sex,age,interest,birthday) values(10001,'小明','boy',16,'football','2004-3-5');
Query OK, 1 row affected (0.008 sec)
方式二:在沒有指定列名的時候我們添加的值要和表的列名一一對應
MariaDB [mydb]> insert into students values(10002,'小紅', 'girl', '14', 'music', '2006-6-18');
Query OK, 1 row affected (0.007 sec)
(1)列名順序自定義,但是列值會和列名的順序一一對應
(2)數字類型的數據不需要加單引號,字符串類型或日期類型的值需要添加單引號
(3)列名部分的內容可以省略,但是此時該列值的順序要和創建表時列的順序一致
一次插入多行數據:
MariaDB [mydb]> insert into students values(10003,'小張','boy',15,'painting','2005-8-14'),(10004,'露絲','girl','13','dance','2007-7-19');
Query OK, 2 rows affected (0.008 sec)
Records: 2 Duplicates: 0 Warnings: 0
3,查詢數據
select * from <表名>;
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 10001 | 小明 | boy | 16 | football | 2004-03-05 |
| 10002 | 小紅 | girl | 14 | music | 2006-06-18 |
| 10003 | 小張 | boy | 15 | painting | 2005-08-14 |
| 10004 | 露絲 | girl | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.000 sec)
select <field1, field2,...fieldN> from <table_name1, table_name2...>
[where condition1 [AND [OR]] condition2.....
- 查詢語句中你可以使用一個或者多個表,表之間使用逗號, 分割,並使用WHERE語句來設定查詢條件。
- 你可以在 WHERE 子句中指定任何條件。
- 你可以使用 AND 或者 OR 指定一個或多個條件。
- WHERE 子句也可以運用於 SQL 的 DELETE 或者 UPDATE 命令。
- WHERE 子句類似於程序語言中的 if 條件,根據 MySQL 表中的字段值來讀取指定的數據
操作符 | 描述 | 實例 |
---|---|---|
= | 等號,檢測兩個值是否相等,如果相等返回true | (A = B) 返回false。 |
<>, != | 不等於,檢測兩個值是否相等,如果不相等返回true | (A != B) 返回 true。 |
> | 大於號,檢測左邊的值是否大於右邊的值, 如果左邊的值大於右邊的值返回true | (A > B) 返回false。 |
< | 小於號,檢測左邊的值是否小於右邊的值, 如果左邊的值小於右邊的值返回true | (A < B) 返回 true。 |
>= | 大於等於號,檢測左邊的值是否大於或等於右邊的值, 如果左邊的值大於或等於右邊的值返回true | (A >= B) 返回false。 |
<= | 小於等於號,檢測左邊的值是否小於或等於右邊的值, 如果左邊的值小於或等於右邊的值返回true | (A <= B) 返回 true。 |
查詢查詢students表中id等於10001的學生
MariaDB [mydb]> select * from students where id=10001;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 10001 | 小明 | boy | 16 | football | 2004-03-05 |
+-------+--------+------+------+----------+------------+
1 row in set (0.001 sec)
查詢查詢students表中id不等於10001的學生
MariaDB [mydb]> select * from students where id!=10001;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 10002 | 小紅 | girl | 14 | music | 2006-06-18 |
| 10003 | 小張 | boy | 15 | painting | 2005-08-14 |
| 10004 | 露絲 | girl | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
3 rows in set (0.001 sec)
查詢查詢students表中id大於10001並且小於10004的學生
MariaDB [mydb]> select * from students where id>10001 and id<10004;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 10002 | 小紅 | girl | 14 | music | 2006-06-18 |
| 10003 | 小張 | boy | 15 | painting | 2005-08-14 |
+-------+--------+------+------+----------+------------+
2 rows in set (0.001 sec)
查詢查詢students表中id大於10001並且小於10004的學生,只輸出學生name和id
MariaDB [mydb]> select id,name from students where id>10001 and id<10004;
+-------+--------+
| id | name |
+-------+--------+
| 10002 | 小紅 |
| 10003 | 小張 |
+-------+--------+
2 rows in set (0.001 sec)
4,更新數據
update <table_name> set <field1=new-value1, field2=new-value2>
[where Clause]
- 你可以同時更新一個或多個字段。
- 你可以在 WHERE 子句中指定任何條件。
- 你可以在一個單獨表中同時更新數據。
將id是10001學生的id修改爲20001
MariaDB [mydb]> update students set id=20001 where id=10001;
Query OK, 1 row affected (0.009 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | boy | 16 | football | 2004-03-05 |
| 10002 | 小紅 | girl | 14 | music | 2006-06-18 |
| 10003 | 小張 | boy | 15 | painting | 2005-08-14 |
| 10004 | 露絲 | girl | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)
將name=‘小紅’的id修改爲20002
MariaDB [mydb]> update students set id=20002 where name='小紅';
Query OK, 1 row affected (0.008 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | boy | 16 | football | 2004-03-05 |
| 20002 | 小紅 | girl | 14 | music | 2006-06-18 |
| 10003 | 小張 | boy | 15 | painting | 2005-08-14 |
| 10004 | 露絲 | girl | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)
將性別改爲中文顯示
MariaDB [mydb]> update students set sex='男' where sex='boy';
Query OK, 2 rows affected (0.008 sec)
Rows matched: 2 Changed: 2 Warnings: 0
MariaDB [mydb]> update students set sex='女' where sex='girl';
Query OK, 2 rows affected (0.010 sec)
Rows matched: 2 Changed: 2 Warnings: 0
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 10003 | 小張 | 男 | 15 | painting | 2005-08-14 |
| 10004 | 露絲 | 女 | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)
將id<20000的批量增加10000
MariaDB [mydb]> update students set id=id+10000 where id<20000;
Query OK, 2 rows affected (0.008 sec)
Rows matched: 2 Changed: 2 Warnings: 0
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | painting | 2005-08-14 |
| 20004 | 露絲 | 女 | 13 | dance | 2007-07-19 |
+-------+--------+------+------+----------+------------+
4 rows in set (0.001 sec)
當我們需要將字段中的特定字符串批量修改爲其他字符串時,可已使用以下操作:
UPDATE table_name SET field=REPLACE(field, 'old-string', 'new-string') [WHERE Clause]
MariaDB [mydb]> update students set interest = replace(interest, 'dance', 'painting');
Query OK, 2 rows affected (0.009 sec)
Rows matched: 5 Changed: 2 Warnings: 0
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 20003 | 小張 | 男 | 15 | painting | 2005-08-14 |
| 20004 | 露絲 | 女 | 13 | painting | 2007-07-19 |
| 20005 | 麗麗 | 女 | 15 | painting | 2005-12-01 |
+-------+--------+------+------+----------+------------+
5 rows in set (0.001 sec)
新增一列
如果想在一個已經建好的表中添加一列,可以用諸如:
alter table <表名> add column <列名> <類型> not null;
這條語句會向已有的表中加入新的一列,這一列在表的最後一列位置。如果我們希望添加在指定的一列,可以用:
alter table <表名> add column <列名> <類型> not null after <在此列後面添加>;
注意,上面這個命令的意思是說添加新列到某一列後面。如果想添加到第一列的話,可以用:
alter table <表名> add column <列名> <類型> not null first;
刪除一列:
alter table <表名> drop column <列名>;
MariaDB [mydb]> alter table score add column class int(1) not null after id;
Query OK, 0 rows affected (0.071 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [mydb]> select * from score;
+-------+-------+-----------+------+----------+--------+
| id | class | name | math | language | sports |
+-------+-------+-----------+------+----------+--------+
| 20001 | 0 | 小明 | 81 | 86 | 93 |
| 20002 | 0 | 小紅 | 86 | 86 | 89 |
| 20003 | 0 | 小張 | 77 | 83 | 93 |
| 20004 | 0 | 露絲 | 88 | 78 | 65 |
| 20005 | 0 | 麗麗 | 92 | 94 | 64 |
| 20006 | 0 | 李明 | 75 | 78 | 88 |
| 20007 | 0 | 張大明 | 54 | 65 | 95 |
+-------+-------+-----------+------+----------+--------+
7 rows in set (0.001 sec)
5,刪除數據
delete from <table_name> [where Clause]
- 如果沒有指定 WHERE 子句,MySQL 表中的所有記錄將被刪除。
- 你可以在 WHERE 子句中指定任何條件
- 您可以在單個表中一次性刪除記錄。
刪除id=10004的學生
MariaDB [mydb]> delete from students where id=10004;
Query OK, 1 row affected (0.008 sec)
MariaDB [mydb]> select * from students;
+-------+--------+------+------+----------+------------+
| id | name | sex | age | interest | birthday |
+-------+--------+------+------+----------+------------+
| 20001 | 小明 | 男 | 16 | football | 2004-03-05 |
| 20002 | 小紅 | 女 | 14 | music | 2006-06-18 |
| 10003 | 小張 | 男 | 15 | painting | 2005-08-14 |
+-------+--------+------+------+----------+------------+
3 rows in set (0.001 sec)
delete,drop,truncate 都有刪除表的作用,區別在於:
- 1、delete 和 truncate 僅僅刪除表數據,drop 連表數據和表結構一起刪除,打個比方,delete 是單殺,truncate 是團滅,drop 是把電腦摔了。
- 2、delete 是 DML 語句,操作完以後如果沒有不想提交事務還可以回滾,truncate 和 drop 是 DDL 語句,操作完馬上生效,不能回滾,打個比方,delete 是發微信說分手,後悔還可以撤回,truncate 和 drop 是直接扇耳光說滾,不能反悔。
- 3、執行的速度上,drop>truncate>delete,打個比方,drop 是神舟火箭,truncate 是和諧號動車,delete 是自行車。
6,刪除表
drop database <數據庫名>;
MariaDB [mydb]> drop table students;
Query OK, 0 rows affected (0.048 sec)