1 show databases; #查看當前MySQL中的全部數據庫 2 create 數據庫名; #建立新的數據庫 3 use 數據庫名; #使用該數據庫 4 show tables; #查看當前數據庫中的全部表 5 create table 表名(nid int,name varchar(20),passwd varchar(20));#建立表 6 desc 表名 #查看錶的詳細信息
1 #1:用戶管理命令 2 建立用戶: 3 create user '用戶名'@'ip地址' identified by '密碼'; 4 刪除用戶: 5 drop user '用戶名'@'ip地址'; 6 修改用戶: 7 rename user '用戶名'@'ip地址' to '新用戶名'@'ip地址'; 8 修改密碼: 9 set password for '用戶名'@'ip地址' = Password("新密碼"); 10 #2:用戶權限管理 11 show grants for '用戶'@'IP地址' -- 查看權限 12 grant 權限 on 數據庫.表 to '用戶'@'IP地址' -- 受權 13 revoke 權限 on 數據庫.表 from '用戶'@'IP地址' -- 取消權限
1 all privileges 除grant外的全部權限 2 select 僅查權限 3 select,insert 查和插入權限 4 ... 5 usage 無訪問權限 6 alter 使用alter table 7 alter routine 使用alter procedure和drop procedure 8 create 使用create table 9 create routine 使用create procedure 10 create temporary tables 使用create temporary tables 11 create user 使用create user、drop user、rename user和revoke all privileges 12 create view 使用create view 13 delete 使用delete 14 drop 使用drop table 15 execute 使用call和存儲過程 16 file 使用select into outfile 和 load data infile 17 grant option 使用grant 和 revoke 18 index 使用index 19 insert 使用insert 20 lock tables 使用lock table 21 process 使用show full processlist 22 select 使用select 23 show databases 使用show databases 24 show view 使用show view 25 update 使用update 26 reload 使用flush 27 shutdown 使用mysqladmin shutdown(關閉MySQL) 28 super 使用change master、kill、logs、purge、master和set global。還容許mysqladmin調試登錄 29 replication client 服務器位置的訪問 30 replication slave 由複製從屬使用
1 數據庫名.* #數據庫中的全部 2 數據庫名.表 #指定數據庫中的某張表 3 數據庫名.存儲過程 #指定數據庫中的存儲過程 4 *.* #全部數據庫
1 grant all privileges on db1.tb1 TO '用戶名'@'IP' 2 3 grant select on db1.* TO '用戶名'@'IP' 4 5 grant select,insert on *.* TO '用戶名'@'IP' 6 7 revoke select on db1.tb1 from '用戶名'@'IP'
1 # 啓動免受權服務端 2 mysqld --skip-grant-tables 3 4 # 客戶端 5 mysql -u root -p 6 7 # 修改用戶名密碼 8 update mysql.user set authentication_string=password('666') where user='root'; 9 flush privileges;
1 注:flush privileges,將數據讀取到內存中,從而當即生效。
1:建立表mysql
1 create table 表名( 2 列名 類型 是否能夠爲空, 3 列名 類型 是否能夠爲空 4 )ENGINE=InnoDB DEFAULT CHARSET=utf8
1 1:是否可空 2 null表示空,非字符串 3 not null - 不可空 4 null - 可空 5 2: 默認值 6 建立列時能夠指定默認值,當插入數據時若是未主動設置,則自動添加默認值 7 create table tb1( 8 nid int not null defalut 2, 9 num int not null 10 )
1 自增,若是爲某列設置自增列,插入數據時無需設置此列,默認將自增(表中只能有一個自增列) 2 create table tb1( 3 nid int not null auto_increment primary key, 4 num int null 5 ) 6 或 7 create table tb1( 8 nid int not null auto_increment, 9 num int null, 10 index(nid) 11 ) 12 注意:1、對於自增列,必須是索引(含主鍵)。 13 2、對於自增能夠設置步長和起始值 14 show session variables like 'auto_inc%'; 15 set session auto_increment_increment=2; 16 set session auto_increment_offset=10; 17 18 shwo global variables like 'auto_inc%'; 19 set global auto_increment_increment=2; 20 set global auto_increment_offset=10;
1 主鍵,一種特殊的惟一索引,不容許有空值,若是主鍵使用單個列,則它的值必須惟一,若是是多列,則其組合必須惟一。 2 create table tb1( 3 nid int not null auto_increment primary key, 4 num int null 5 ) 6 或 7 create table tb1( 8 nid int not null, 9 num int not null, 10 primary key(nid,num) 11 )
1 外鍵,一個特殊的索引,只能是指定內容 2 creat table color( 3 nid int not null primary key, 4 name char(16) not null 5 ) 6 7 create table fruit( 8 nid int not null primary key, 9 smt char(32) null , 10 color_id int not null, 11 constraint fk_cc foreign key (color_id) references color(nid) 12 )
2:刪除表sql
1 drop table 表名
3:清空表數據庫
1 delete from 表名 2 truncate table 表名
4:修改表服務器
1 添加列:alter table 表名 add 列名 類型 2 刪除列:alter table 表名 drop column 列名 3 修改列: 4 alter table 表名 modify column 列名 類型; -- 類型 5 alter table 表名 change 原列名 新列名 類型; -- 列名,類型 6 7 添加主鍵: 8 alter table 表名 add primary key(列名); 9 刪除主鍵: 10 alter table 表名 drop primary key; 11 alter table 表名 modify 列名 int, drop primary key; 12 13 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段); 14 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱 15 16 修改默認值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; 17 刪除默認值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
5:基本數據類型session
mysql數據類型大體分爲:數字,字符串,時間less
1 bit[(M)] 2 二進制位(101001),m表示二進制位的長度(1-64),默認m=1 3 4 tinyint[(m)] [unsigned] [zerofill] 5 6 小整數,數據類型用於保存一些範圍的整數數值範圍: 7 有符號: 8 -128 ~ 127. 9 無符號: 10 ~ 255 11 12 特別的: MySQL中無布爾值,使用tinyint(1)構造。 13 14 int[(m)][unsigned][zerofill] 15 16 整數,數據類型用於保存一些範圍的整數數值範圍: 17 有符號: 18 -2147483648 ~ 2147483647 19 無符號: 20 ~ 4294967295 21 22 特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select 時數據顯示爲: 00002 23 24 bigint[(m)][unsigned][zerofill] 25 大整數,數據類型用於保存一些範圍的整數數值範圍: 26 有符號: 27 -9223372036854775808 ~ 9223372036854775807 28 無符號: 29 ~ 18446744073709551615 30 31 decimal[(m[,d])] [unsigned] [zerofill] 32 準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值爲65,d最大值爲30。 33 34 特別的:對於精確數值計算時須要用此類型 35 decaimal可以存儲精確值的緣由在於其內部按照字符串存儲。 36 37 FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] 38 單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 39 無符號: 40 -3.402823466E+38 to -1.175494351E-38, 41 1.175494351E-38 to 3.402823466E+38 42 有符號: 43 1.175494351E-38 to 3.402823466E+38 44 45 **** 數值越大,越不許確 **** 46 47 DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 48 雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 49 50 無符號: 51 -1.7976931348623157E+308 to -2.2250738585072014E-308 52 2.2250738585072014E-308 to 1.7976931348623157E+308 53 有符號: 54 2.2250738585072014E-308 to 1.7976931348623157E+308 55 **** 數值越大,越不許確 **** 56 57 58 char (m) 59 char數據類型用於表示固定長度的字符串,能夠包含最多達255個字符。其中m表明字符串的長度。 60 PS: 即便數據小於m長度,也會佔用m長度 61 varchar(m) 62 varchars數據類型用於變長的字符串,能夠包含最多達255個字符。其中m表明該數據類型所容許保存的字符串的最大長度,只要長度小於該最大值的字符串均可以被保存在該數據類型中。 63 64 注:雖然varchar使用起來較爲靈活,可是從整個系統的性能角度來講,char數據類型的處理速度更快,有時甚至能夠超出varchar處理速度的50%。所以,用戶在設計數據庫時應當綜合考慮各方面的因素,以求達到最佳的平衡 65 66 text 67 text數據類型用於保存變長的大字符串,能夠組多到65535 (2**16 − 1)個字符。 68 69 mediumtext 70 A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. 71 72 longtext 73 A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters. 74 75 76 enum 77 枚舉類型, 78 An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) 79 示例: 80 CREATE TABLE shirts ( 81 name VARCHAR(40), 82 size ENUM('x-small', 'small', 'medium', 'large', 'x-large') 83 ); 84 INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small'); 85 86 set 87 集合類型 88 A SET column can have a maximum of 64 distinct members. 89 示例: 90 CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); 91 INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d'); 92 93 DATE 94 YYYY-MM-DD(1000-01-01/9999-12-31) 95 96 TIME 97 HH:MM:SS('-838:59:59'/'838:59:59') 98 99 YEAR 100 YYYY(1901/2155) 101 102 DATETIME 103 104 YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) 105 106 TIMESTAMP 107 108 YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
1:增長ide
1 insert into 表 (列名,列名...) values (值,值,值...) 2 insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) 3 insert into 表 (列名,列名...) select (列名,列名...) from 表
2:刪除性能
1 delete from 表 2 delete from 表 where id=1 and name='kelvin'
3:修改學習
1 update 表 set name = 'kelvin' where id>1
4:查詢ui
1 select * from 表 2 select * from 表 where id > 1 3 select nid,name,gender as new_gender from 表 where id > 1
5:其餘
1 a、條件 2 select * from 表 where id > 1 and name != 'kelvin' and num = 12; 3 4 select * from 表 where id between 5 and 16; 5 6 select * from 表 where id in (11,22,33) 7 select * from 表 where id not in (11,22,33) 8 select * from 表 where id in (select nid from 表) 9 10 b、通配符 11 select * from 表 where name like 'kelv%' - kelv開頭的全部(多個字符串) 12 select * from 表 where name like 'kelvi_' - kelvi開頭的全部(一個字符) 13 14 c、限制 15 select * from 表 limit 5; - 前5行 16 select * from 表 limit 4,5; - 從第4行開始的5行 17 select * from 表 limit 5 offset 4 - 從第4行開始的5行 18 19 d、排序 20 select * from 表 order by 列 asc - 根據 「列」 從小到大排列 21 select * from 表 order by 列 desc - 根據 「列」 從大到小排列 22 select * from 表 order by 列1 desc,列2 asc - 根據 「列1」 從大到小排列,若是相同則按列2從小到大排序 23 24 e、分組 25 select num from 表 group by num 26 select num,nid from 表 group by num,nid 27 select num,nid from 表 where nid > 10 group by num,nid order nid desc 28 select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid 29 30 select num from 表 group by num having max(id) > 10 31 32 特別的:group by 必須在where以後,order by以前 33 34 f、連表 35 無對應關係則不顯示 36 select A.num, A.name, B.name 37 from A,B 38 Where A.nid = B.nid 39 40 無對應關係則不顯示 41 select A.num, A.name, B.name 42 from A inner join B 43 on A.nid = B.nid 44 45 A表全部顯示,若是B中無對應關係,則值爲null 46 select A.num, A.name, B.name 47 from A left join B 48 on A.nid = B.nid 49 50 B表全部顯示,若是B中無對應關係,則值爲null 51 select A.num, A.name, B.name 52 from A right join B 53 on A.nid = B.nid 54 55 g、組合 56 組合,自動處理重合 57 select nickname 58 from A 59 union 60 select name 61 from B 62 63 組合,不處理重合 64 select nickname 65 from A 66 union all 67 select name 68 from B