一、數據操作 1.顯示資料庫 2.創建資料庫 3.使用資料庫 4.用戶管理 特殊情況:忘記密碼 二、數據表基本操作 1.創建表 2.刪除表 3.清空表 4.修改表 5.基本數據類型 MySQL的數據類型大致分為:數值、時間和字元串 1 bit[(M)] 2 二進位位(101001),m表示二進位位的 ...
一、數據操作
1.顯示資料庫
1 show databases;
2.創建資料庫
1 #utf-8 2 create database 資料庫名稱 default charset utf8 collate utf8_general_ci;
3 #gbk
4 create datahase 資料庫名稱 default charset utf8 gbk collate gbk_chinese_ci;
3.使用資料庫
1 use 資料庫名稱
4.用戶管理
1 1.創建用戶 2 create user '用戶名'@'IP地址' identified by ‘密碼’; 3 2.刪除用戶 4 drop user '用戶名'@ 'IP地址; 5 3.修改用戶 6 rename user '用戶名'@ 'IP地址'; to '新用戶'@ 'IP地址; 7 4.許可權管理 8 查看許可權:show grant for ‘用戶’@’IP地址’; 9 授權:grant 許可權 on 資料庫.表 to ‘用戶’@’IP地址’; 10 取消授權:revoke 許可權 on 資料庫.表. from ‘用戶’@’IP地址’; 11 出grant外所有的許可權:all privileges 12 插入許可權:select,insert
特殊情況:忘記密碼
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.創建表
1 create table 表名( 2 列名 類型 是否可以為空 auto_increment primary key, 3 列名 類型 是否可以為空 4 )engine=ioondb, default chartset=utf8; 5 或者 6 create table 表名( 7 列名 類型 是否可以為空 , 8 列名 類型 是否可以為空, 9 primary key(列名,列名) 10 )engine=ioondb, default chartset=utf8;
2.刪除表
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.基本數據類型
MySQL的數據類型大致分為:數值、時間和字元串

1 bit[(M)] 2 二進位位(101001),m表示二進位位的長度(1-64),預設m=1 3 4 tinyint[(m)] [unsigned] [zerofill] 5 6 小整數,數據類型用於保存一些範圍的整數數值範圍: 7 有符號: 8 -128 ~ 127. 9 無符號: 10 0 ~ 255 11 12 特別的: MySQL中無布爾值,使用tinyint(1)構造。 13 14 int[(m)][unsigned][zerofill] 15 16 整數,數據類型用於保存一些範圍的整數數值範圍: 17 有符號: 18 -2147483648 ~ 2147483647 19 無符號: 20 0 ~ 4294967295 21 22 特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select 時數據顯示為: 00002 23 24 bigint[(m)][unsigned][zerofill] 25 大整數,數據類型用於保存一些範圍的整數數值範圍: 26 有符號: 27 -9223372036854775808 ~ 9223372036854775807 28 無符號: 29 0 ~ 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 0 42 1.175494351E-38 to 3.402823466E+38 43 有符號: 44 0 45 1.175494351E-38 to 3.402823466E+38 46 47 **** 數值越大,越不准確 **** 48 49 DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 50 雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 51 52 無符號: 53 -1.7976931348623157E+308 to -2.2250738585072014E-308 54 0 55 2.2250738585072014E-308 to 1.7976931348623157E+308 56 有符號: 57 0 58 2.2250738585072014E-308 to 1.7976931348623157E+308 59 **** 數值越大,越不准確 **** 60 61 62 char (m) 63 char數據類型用於表示固定長度的字元串,可以包含最多達255個字元。其中m代表字元串的長度。 64 PS: 即使數據小於m長度,也會占用m長度 65 varchar(m) 66 varchars數據類型用於變長的字元串,可以包含最多達255個字元。其中m代表該數據類型所允許保存的字元串的最大長度,只要長度小於該最大值的字元串都可以被保存在該數據類型中。 67 68 註:雖然varchar使用起來較為靈活,但是從整個系統的性能角度來說,char數據類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設計資料庫時應當綜合考慮各方面的因素,以求達到最佳的平衡 69 70 text 71 text數據類型用於保存變長的大字元串,可以組多到65535 (2**16 − 1)個字元。 72 73 mediumtext 74 A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. 75 76 longtext 77 A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters. 78 79 80 enum 81 枚舉類型, 82 An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) 83 示例: 84 CREATE TABLE shirts ( 85 name VARCHAR(40), 86 size ENUM('x-small', 'small', 'medium', 'large', 'x-large') 87 ); 88 INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small'); 89 90 set 91 集合類型 92 A SET column can have a maximum of 64 distinct members. 93 示例: 94 CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); 95 INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d'); 96 97 DATE 98 YYYY-MM-DD(1000-01-01/9999-12-31) 99 100 TIME 101 HH:MM:SS('-838:59:59'/'838:59:59') 102 103 YEAR 104 YYYY(1901/2155) 105 106 DATETIME 107 108 YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) 109 110 TIMESTAMP 111 112 YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)View Code
三、表內容操作
1.增
1 insert into 表名 (列名,列名...) values (值,值,值...); 2 insert into 表名 (列名,列名...) values (值,值) ,(值,值)...;
2.刪
1 delete from 表; 2 delete from 表 where id =1 and name ='yan';
3.改
1 update 表 set name = 'yan' where id>1 or name = 'xxx';
4.查
1 select * from 表; 2 select id,name from 表; 3 select id,name from 表 where id>10 or name = 'yan'; 4 select id,name as cname from 表 where id>10 or name = 'yan';
其他語句:

1 條件: 2 select * from 表where id >1 and name != ‘yan’ and num=12; 3 select * from 表where id in (1,5,12); 4 select * from 表where id not in (1,5,12); 5 select * from 表where id in (select id from db1); 6 select * from 表where id between 5 and 110; 7 8 通配符 9 select * from 表名 where name like 'ya% ; -ya開頭的所有(多個字元) 10 select * from 表名 where name like 'ya_'; -ya開頭的所有(一個字元) 11 限制 12 select * from 表 limit5; -前5行 13 select * from 表 limit3,5; -從三行到第5行 14 select * from 表 limit3 offset 4; -從第4行到第5行 15 排序 16 select * from 表 order by 列 asc; -根據“列”從小到大排列 17 select * from 表 order by 列 besc; -根據“列”從大到小排列 18 select * from 表 order by id desc limit 10; -先排序再找 19 select * from 表 order by 列表1 desc,列表2 asc; - 根據 “列1” 從大到小排列,如 果相同則按列2從小到大排序View Code
1 連表 2 select * from 表1 left join 表2 on 表1.id = 表2.name;
5.查看表結構
1 desc 表名;
6.查看表創建語句
1 show create table 表名\G;
7.自增步長
1 1.基於會話級別 2 show session variaoles like 'auto_inc%'; 查看全局變數 3 set session auto_increment_increment=2; 設置會話步長 4 2.基於全局級別: 5 show global variables like 'auto_inc%'; 查看全局變數 6 set global auto_increment_increment=2; 設置會話步長