標籤:欄位 add from modify 賬戶 mysql\ mysql學習 for rename
1.什麼是資料庫?
資料的倉庫,如在ATM的樣本中建立了一個db目錄,稱其為資料庫
2.安裝
下載http://dev.mysql.com/downloads/mysql/
安裝
windows:
(1)解壓:如果想要讓MySQL安裝在指定目錄,那麼就將解壓後的檔案夾移動到指定目錄;
(2)初始化:MySQL解壓後的bin目錄下有一大堆可執行檔,執行如下命令初始化資料
D:\mysql\mysql5.7.16\mysql-5.7.16-winx64\bin\mysql --initialize-insecure
(3)啟動MySQL服務:
#進入可執行檔目錄
cd D:\mysql\mysql5.7.16\mysql-5.7.16-winx64\bin
#啟動MySQL服務
mysql
(4)啟動MySQL用戶端並串連MySQL服務
由於初始化資料時未給root賬戶設定密碼
#進入可執行檔目錄
cd D:\mysql\mysql5.7.16\mysql-5.7.16-winx64\bin
#串連MySQL伺服器
mysql -u root -p
#提示輸入密碼,直接斷行符號即可
見如表示安裝成功:
2.1添加環境變數
【右鍵電腦】--》【屬性】--》【進階系統設定】--》【進階】--》【環境變數】
以後再啟動服務並串連時,僅需:
#啟動MySQL服務,在終端輸入mysqld#串連MySQL服務,在終端輸入mysql -u root -p
3.資料庫操作
(1)顯示資料庫
show databases;
(2)建立資料庫
create database db2;create database db2 default charset utf8;
#查看資料庫
show databases;
#使用資料庫
use 資料庫名稱
#刪除資料庫名稱為db2的資料庫
drop database db2;
(3)使用者管理
#建立使用者create user "alex"@"192.168.1.1" ["使用者名稱”@“IP地址"] identified by "123123"[“密碼”];#刪除使用者drop user "alex"@"192.168.1.1"["使用者名稱”@“IP地址"];#修改使用者rename user "alex"@"192.168.1.1["使用者名稱”@“IP地址"];to "alex1"@"192.168.1.2["新使用者名稱”@“新IP地址"]#修改密碼set password for "alex"@"192.168.1.1["使用者名稱”@“IP地址"] = Password("新密碼")
(4)授權管理
#查看許可權show grants for "使用者"@“IP地址”#授權grant 許可權 on 資料庫.表 to "使用者"@“IP地址”#取消許可權revoke 許可權 on 資料庫.表 from "使用者"@“IP地址”
4.基本資料表
( 1 )建立表
create table 表名( 列名 類型 是否可以為空白, 列名 類型 是否可以為空白)engine=innodb default charset=utf8
( 2 )刪除表
drop table 表名
(3)清空表
delete from 表名truncate table 表名
(4)修改表
添加列:alter table 表名 add 列名 類型刪除列:alter table 表名 drop column 列名修改列: alter table 表名 modify column 列名 類型; -- 類型 alter table 表名 change 原列名 新列名 類型; -- 列名,類型 添加主鍵: alter table 表名 add primary key(列名);刪除主鍵: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);刪除外鍵:alter table 表名 drop foreign key 外鍵名稱 修改預設值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;刪除預設值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
5.基礎資料型別 (Elementary Data Type)
MySQL的資料類型大致分為:數值,時間和字串
(1)整數類型
tinyint, smallint, mediumint, int/intrger, bigint
(2)浮點數類型
float, double
(3)日期時間
date, datetime, timestamp, time, year
(4)字串類型
char 速度快
varchar 節省空間的
6.表內容操作
(1)增
insert into 表 (列名,列名...) values (值,值,值...)insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)insert into 表 (列名,列名...) select (列名,列名...) from 表
(2)刪
delete from 表delete from 表 where id=1 and name=‘alex‘
(3)改
update 表 set name = ‘alex‘ where id>1
(4)查
select * from 表select * from 表 where id > 1select nid,name,gender as gg from 表 where id > 1
(5)其他
a、條件 select * from 表 where id > 1 and name != ‘alex‘ and num = 12; select * from 表 where id between 5 and 16; select * from 表 where id in (11,22,33) select * from 表 where id not in (11,22,33) select * from 表 where id in (select nid from 表) b、萬用字元 select * from 表 where name like ‘ale%‘ - ale開頭的所有(多個字串) select * from 表 where name like ‘ale_‘ - ale開頭的所有(一個字元) c、限制 select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 從第4行開始的5行 select * from 表 limit 5 offset 4 - 從第4行開始的5行 d、排序 select * from 表 order by 列 asc - 根據 “列” 從小到大排列 select * from 表 order by 列 desc - 根據 “列” 從大到小排列 select * from 表 order by 列1 desc,列2 asc - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序 e、分組 select num from 表 group by num select num,nid from 表 group by num,nid select num,nid from 表 where nid > 10 group by num,nid order nid desc select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid select num from 表 group by num having max(id) > 10 特別的:group by 必須在where之後,order by之前 f、連表 無對應關係則不顯示 select A.num, A.name, B.name from A,B Where A.nid = B.nid 無對應關係則不顯示 select A.num, A.name, B.name from A inner join B on A.nid = B.nid A表所有顯示,如果B中無對應關係,則值為null select A.num, A.name, B.name from A left join B on A.nid = B.nid B表所有顯示,如果B中無對應關係,則值為null select A.num, A.name, B.name from A right join B on A.nid = B.nid g、組合 組合,自動處理重合 select nickname from A union select name from B 組合,不處理重合 select nickname from A union all select name from B
mysql學習1