Mysql基礎(chǔ)學習

創(chuàng)建數(shù)據(jù)表

CREATE TABLE table_name (column_name column_type);

create table test(
  `userid` int auto_increment,
  `username` varchar(32) not null,
  `userage` int not null,
  primary key ('userid')
)

1 如果你不想字段為 NULL 可以設(shè)置字段的屬性為 NOT NULL, 在操作數(shù)據(jù)庫時如果輸入該字段的數(shù)據(jù)為NULL ,就會報錯。
2 AUTO_INCREMENT定義列為自增的屬性,一般用于主鍵,數(shù)值會自動加1。
3 PRIMARY KEY關(guān)鍵字用于定義列為主鍵。 您可以使用多列來定義主鍵,列間以逗號分隔。
4 ENGINE 設(shè)置存儲引擎,CHARSET 設(shè)置編碼。

刪除數(shù)據(jù)表

DROP TABLE table_name ;

drop table test;

1 刪除表內(nèi)數(shù)據(jù),用 delete : delete from 表名 where 刪除條件;
2 清除表內(nèi)數(shù)據(jù),保存表結(jié)構(gòu),用 truncate: truncate table 表名;
3 刪除表用 drop,就是啥都沒了:DROP TABLE 表名;
4 delete 操作以后,使用 optimize table table_name ,會立刻釋放磁盤空間

插入數(shù)據(jù)

INSERT INTO table_name (xx)values (xx)

insert into test (username,userage) values ('test',100)

查詢數(shù)據(jù)

select column_name from 表名 [where clause] [limit n] [offset m]

select username from test where userid=1 limit 1 offset 1

基本查詢語句

SELECT * FROM websites;                      /* 查詢表所有數(shù)據(jù) */

SELECT NAME FROM websites;                   /* 查詢表字段數(shù)據(jù) */

SELECT * FROM websites where name = "廣西";   /* 查詢表字段下條件數(shù)據(jù) */

SELECT * from websites where name like "_o%"; /* 模糊查詢表下數(shù)據(jù) */

SELECT * FROM websites where id BETWEEN "1" AND "5";    /* 查詢表下字段范圍數(shù)據(jù) */

SELECT * FROM websites WHERE name in ("廣西","百度");    /* 查詢表字段下固定條件數(shù)據(jù) */

SELECT DISTINCT country FROM Websites;                  /* 查詢?nèi)ブ刂?*/

SELECT * FROM Websites WHERE country = "CN" AND alexa > 50;  /*查詢表下范圍條件數(shù)據(jù)*/

SELECT * FROM Websites WHERE country = "USA" OR country="sh"; /* 查詢表下條件不同值 */

SELECT * FROM Websites ORDER BY alexa;                      /* 查詢表下值排序結(jié)果 */

SELECT * FROM Websites ORDER BY alexa DESC;                 /* 查詢表下排序結(jié)果降序 */

SELECT * FROM Websites LIMIT 2;      /* 查詢表下范圍數(shù)據(jù) */

SELECT name as zzz from websites;    /*別名查詢表下數(shù)據(jù)*/

分頁查詢案例

select  *  from _table limit (page_number-1)*lines_perpage, lines_perpage
或者
select * from _table limit lines_perpage offset (page_number-1)*lines_perpage

1 查詢語句中你可以使用一個或者多個表,表之間使用逗號(,)分割,并使用WHERE語句來設(shè)定查詢條件。
2 SELECT 命令可以讀取一條或者多條記錄
3你可以使用星號(*)來代替其他字段,SELECT語句會返回表的所有字段數(shù)據(jù)
4 你可以使用 WHERE 語句來包含任何條件。
5 你可以通過OFFSET指定SELECT語句開始查詢的數(shù)據(jù)偏移量。默認情況下偏移量為0。
6 你可以使用 AND 或者 OR 指定一個或多個條件。

修改數(shù)據(jù)

UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

update test set username='xx' where userid=1;

1 你可以同時更新一個或多個字段。
2 你可以在 WHERE 子句中指定任何條件。
3 你可以在一個單獨表中同時更新數(shù)據(jù)。

刪除數(shù)據(jù)

DELETE FROM table_name [WHERE Clause]

delete from test where username='xx';

1 如果沒有指定 WHERE 子句,MySQL 表中的所有記錄將被刪除
2 你可以在 WHERE 子句中指定任何條件
3 您可以在單個表中一次性刪除記錄。

LIKE子句

select * from table_name where column_name like %xx;
匹配模式

'%a'     //以a結(jié)尾的數(shù)據(jù)
'a%'     //以a開頭的數(shù)據(jù)
'%a%'    //含有a的數(shù)據(jù)
'_a_'    //三位且中間字母是a的
'_a'     //兩位且結(jié)尾字母是a的
'a_'     //兩位且開頭字母是a的'%a'  

1 你可以使用LIKE子句代替等號 =。
2 LIKE 通常與 % 一同使用,類似于一個元字符的搜索。
3 你可以在 DELETE 或 UPDATE 命令中使用 WHERE...LIKE 子句來指定條件。

union操作符

UNION 操作符用于連接兩個以上的 SELECT 語句的結(jié)果組合到一個結(jié)果集合中。多個 SELECT 語句會刪除重復(fù)的數(shù)據(jù)。
select XX UNION [ALL | DISTINCT] select XX

1 ALL所有,包含重復(fù)數(shù)據(jù)。
2 DISTINCT刪除結(jié)果集中重復(fù)的數(shù)據(jù),默認值。

排序

SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]

select * from test order by userid ASC

你可以使用任何字段來作為排序的條件,從而返回排序后的查詢結(jié)果。
你可以設(shè)定多個字段來排序。
你可以使用 ASC 或 DESC 關(guān)鍵字來設(shè)置查詢結(jié)果是按升序或降序排列。 默認情況下,它是按升序排列。
你可以添加 WHERE...LIKE 子句來設(shè)置條件。

連接使用

SELECT XX FROM TABLE_NAME JOIN TABLE_NAME ON 條件

create table user (`username` varchar(32),`time` DATETIME);
select a.username b.time from test a join user b on b.username=a.username;

INNER JOIN(內(nèi)連接,或等值連接):獲取兩個表中字段匹配關(guān)系的記錄。
LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒有對應(yīng)匹配的記錄。
RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用于獲取右表所有記錄,即使左表沒有對應(yīng)匹配的記錄。

正則匹配使用

SELECT name FROM person_tbl WHERE name REGEXP 正則表達式;

select username from test where username regexp '^xx'

事務(wù)

事務(wù)處理主要有兩種方法:
BEGIN 開始一個事務(wù)
ROLLBACK 事務(wù)回滾
COMMIT 事務(wù)確認
直接用 SET 來改變 MySQL 的自動提交模式:
SET AUTOCOMMIT=0 禁止自動提交
SET AUTOCOMMIT=1 開啟自動提交

begin;
insert into test(username,userage) values('zz',11);
commit;
insert into test(username,userage) values('zzzz',1111);
rollback;

在 MySQL 中只有使用了 Innodb 數(shù)據(jù)庫引擎的數(shù)據(jù)庫或表才支持事務(wù)。
事務(wù)處理可以用來維護數(shù)據(jù)庫的完整性,保證成批的 SQL 語句要么全部執(zhí)行,要么全部不執(zhí)行。
事務(wù)用來管理 insert,update,delete 語句

ALTER命令

1 添加行

alter table test add country varchar(32);
show columns from test;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| userid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(32) | NO   |     | NULL    |                |
| userage  | int(11)     | YES  |     | NULL    |                |
| time     | datetime    | YES  |     | NULL    |                |
| country  | varchar(32) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

2 修改字段名和類型

alter table test change country countrys varchar(42);
show columns from test;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| userid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(32) | NO   |     | NULL    |                |
| userage  | int(11)     | YES  |     | NULL    |                |
| time     | datetime    | YES  |     | NULL    |                |
| countrys | varchar(42) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

3 刪除行

alter table test drop country;
show columns from test;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| userid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(32) | NO   |     | NULL    |                |
| userage  | int(11)     | YES  |     | NULL    |                |
| time     | datetime    | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

索引

1 添加索引和刪除索引。

alter table test add index  id (username);
alter table test drop index id;

Join

mysql> select * from user_write;
+----+-----+---------------+---------------------+
| id | uid | write         | ctime               |
+----+-----+---------------+---------------------+
|  1 |   1 | 學習寫        | 2018-10-19 15:20:21 |
|  2 |   2 | 學習寫sql     | 2018-10-19 15:30:21 |
|  3 |   1 | 學習寫Html    | 2018-10-19 15:30:21 |
+----+-----+---------------+---------------------+
3 rows in set (0.00 sec)

mysql> select * from user_read;
+----+-----+---------------+---------------------+
| id | uid | read          | ctime               |
+----+-----+---------------+---------------------+
|  1 |   1 | 學習          | 2018-10-19 14:49:21 |
|  2 |   2 | 學習寫sql     | 2018-10-19 14:54:00 |
|  3 |   3 | 學習寫Html    | 2018-10-19 15:12:46 |
+----+-----+---------------+---------------------+
3 rows in set (0.01 sec)

mysql> select * from user;
+----+----------+-----+-----+
| id | username | age | sex |
+----+----------+-----+-----+
|  1 | 西門0     |  24 |   1 |
|  2 | 西門1    |  24 |   1 |
|  3 | 西門2    |  24 |   1 |
|  4 | 西門3    |  33 |   2 |
+----+----------+-----+-----+
3 rows in set (0.01 sec)

1 怎么找到只read的人全部信息?

mysql> select * from user inner join user_read on user.id=user_read.uid;
+----+----------+-----+-----+----+-----+---------------+---------------------+
| id | username | age | sex | id | uid | read          | ctime               |
+----+----------+-----+-----+----+-----+---------------+---------------------+
|  1 | 西門0    |  24 |   1 |  1 |   1 | 學習          | 2018-10-19 14:49:21 |
|  2 | 西門1    |  24 |   1 |  2 |   2 | 學習寫sql     | 2018-10-19 14:54:00 |
|  3 | 西門2    |  24 |   1 |  3 |   3 | 學習寫Html    | 2018-10-19 15:12:46 |
+----+----------+-----+-----+----+-----+---------------+---------------------+
3 rows in set (0.01 sec)

2 怎么找到只write的人全部信息?

mysql> select * from user inner join user_write on user.id=user_write.uid;
+----+----------+-----+-----+----+-----+---------------+---------------------+
| id | username | age | sex | id | uid | write         | ctime               |
+----+----------+-----+-----+----+-----+---------------+---------------------+
|  1 | 楊旭     |  24 |   1 |  1 |   1 | 學習寫        | 2018-10-19 15:20:21 |
|  1 | 楊旭     |  24 |   1 |  3 |   1 | 學習寫Html    | 2018-10-19 15:30:21 |
|  2 | 西門1    |  24 |   1 |  2 |   2 | 學習寫sql     | 2018-10-19 15:30:21 |
+----+----------+-----+-----+----+-----+---------------+---------------------+
3 rows in set (0.00 sec)

3 找到write和read的人?

mysql> select * from user inner join user_read on user.id=user_read.uid inner join user_write on user.id=user_write.uid;
+----+----------+-----+-----+----+-----+--------------+---------------------+----+-----+---------------+---------------------+
| id | username | age | sex | id | uid | read         | ctime               | id | uid | write         | ctime               |
+----+----------+-----+-----+----+-----+--------------+---------------------+----+-----+---------------+---------------------+
|  1 | 西門0    |  24 |   1 |  1 |   1 | 學習         | 2018-10-19 14:49:21 |  1 |   1 | 學習寫        | 2018-10-19 15:20:21 |
|  2 | 西門1    |  24 |   1 |  2 |   2 | 學習寫sql    | 2018-10-19 14:54:00 |  2 |   2 | 學習寫sql     | 2018-10-19 15:30:21 |
|  1 | 西門0    |  24 |   1 |  1 |   1 | 學習         | 2018-10-19 14:49:21 |  3 |   1 | 學習寫Html    | 2018-10-19 15:30:21 |
+----+----------+-----+-----+----+-----+--------------+---------------------+----+-----+---------------+---------------------+
3 rows in set (0.01 sec)

4 找到不讀人?

mysql> select * from user left join user_read on user.id=user_read.uid where user_read.id is null;
+----+----------+-----+-----+------+------+------+-------+
| id | username | age | sex | id   | uid  | read | ctime |
+----+----------+-----+-----+------+------+------+-------+
|  4 | 西門3    |  33 |   2 | NULL | NULL | NULL | NULL  |
+----+----------+-----+-----+------+------+------+-------+
1 row in set (0.00 sec)

5 找到不寫的人?

mysql> select * from user left join user_write on user.id=user_write.uid where user_write.id is null;
+----+----------+-----+-----+------+------+-------+-------+
| id | username | age | sex | id   | uid  | write | ctime |
+----+----------+-----+-----+------+------+-------+-------+
|  3 | 西門2    |  24 |   1 | NULL | NULL | NULL  | NULL  |
|  4 | 西門3    |  33 |   2 | NULL | NULL | NULL  | NULL  |
+----+----------+-----+-----+------+------+-------+-------+
2 rows in set (0.00 sec)

6 找到不讀不寫的人?

mysql> select * from user left join user_read on user.id=user_read.uid left join user_write on user.id=user_write.uid where user_read.id is null and user_write.id is null;
+----+----------+-----+-----+------+------+------+-------+------+------+-------+-------+
| id | username | age | sex | id   | uid  | read | ctime | id   | uid  | write | ctime |
+----+----------+-----+-----+------+------+------+-------+------+------+-------+-------+
|  4 | 西門3    |  33 |   2 | NULL | NULL | NULL | NULL  | NULL | NULL | NULL  | NULL  |
+----+----------+-----+-----+------+------+------+-------+------+------+-------+-------+
1 row in set (0.00 sec)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容