MySQL
是一種快速易用的關係型數據庫管理系統(RDBMS)
,不少企業都在使用它來構建本身的數據庫。python
MySQL由一家瑞典公司MySQL AB
開發、運營並予以支持。它之因此很是流行,緣由在於具有如下這些優勢:mysql
RDBMS術語程序員
術語 | 描述 |
---|---|
數據庫(Database) |
數據庫是帶有相關數據的表的集合 |
表(Table) |
表是帶有數據的矩陣。數據庫中的表就像一種簡單的電子表格 |
列(Column) |
每一列(數據元素)都包含着同種類型的數據,好比郵編 |
行(Row) |
行(又被稱爲元組、項或記錄)是一組相關數據,好比有關訂閱量的數據 |
冗餘(Redundancy) |
存儲兩次數據,以便使系統更快速 |
主鍵(Primary Key) |
主鍵是惟一的。同一張表中不容許出現一樣兩個鍵值。一個鍵值只對應着一行 |
外鍵(Foreign Key) |
用於鏈接兩張表 |
複合鍵(Compound Key) |
複合鍵(又稱組合鍵)是一種由多列組成的鍵,由於一列並不足以肯定惟一性 |
索引(Index) |
它在數據庫中的做用就像書後的索引同樣 |
引用完性(Referential Integrity) |
用來確保外鍵一直指向已存在的一行 |
鏈接已經啓動的Mysqlsql
mac@:~$ mysql -uroot -p9998877
數據庫
查看當前的全部數據庫bash
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
默認數據庫 | 描述 |
---|---|
information_schema |
提供了訪問數據庫元數據的方式,元數據如數據庫名或表名,列的數據類型或訪問權限等 |
test |
用戶用來測試的數據庫庫 |
mysql |
用戶權限相關數據 |
performance_schema |
用於收集數據庫服務器性能參數 |
sys |
包含了一系列視圖、函數和存儲過程 |
建立數據庫服務器
-- 建立字符串爲utf-8的數據庫 CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- 建立字符串爲gbk的數據庫 CREATE DATABASE dbname DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
進入數據庫session
use dbname;
刪除數據庫ide
drop database dbname;
用戶相關函數
建立kongling用戶,容許全部主機鏈接,密碼設置爲as
create user 'kongling'@'%' identified by 'as';
修改kongling
用戶的用戶名爲as
rename user 'kongling'@'%' to 'konglingme'@'192.168.56.1';
修改anshengme
用戶的密碼爲123
set password for 'kongling'@'192.168.56.1' = Password('123');
刪除anshengme
用戶
drop user 'konglingme'@'192.168.56.1';
用戶權限相關數據保存在mysql數據庫的user表中,因此也能夠直接對其進行操做(不建議)
用戶受權
-- 查看權限 show grants for 'root'@'localhost'; -- 受權 grant 權限 on 數據庫.表 to '用戶'@'IP地址'; -- 取消權限 revoke 權限 on 數據庫.表 from '用戶'@'IP地址';
建立表
create table 表名( 列名 類型 是否能夠爲空, 列名 類型 是否能夠爲空 )ENGINE=InnoDB DEFAULT CHARSET=utf8
是否能夠爲空
create table tb_name( `username_not` varchar(30) NOT NULL , -- 不可空 `username_null` varchar(30) NULL -- 可空 )ENGINE=InnoDB DEFAULT CHARSET=utf8
默認值,建立列時能夠指定默認值,當插入數據時若是未主動設置,則自動添加默認值
create table tb_name( nid int not null defalut 2, num int not null )ENGINE=InnoDB DEFAULT CHARSET=utf8
自增,若是爲某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列)
create table tb_name( nid int not null auto_increment primary key, num int null )ENGINE=InnoDB DEFAULT CHARSET=utf8
或
create table tb_name( nid int not null auto_increment, num int null, index(nid) )ENGINE=InnoDB DEFAULT CHARSET=utf8
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) )
外鍵,一個特殊的索引,只能是指定內容
create 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 tb_name;
清空表
-- 若是清空的表又自增列,那麼在清空以後會繼續上次自增的值繼續自增 delete from tb_name; -- 若是清空的表又自增列,那麼在清空以後再次添加數據自增的值會重新開始計算 truncate table tb_name;
修改表
-- 添加列 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;