banner
venom

venom

Mysql基礎

連接資料庫#

mysql -u user -p
//例如:mysql -u root -p

常見錯誤:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2),這意味著 MySQL 伺服器守護進程(Unix)或服務(Windows)未運行。

退出連接#

QUIT 或者 Ctry + D

查看資料庫,創建資料庫,使用資料庫查看資料庫#

show databases;

默認資料庫:

mysql - 用戶權限相關數據
test - 用於用戶測試數據
Information_schema - MySQL本身架構相關數據

創建資料庫:

create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; #utf8編碼
create database db1 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; #gbk編碼

使用資料庫:

use db1;

顯示當前使用的資料庫中所有表:

SHOW TABLES;

用戶管理#

創建用戶:

create user '用戶名'@'IP地址' identified by '密碼';

刪除用戶:

drop user '用戶名'@'IP地址';

修改用戶:

rename user '用戶名'@'IP地址';
to  '新用戶名'@'IP地址';

修改密碼:

set password for '用戶名'@'IP地址' = Password('新密碼');

注:用戶權限相關數據保存在 mysql 資料庫的 user 表中,所以也可以直接對其進行操作(不建議)

權限管理#

mysql 對於權限這塊有以下限制:

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地址 //用戶只能在該IP下才能訪問
用戶名@192.168.1.% //用戶只能在該IP段下才能訪問(通配符%表示任意)
用戶名@% //用戶可以在任意IP下訪問(默認IP地址為%)

查看權限:

show grants for '用戶'@'IP地址'

授權:

grant 權限 on 資料庫.表 to '用戶'@'IP地址'

取消授權:

revoke 權限 on 資料庫.表 from '用戶名'@'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'

MySQL 表操作#

查看表:

show tables; //查看資料庫全部表
select * from 表名; //查看表所有內容

創建表:

create table 表名(
    列名  類型  是否可以為空,
    列名  類型  是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8

舉例說明:

CREATE TABLE 'tab1'(
    'nid' int(11) NOT NULL auto_increment,
    'name' varchar(255) DEFAULT zhangyanlin,
    'email' varchar(255),
    PRIMARY KEY('nid')
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

注:

  • 默認值,創建列時可以指定默認值,當插入數據時如果未主動設置,則自動添加默認值
  • 自增,如果某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列)注意:1. 對於自增列,必須是索引(含主鍵)2. 對於自增可以設置步長和起始值
  • 主鍵,一種特殊的唯一索引,不允許有重複,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。

刪除表:

drop table 表名

清空表內容:

delete from 表名
trucate 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 的數據類型大致分為:數值、時間和字符串

bit[(M)]
二進制位(101001),m表示二進制位的長度(1-64),默認m=1

tinyint[(m)] [unsigned] [zerofill]
小整數,數據類型用於保存一些範圍的整數數值範圍
有符號:-128 ~ 127
無符號:0 ~255
特別的:MySQL中無布爾值,使用tinyint(1)構造。

int[(m)] [unsigned] [zerofill]
整數,數據類型用於保存一些範圍的整數數值範圍
有符號:-2147483648 ~ 2147483647
無符號:0 ~ 4294967295
特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select時顯示為:00002

bigint[(m)] [unsigned] [zerofill]
大整數,數據類型用於保存一些範圍的整數數值範圍
有符號:-9223372036854775808 ~ 9223372036854775807
無符號:0 ~ 18446744073709551615

decimal[(m[,d])] [unsigned] [zerofill]
準確的小數值,m是數字總個數(負號不算),d是小數點後個數。m最大值為65,d最大值為30。
特別的:對於精確數值計算時需要用此類型
decaimal能存儲精確值的原因在於其內部按照字符串存儲。

Float[(M,D)] [UNSIGNED] [ZEROFILL]
單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數
無符號:-3.402823466E+38 to -1.175494351E-38 , 0
有符號:0 , 1.175494351E-38 to 3.402823466E+38
//數值越大,越不準確

DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數
無符號:-1.7976931348623157E+308 to -2.2250738585072014E-308 , 0 ,2.2250738585072014E-308 to 1.7976931348623157E+308
有符號:0 , 2.2250738585072014E-308 to 1.7976931348623157E+308
//數值越大,越不準確

char(m)
char數據類型用於表示固定長度的字符串,可以包含最多達255個字符。其中m代表字符串的長度
PS: 即使數據小於m長度,也會占用m長度

varchar(m)
varchars數據類型用於變長的字符串,可以包含最多達255個字符。其中m代表該數據類型所允許保存的字符串的最大長度,只要長度小於該最大值的字符串都可以被保存在該數據類型中
注:雖然varchar使用起來較為靈活,但是從整個系統的性能角度來說,char數據類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設計資料庫時應當綜合考慮各方面的因素,以求達到最佳的平衡

text
text數據類型用於保存變長的大字符串,可以組多到65535 (2**16 − 1)個字符

mediutext
A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters

longtext
A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters

enum
枚舉類型
An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
示例:
CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', x-large)
);
INSERT INTO shirts(name, size) VALUES('dress shirt', 'large'),('t-shirt','medium'),('polo shirt','small');

set
集合類型
A SET can have a maximum of 64 distinct members
示例:
CREATE TABLE myset(col SET('a', 'b','c', 'd'));
INSERT INTO myset(col)VALUES('a,d'),('d,a'),("a,d,a"),('a,d,d'), ('d,a,d');

DATE
YYYY-MM-DD(1000-01-01/9999-12-31)

TIME
HH:MM:SS('-838:59:59'/'838:59:59')

YEAR
YYYY(1901/2155)

DATETIME
YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)

TMESTAMP
YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)

MySQL 表內容操作#

insert into 表(列名,列名...) values(值,值...)
insert into 表(列名,列名...) values(值,值...),(值,值,值...)
insert into 表(列名,列名...) select(列名,列名...) from 表
示例:
insert into tab1(name,email) values('zhangsan','[email protected]')

delete from 表 //刪除表裡全部內容
delete from 表 where id=1 and name='zhangsan' //刪除ID =1 和name='zhangsan' 那一行數據

update 表 set name='zhangsan' where id>1

select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
  • 條件判斷 where
select * from 表 where id > 1 and name != 'zhangsan' 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 表)
  • 通配符 like
select * from 表 where name like 'zhang%'    //zhang開頭的所有(多個字符串)
select * from 表 where name like 'zhang_'    //zhang開頭的所有(一個字符)
  • 限制 limit
select * from 表 limit 5;   //前5行
select * from 表 limit 4,5  //從第4行到第5行
select * from 表 limit 5 offset 4   //從第4行到第5行
  • 排序 asc,desc
select * from 表 order by 列 asc    //根據“列”從小到大排列
select * from 表 order by 列 desc   //根據“列”從大到小排列
select * from 表 order by 列1 desc,列2 asc    //根據“列1”從大到小排列,如果相同則按列2從小到大排列
  • 分組 group by
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 之前
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。