1. 程式人生 > 資訊 >英雄聯盟官方宣佈《雲頂之弈:雙城之戰》將於 11 月 4 日正式上線

英雄聯盟官方宣佈《雲頂之弈:雙城之戰》將於 11 月 4 日正式上線

MySQL常用語句
一、MySQL啟動與停止
1.啟動MySQL服務
NET START mysql
2.停止MySQL服務
NET STOP mysql

二、登入MySQL
1.命令列MySQL的路徑
cd C:\MySQL\MySQL Server 5.7\bin
2.登入MySQL資料庫
mysql -h localhost -u root -p

三、資料庫操作

1>.建立資料庫
1.建立資料庫test_db
CREATE DATABASE test_db
2.建立資料庫如果不存在
CREATE DATABASE IF NOT EXISTS test_db
3.建立資料庫時指定字符集和校對規則
CREATEDATABASE test_db
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_chinese_ci

2>.檢視資料庫
1.檢視所有資料庫
SHOW DATABASES
2.檢視名為test_db的資料庫
SHOW DATABASES LIKE 'test_db'
3.檢視名字中包含test的資料庫
SHOW DATABASES LIKE '%test%'
4.檢視名字以db開頭的資料庫
SHOW DATABASES LIKE 'db%'
5.檢視名字以test結尾的資料庫
SHOW DATABASES LIKE '%test'

3>.修改資料庫
1.修改資料庫
ALTER DATABASE test_db
DEFAULT CHARACTER SET gb2312
DEFAULT COLLATE gb2312_chinese_ci

4>.刪除資料庫
1.刪除資料庫
DROP DATABASE test_db
2.如果有test_db資料庫,則刪除
DROP DATABASE IF EXISTS test_db

5>.選擇資料庫
1.指定資料庫
USE test_db

6>.MySQL註釋
1.單行註釋
#註釋內容

-- 註釋內容(注意內容前的空格)
2.多行註釋
/*
第一行
第二行
*/

7>.大小寫規範
mysql語句的關鍵字和函式名沒有大小寫之分,select和SELECT和SeLect都是一樣的,
但是表的別名、欄位名都是區分大小寫的,操作資料庫過程中要注意欄位名、表名等是否一致,

8>.備份資料庫
mysqldump -u root -p 123456 db > db.sql
mysqldump -u root -p 123456 -A > all.sql

9>.恢復資料
mysql -u root -p 123456 db < db.sql


四、使用者管理:
1.新建使用者:
CREATE user name
2.更改密碼:
set password for name=111111

五、資料表基本操作
1>.建立資料表
/*
欄位名:id 資料型別:int(11) 備註:員工編號
欄位名:name 資料型別:varchar(25) 備註:員工名稱
欄位名:deptid 資料型別:int(11) 備註:部門編號
欄位名:salary 資料型別:float 備註:工資
*/
CREATE TABLE table1
(
id int(11), -- 建立欄位id,int型別,限制11位數
name VARCHAR(25),
deptid int(11),
salary FLOAT
)

2>.增加欄位
1.增加列deptname,型別VARCHAR(25)
ALTER TABLE tb1 ADD deptname VARCHAR(25)
2.在student表的第一列增加stuId欄位
ALTER TABLE student ADD stuId INT(4) FIRST
3.在name欄位後新增stuno欄位
ALTER TABLE student ADD stuno INT(11) AFTER name

3>.修改資料表
1.修改表名
ALTER TABLE table1 RENAME TO tb1
2.修改表中欄位名deptid改為deid
ALTER TABLE tb1 CHANGE deptid deid int(8)
3.修改資料型別
ALTER TABLE tb1 CHANGE deid int(8)
4.修改欄位資料型別
ALTER TABLE tb1 MODIFY deid int(11)
5.刪除欄位
ALTER TABLE tb1 DROP deptname
6.修改表字符集
ALTER TABLE tb1 CHARACTER SET gb2312 DEFAULT COLLATE gb2312_chinese_ci

4>.刪除資料表
1.刪除資料表
DROP TABLE tb1
2.刪除關聯的表
①.建立關聯表
CREATE TABLE tb_1
(
id INT(11) PRIMARY KEY,
name VARCHAR(22),
location VARCHAR(50)
)
CREATE TABLE tb_2
(
ID INT(11) PRIMARY KEY,
NAME vARCHAR(25),
deptid INT(11),
SALARY FLOAT,
CONSTRAINT fk_emp1_emp2 FOREIGN KEY (deptid) REFERENCES tb_1(id)
)
②.解除tb_2的外來鍵約束
ALTER TABLE tb_2 DROP FOREIGN KEY fk_emp1_emp2
③.解除關聯關係後,就可以使用drop table 語句刪除tb_1
DROP TABLE tb_1

5>.查看錶結構
1.以表格形式展示表的欄位資訊
DESC tb1
2.以sql語句形式展示表結構
SHOW CREATE TABLE tb1

六、常用增刪查改
1>.增加表中資料
1.增加資料包含表中全部欄位內容
insert into student values (1,'hjk','男',23,11111)
2.增加資料包含表中部分欄位資料
INSERT INTO student (name,sex,age,phone) values ('jh','男',23,10000)

2>.查詢表中資料
1.無條件的查詢表中資料
select * from student
2.查詢表中部分資料
select name,phone from student
3.查詢表中第一到五條資料
select * from student limit 1,5
4.單條件的查詢表中資料
select * from student where id=1
5.多條件查詢表中資料
select * from student where name='' and sex='男'
6.模糊查詢名字中含jk的學生資訊
select * from student where name like '%jk%'
7.模糊查詢學生姓名為h開頭的兩字名 -- 下劃線_表示任意一個單字元,h後面只有一個字
select * from student where name like 'h_'
8.給表起別名,as可省略
select a.stuname as '學生姓名',a.phone '學生電話' from student as a where a.stuId=1
9.查詢名字在這列表中的學生資訊
select * from student where a.stuname in ('hj','hjk','jk')
10.查詢年齡在20-25之間的學生資訊
select * from student where age between 20 and 25
11.查詢到的資訊根據id升序,age降序
select * from student order by id asc,money desc
12.去重查詢
select distinct a.stuname from student a
13.統計符合要求的人數
select count(*) from student where sex='男'
14.查詢最大值
select max(age) from student
15.查詢最小值
select min(age) from student
16.查詢平均值
select avg(age) from student
17.查詢結果分組
select * from student group by sex
18.多條件查詢
select * from student where age=23 group by sex having stuname like '%jk%' -- group by後還有條件必須要用having子句
19.多個欄位進行分組
select * from student group by sex,age
20.合併兩條select語句的結果,欄位數和資料型別都要一致
select stuname,age from student union select select teaname,age from teacher -- 還有union all,它和union的區別在於它不會去重
21.兩表關聯無條件查詢
select * from student s, course c where s.coid=c.id
22.兩表關聯含條件查詢
select * from student s, course c where
s.coid = c.id
and s.age = 23
23.左連線查詢
select * from student s left join course c on s.coid=c.id
24.右連線查詢
select * from student s right join course c on s.coid=c.id
25.內連線查詢
select * from student s inner join course c on s.coid=c.id
26.子查詢,把子查詢的結果當做另一條sql的條件
select * from course c where c.id = (select coid from student where stuname='hj')
27.子查詢,把子查詢的結果當做一個表
select c.name,s.stuname,s.id from course c,(select id,stuname from student where phone=10000) where c.id=s.coid