mysql簡介
1、什么是資料庫 ?
資料庫(Database)是按照資料結構來組織、存盤和管理資料的倉庫,它產生于距今六十多年前,隨著資訊技術和市場的發展,特別是二十世紀九十年代以后,資料管理不再僅僅是存盤和管理資料,而轉變成用戶所需要的各種資料管理的方式,資料庫有很多種型別,從最簡單的存盤有各種資料的表格到能夠進行海量資料存盤的大型資料庫系統都在各個方面得到了廣泛的應用,
主流的資料庫有:sqlserver,mysql,Oracle、SQLite、Access、MS SQL Server等,本文主要講述的是mysql
2、資料庫管理是干什么用的?
a. 將資料保存到檔案或記憶體
b. 接收特定的命令,然后對檔案進行相應的操作
PS:如果有了以上管理系統,無須自己再去創建檔案和檔案夾,而是直接傳遞 命令 給上述軟體,讓其來進行檔案操作,他們統稱為資料庫管理系統(DBMS,Database Management System)
mysql安裝
MySQL是一種開放源代碼的關系型資料庫管理系統(RDBMS),MySQL資料庫系統使用最常用的資料庫管理語言--結構化查詢語言(SQL)進行資料庫管理,在 WEB 應用方面MySQL是最好的 RDBMS (Relational Database Management System,關系資料庫管理系統) 應用軟體之一,
使用mysql必須具備一下條件
a. 安裝MySQL服務端
b. 安裝MySQL客戶端
b. 【客戶端】連接【服務端】
c. 【客戶端】發送命令給【服務端MySQL】服務的接受命令并執行相應操作(增刪改查等)
1、下載地址:http://dev.mysql.com/downloads/mysql/
2、安裝
windows安裝請參考:http://www.cnblogs.com/lonelywolfmoutain/p/4547115.html
linux下安裝:http://www.cnblogs.com/chenjunbiao/archive/2011/01/24/1940256.html
注:以上兩個鏈接有完整的安裝方式,安裝完以后mysql.server start啟動mysql服務
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), it means that the MySQL server daemon (Unix) or service (Windows) is not running.
退出連接:
QUIT 或者 Ctrl+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對于權限這塊有以下限制:
View Code
對于資料庫及內部其他權限如下:
資料庫名.* 資料庫中的所有
資料庫名.表 指定資料庫中的某張表
資料庫名.存盤程序 指定資料庫中的存盤程序
*.* 所有資料庫
對于用戶和IP的權限如下:
用戶名@IP地址 用戶只能在改IP下才能訪問
用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(通配符%表示任意)
用戶名@% 用戶可以再任意IP下訪問(默認IP地址為%)
1、查看權限:
show grants for '用戶'@'IP地址'
2、授權
grant 權限 on 資料庫.表 to '用戶'@'IP地址'
3、取消授權
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表操作
1、查看表
show tables; # 查看資料庫全部表
select * from 表名; # 查看表所有內容
2、創建表
create table 表名(
列名 型別 是否可以為空,
列名 型別 是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
來一個實體好詳解
復制代碼
CREATE TABLE tab1 (
nid int(11) NOT NULL auto_increment, # not null表示不能為空,auto_increment表示自增
name varchar(255) DEFAULT zhangyanlin, # default 表示默認值
email varchar(255),
PRIMARY KEY (nid) # 把nid列設定成主鍵
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
復制代碼
注:
默認值,創建列時可以指定默認值,當插入資料時如果未主動設定,則自動添加默認值
自增,如果為某列設定自增列,插入資料時無需設定此列,默認將自增(表中只能有一個自增列)注意:1、對于自增列,必須是索引(含主鍵)2、對于自增可以設定步長和起始值
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一,
3、洗掉表
drop table 表名
3、清空表內容
delete from 表名
truncate table 表名
4、修改表
復制代碼
添加列: 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;
復制代碼
對于上述這些操作是不是看起來很麻煩,很浪費時間,別慌!有專門的軟體能提供這些功能,操作起來非常簡單,這個軟體名字叫Navicat Premium ,大家自行在網上下載,練練手,但是下面的即將講到表內容操作還是建議自己寫命令來進行
5、基本資料型別
MySQL的資料型別大致分為:數值、時間和字串
View Code
mysql表內容操作
表內容操作無非就是增刪改查,當然用的最多的還是查,而且查這一塊東西最多,用起來最難,當然對于大神來說那就是so easy了,對于我這種小白還是非常難以靈活運用的,下面咱來一一操作一下
1、增
insert into 表 (列名,列名...) values (值,值,...)
insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
例:
insert into tab1(name,email) values('zhangyanlin','[email protected]')
2、刪
delete from 表 # 洗掉表里全部資料
delete from 表 where id=1 and name='zhangyanlin' # 洗掉ID =1 和name='zhangyanlin' 那一行資料
3、改
update 表 set name = 'zhangyanlin' where id>1
4、查
select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
查這塊的條件太多太多我給列舉出來至于組合還得看大家的理解程度哈
a、條件判斷where
select * from 表 where id > 1 and name != 'aylin' 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 表)
b、通配符like
select * from 表 where name like 'zhang%' # zhang開頭的所有(多個字串)
select * from 表 where name like 'zhang_' # zhang開頭的所有(一個字符)
c、限制limit
select * from 表 limit 5; - 前5行
select * from 表 limit 4,5; - 從第4行開始的5行
select * from 表 limit 5 offset 4 - 從第4行開始的5行
d、排序asc,desc
select * from 表 order by 列 asc - 根據 “列” 從小到大排列
select * from 表 order by 列 desc - 根據 “列” 從大到小排列
select * from 表 order by 列1 desc,列2 asc - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序
e、分組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之前
來自:https://www.cnblogs.com/aylin/p/5744312.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/209280.html
標籤:其他
上一篇:【11月9日】Redis 持久化
