MySQL知識點整理
阿新 • • 發佈:2017-06-05
啟動服務 db2 sign slist mem lte 外鍵 open 用戶名
一、MySQL安裝
1.Windows:
運行可執行文件
壓縮包
放置任意目錄
2.初始化:
服務端:E:mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld --initialize-insecure
#此時會獲得一個 用戶名 root 密碼:空
啟動服務端:
E:mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysqld
客戶端連接:
E:mysql-5.7.16-winx64\mysql-5.7.16-winx64\bin\mysqld\mysql -u root -p
3.將MySQL服務制作成windows服務
在執行【mysqd】啟動MySQL服務器時,當前終端會被hang住,那麽做一下設置即可解決此問題:
1 # 制作MySQL的Windows服務,在終端執行此命令: 2 "c:\mysql-5.7.16-winx64\bin\mysqld" --install 3 4 # 移除MySQL的Windows服務,在終端執行此命令: 5 "c:\mysql-5.7.16-winx64\bin\mysqld" --remove
註冊成服務之後,以後再啟動和關閉MySQL服務時,僅需執行如下命令:
1 # 啟動MySQL服務 2 net start mysql3 4 # 關閉MySQL服務 5 net stop mysql
二、數據庫操作
1.基本操作
1 show databases;#顯示數據庫 2 3 use 數據庫名稱;#進入當前數據庫目錄下 4 5 show tables;#顯示當前使用的數據庫中所有表 6 select * from 表名;#讀取文件內容 7 8 select name,age,id from 表名;#按name,age,id順序讀取文件內容 9 10 use mysql;#回到數據庫起始目錄 11 12 select user,host from user;#顯示數據庫用戶信息
2.用戶管理
1 創建用戶 2 create user ‘用戶名‘@‘IP地址‘ identified by ‘密碼‘; 3 刪除用戶 4 drop user ‘用戶名‘@‘IP地址‘; 5 修改用戶 6 rename user ‘用戶名‘@‘IP地址‘; to ‘新用戶名‘@‘IP地址‘;; 7 修改密碼 8 set password for ‘用戶名‘@‘IP地址‘ = Password(‘新密碼‘)
[email protected] 用戶只能在改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‘註意事項和實例
3.授權管理
1 show grants for ‘用戶‘@‘IP地址‘ -- 查看權限 2 grant 權限 on 數據庫.表 to ‘用戶‘@‘IP地址‘ -- 授權 3 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 由復制從屬使用 對於權限權限內容
對於目標數據庫以及內部其他: 數據庫名.* 數據庫中的所有 數據庫名.表 指定數據庫中的某張表 數據庫名.存儲過程 指定數據庫中的存儲過程 *.* 所有數據庫其他
三、數據表操作
1.創建文件夾
1 create database db2;#創建 2 create database db2 default charset utf8;#涉及編碼問題,需要添加編碼方式 3 4 drop database db2;#刪除
2.創建表
1 create table t1( 2 列名 類型 null, 3 列名 類型 not null, 4 列名 類型 not null auto_increment primary key, 5 id int, 6 name char(10) 7 )engine=innodb default charset=utf8;
是否可空,null表示空,非字符串 not null - 不可空 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) ) 註意:1、對於自增列,必須是索引(含主鍵)。 2、對於自增可以設置步長和起始值 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; 自增auto_increment :自增
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。 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) ) 主鍵primary key:主鍵 表示 約束(不能重復且不能為空); 加速查找
外鍵,一個特殊的索引,只能是指定內容 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) ) ‘‘‘ create table userinfo( uid bigint auto_increment primary key, name varchar(32), department_id int, xx_id int, constraint fk_user_depar foreign key ("department_id",) references department(‘id‘), constraint fk_xx_ff foreign key ("xx_id",) references XX(‘id‘) )engine=innodb default charset=utf8; create table department( id bigint auto_increment primary key, title char(15) )engine=innodb default charset=utf8; ‘‘‘外鍵
2.刪除表
1 drop table 表名
3.清空表
1 delete from 表名 2 truncate table 表名
4.修改表中內容
1 插入數據: 2 insert into t1(id,name) values(1,‘alex‘); 3 刪除: 4 delete from t1 where id<6 5 修改: 6 update t1 set age=18; 7 update t1 set age=18 where age=17; 8 查看數據: 9 select * from t1;
5.基本數據類型
bit[(M)] 二進制位(101001),m表示二進制位的長度(1-64),默認m=1 tinyint[(m)] [unsigned] [zerofill] 小整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -128 ~ 127. 無符號: ~ 255 特別的: MySQL中無布爾值,使用tinyint(1)構造。 int[(m)][unsigned][zerofill] 整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -2147483648 ~ 2147483647 無符號: ~ 4294967295 特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select 時數據顯示為: 00002 bigint[(m)][unsigned][zerofill] 大整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -9223372036854775808 ~ 9223372036854775807 無符號: ~ 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, 1.175494351E-38 to 3.402823466E+38 有符號: 1.175494351E-38 to 3.402823466E+38 **** 數值越大,越不準確 **** DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 無符號: -1.7976931348623157E+308 to -2.2250738585072014E-308 2.2250738585072014E-308 to 1.7976931348623157E+308 有符號: 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)個字符。 mediumtext 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 column 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) TIMESTAMP YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)數據類型
MySQL知識點整理