答:數據的倉庫,如:在ATM的示例中咱們建立了一個 db 目錄,稱其爲數據庫html
答:他們均是一個軟件,都有兩個主要的功能:mysql
PS:若是有了以上軟件,無須本身再去建立文件和文件夾,而是直接傳遞 命令 給上述軟件,讓其來進行文件操做,他們統稱爲數據庫管理系統(DBMS,Database Management System)sql
答:上述提到MySQL等軟件能夠接受命令,並作出相應的操做,因爲命令中能夠包含刪除文件、獲取文件內容等衆多操做,對於編寫的命令就是是SQL語句。SQL是結構化語言(Structured Query Language)的縮寫,SQL是一種專門用來與數據庫通訊的語言。數據庫
MySQL是一個關係型數據庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下公司。MySQL 最流行的關係型數據庫管理系統,在 WEB 應用方面MySQL是最好的 RDBMS (Relational Database Management System,關係數據庫管理系統) 應用軟件之一。windows
想要使用MySQL來存儲並操做數據,則須要作幾件事情:
a. 安裝MySQL服務端
b. 安裝MySQL客戶端
b. 【客戶端】鏈接【服務端】
c. 【客戶端】發送命令給【服務端MySQL】服務的接受命令並執行相應操做(增刪改查等)服務器
# 下載 http://dev.mysql.com/downloads/mysql/ # 安裝 windows: 點點點 Linux: yum install mysql-server Mac: 點點點
一、顯示數據庫session
SHOW DATABASES;
默認數據庫:
mysql - 用戶權限相關數據
test - 用於用戶測試數據
information_schema - MySQL自己架構相關數據架構
二、使用數據庫ide
USE db_name;
三、顯示全部表測試
SHOW TABLES;
四、用戶受權
用戶管理:
# 建立用戶 create user '用戶名'@'IP地址' identified by '密碼'; # 刪除用戶 drop user '用戶名'@'IP地址'; # 修改用戶 rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址';; # 修改密碼 set password for '用戶名'@'IP地址' = Password('新密碼')
受權管理:
show grants for '用戶'@'IP地址' -- 查看權限 grant 權限 on 數據庫.表 to '用戶'@'IP地址' -- 受權 revoke 權限 on 數據庫.表 from '用戶'@'IP地址' -- 取消權限
all privileges 除grant外的全部權限 select 僅查權限 select,insert 查和插入權限 ... usage 無訪問權限 alter 使用alter table alter routine 使用alter procedure和drop procedure create 使用create table create routine 使用create procedure create temporary tables 使用create temporary tables create user 使用create user、drop user、rename user和revoke all privileges create view 使用create view delete 使用delete drop 使用drop table execute 使用call和存儲過程 file 使用select into outfile 和 load data infile grant option 使用grant 和 revoke index 使用index insert 使用insert lock tables 使用lock table process 使用show full processlist select 使用select show databases 使用show databases show view 使用show view update 使用update reload 使用flush shutdown 使用mysqladmin shutdown(關閉MySQL) super 使用change master、kill、logs、purge、master和set global。還容許mysqladmin調試登錄 replication client 服務器位置的訪問 replication slave 由複製從屬使用 對於權限
對於目標數據庫以及內部其餘: 數據庫名.* 數據庫中的全部 數據庫名.表 指定數據庫中的某張表 數據庫名.存儲過程 指定數據庫中的存儲過程 *.* 全部數據庫
用戶名@IP地址 用戶只能在改IP下才能訪問 用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(通配符%表示任意) 用戶名@% 用戶能夠再任意IP下訪問(默認IP地址爲%)
grant all privileges on db1.tb1 TO '用戶名'@'IP' grant select on db1.* TO '用戶名'@'IP' grant select,insert on *.* TO '用戶名'@'IP' revoke select on db1.tb1 from '用戶名'@'IP'
一、建立表
create table 表名(
列名 類型 是否能夠爲空,
列名 類型 是否能夠爲空
)
是否可空,null表示空,非字符串 not null - 不可空 null - 可空
#默認值,建立列時能夠指定默認值,當插入數據時若是未主動設置,則自動添加默認值 create table tb1( nid int not null defalut 2, num int not null )
# 自增,若是爲某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列) create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null auto_increment, num int null, index(nid) ) # 注意:一、對於自增列,必須是索引(含主鍵)。 # 二、對於自增能夠設置步長和起始值 show session variables like 'auto_inc%'; set session auto_increment_increment=2; set session auto_increment_offset=10; shwo global variables like 'auto_inc%'; set global auto_increment_increment=2; set global auto_increment_offset=10;
#主鍵,一種特殊的惟一索引,不容許有空值,若是主鍵使用單個列,則它的值必須惟一,若是是多列,則其組合必須惟一。 create table tb1( nid int not null auto_increment primary key, num int null ) #或 create table tb1( nid int not null, num int not null, primary key(nid,num) )
#外鍵,一個特殊的索引,只能是指定內容 creat table color( nid int not null primary key, name char(16) not null ) create table fruit( nid int not null primary key, smt char(32) null , color_id int not null, constraint fk_cc foreign key (color_id) references color(nid) )
二、刪除表
drop table 表名
三、清空表
delete from 表名
truncate table 表名
四、修改表
#添加列: 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;
五、基本數據類型
MySQL的數據類型大體分爲:數值、時間和字符串
http://www.runoob.com/mysql/mysql-data-types.html
一、增
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
二、刪
delete from 表
delete from 表 where id=1 and name='alex'
三、改
update 表 set name = 'alex' where id>1
四、查
select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
五、其餘
#一、條件 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 表) #二、通配符 select * from 表 where name like 'ale%' - ale開頭的全部(多個字符串) select * from 表 where name like 'ale_' - ale開頭的全部(一個字符) #三、限制 select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 從第4行開始的5行 select * from 表 limit 5 offset 4 - 從第4行開始的5行 #四、排序 select * from 表 order by 列 asc - 根據 「列」 從小到大排列 select * from 表 order by 列 desc - 根據 「列」 從大到小排列 select * from 表 order by 列1 desc,列2 asc - 根據 「列1」 從大到小排列,若是相同則按列2從小到大排序 #五、分組 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以前 #六、連表 無對應關係則不顯示 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 #七、組合 組合,自動處理重合 select nickname from A union select name from B 組合,不處理重合 select nickname from A union all select name from B