MySQL基礎操作(一)

MySQL操作

一、創建數據庫

# utf-8
CREATE DATABASE 數據庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;# gbk
CREATE DATABASE 數據庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

二、用戶管理

創建用戶create user '用戶名'@'IP地址' identified by '密碼';
刪除用戶drop user '用戶名'@'IP地址';
修改用戶rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址';;
修改密碼set password for '用戶名'@'IP地址' = Password('新密碼')

用戶權限相關數據保存在mysql數據庫的user表中,所以也可以直接對其進行操作,但是不建議。

三、授權管理

show grants for '用戶'@'IP地址'                  -- 查看權限
grant  權限 on 數據庫.表 to   '用戶'@'IP地址'      -- 授權
revoke 權限 on 數據庫.表 from '用戶'@'IP地址'      -- 取消權限

權限列表

all privileges  除grant外的所有權限select          僅查權限select,insert   查和插入權限...usage                   無訪問權限alter                   使用alter tablealter routine           使用alter procedure和drop procedurecreate                  使用create tablecreate routine          使用create procedurecreate temporary tables 使用create temporary tablescreate user             使用create userdrop user、rename user和revoke  all privilegescreate view             使用create viewdelete                  使用deletedrop                    使用drop tableexecute                 使用call和存儲過程file                    使用select into outfile 和 load data infilegrant option            使用grant 和 revokeindex                   使用indexinsert                  使用insertlock tables             使用lock tableprocess                 使用show full processlistselect                  使用selectshow databases          使用show databasesshow view               使用show viewupdate                  使用updatereload                  使用flushshutdown                使用mysqladmin shutdown(關閉MySQL)super                   使用change master、kill、logs、purge、master和set global。還允許mysqladmin調試登陸replication client      服務器位置的訪問replication slave       由復制從屬使用
View Code

tips:忘記密碼

# 啟動免授權服務端
mysqld --skip-grant-tables

# 客戶端
mysql -u root -p# 修改用戶名密碼
update mysql.user set authentication_string=password('666') where user='root';
flush privileges;
忘記密碼

?四、數據表操作

1.創建表

create table 表名(列名  類型  是否可以為空,列名  類型  是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
是否可空,null表示空,非字符串not 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;主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。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))外鍵,一個特殊的索引,只能是指定內容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))
具體用法

2.刪除表

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;

5.數據類型

bit[(M)]二進制位(101001),m表示二進制位的長度(1-64),默認m=1tinyint[(m)] [unsigned] [zerofill]小整數,數據類型用于保存一些范圍的整數數值范圍:有符號:-128127.無符號:0255特別的: MySQL中無布爾值,使用tinyint(1)構造。int[(m)][unsigned][zerofill]整數,數據類型用于保存一些范圍的整數數值范圍:有符號:-21474836482147483647無符號:04294967295特別的:整數類型中的m僅用于顯示,對存儲范圍無限制。例如: int(5),當插入數據2時,select 時數據顯示為: 00002bigint[(m)][unsigned][zerofill]大整數,數據類型用于保存一些范圍的整數數值范圍:有符號:-92233720368547758089223372036854775807無符號:018446744073709551615decimal[(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,01.175494351E-38 to 3.402823466E+38有符號:01.175494351E-38 to 3.402823466E+38**** 數值越大,越不準確 ****DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]雙精度浮點數(非準確小數值),m是數字總個數,d是小數點后個數。無符號:-1.7976931348623157E+308 to -2.2250738585072014E-30802.2250738585072014E-308 to 1.7976931348623157E+308有符號:02.2250738585072014E-308 to 1.7976931348623157E+308**** 數值越大,越不準確 ****char (m)char數據類型用于表示固定長度的字符串,可以包含最多達255個字符。其中m代表字符串的長度。PS: 即使數據小于m長度,也會占用m長度varchar(m)varchars數據類型用于變長的字符串,可以包含最多達255個字符。其中m代表該數據類型所允許保存的字符串的最大長度,只要長度小于該最大值的字符串都可以被保存在該數據類型中。注:雖然varchar使用起來較為靈活,但是從整個系統的性能角度來說,char數據類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設計數據庫時應當綜合考慮各方面的因素,以求達到最佳的平衡texttext數據類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個字符。mediumtextA TEXT column with a maximum length of 16,777,215 (2**24 ? 1) characters.longtextA 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');DATEYYYY-MM-DD(1000-01-01/9999-12-31)TIMEHH:MM:SS('-838:59:59'/'838:59:59'YEARYYYY(1901/2155DATETIMEYYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)TIMESTAMPYYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
View Code

6.表內容基本操作

1、增
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from2、刪delete fromdelete fromwhere id=1 and name='alex'3、改
updateset name = 'alex' where id>14、查
select * fromselect * fromwhere id > 1
select nid,name,gender as gg fromwhere id > 1

7.進階操作

a、條件select * fromwhere id > 1 and name != 'alex' and num = 12;select * fromwhere id between 5 and 16;select * fromwhere id in (11,22,33)select * fromwhere id not in (11,22,33)select * fromwhere id in (select nid from 表)b、通配符select * fromwhere name like 'ale%'  - ale開頭的所有(多個字符串)select * fromwhere name like 'ale_'  - ale開頭的所有(一個字符)c、限制select * from 表 limit 5;            - 前5行select * from 表 limit 4,5;          - 從第4行開始的5行select * from 表 limit 5 offset 4    - 從第4行開始的5行d、排序select * fromorder byasc              - 根據 “列” 從小到大排列select * fromorder bydesc             - 根據 “列” 從大到小排列select * fromorder by 列1 desc,列2 asc    - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序e、分組select num fromgroup by numselect num,nid fromgroup by num,nidselect num,nid fromwhere nid > 10 group by num,nid order nid descselect num,nid,count(*),sum(score),max(score),min(score) fromgroup by num,nidselect num fromgroup by num having max(id) > 10特別的:group by 必須在where之后,order by之前f、連表無對應關系則不顯示select A.num, A.name, B.namefrom A,BWhere A.nid = B.nid無對應關系則不顯示select A.num, A.name, B.namefrom A inner join Bon A.nid = B.nidA表所有顯示,如果B中無對應關系,則值為nullselect A.num, A.name, B.namefrom A left join Bon A.nid = B.nidB表所有顯示,如果B中無對應關系,則值為nullselect A.num, A.name, B.namefrom A right join Bon A.nid = B.nidg、組合組合,自動處理重合select nicknamefrom Aunionselect namefrom B組合,不處理重合select nicknamefrom Aunion allselect namefrom B
View Code

?

附加:

連接時可能會拋出時區錯誤,在MySQL查詢上執行以下語句可以解決:

show variables like '%time_zone%';
set global time_zone='+8:00';
時區錯誤

?

轉載于:https://www.cnblogs.com/smilepup-hhr/p/10955843.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/393247.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393247.shtml
英文地址,請注明出處:http://en.pswp.cn/news/393247.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

集合框架05

一、HashSet集合 1 public class Demo01 {2 /*3 * Set接口,特點不重復元素,沒索引4 * Set接口的實現類,HashSet(哈希表)5 * 特點:無序集合,存儲和取出的順序不同,沒有索引,不…

leetcode1233. 刪除子文件夾

你是一位系統管理員,手里有一份文件夾列表 folder,你的任務是要刪除該列表中的所有 子文件夾,并以 任意順序 返回剩下的文件夾。 我們這樣定義「子文件夾」: 如果文件夾 folder[i] 位于另一個文件夾 folder[j] 下,那…

HIVE-分桶表的詳解和創建實例

我們學習一下分桶表,其實分區和分桶這兩個概念對于初學者來說是比較難理解的。但對于理解了的人來說,發現又是如此簡單。 我們先建立一個分桶表,并嘗試直接上傳一個數據 create table student4(sno int,sname string,sex string,sage int, sd…

51nod1270(dp)

題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId1270 題意:中文題誒~ 思路:dp sabs(a1-a0)abs(a2-a1).... 要使s盡量大,需要讓abs(ai-ai-1)盡量大,那么可以讓其中一個盡量小&…

Windows IIS 日志分析研究(Log Parser Log Parser Lizard Log Parser Studio) update...

Windows主要有以下三類日志記錄系統事件:應用程序日志、系統日志和安全日志。 存放目錄:X:\Windows\System32\winevt\Logs\ System.evtx 系統日志 Application.evtx 應用程序日志 Security.evtx 安全日志 審核策略與事件查看器 # 管理工具 → 本地安全…

ios php ide,最好的PHP IDE for Mac? (最好免費!)

這里是PHP的Mac IDE的下降NetBeans自由!此外,所有產品的最佳功能。包括內聯數據庫連接,代碼完成,語法檢查,顏色編碼,分割視圖等。下降:這是一個內存豬在Mac上。準備好允許一半的內存&#xff0c…

leetcode79. 24 點游戲

你有 4 張寫有 1 到 9 數字的牌。你需要判斷是否能通過 *,/,,-,(,) 的運算得到 24。 示例 1: 輸入: [4, 1, 8, 7] 輸出: True 解釋: (8-4) * (7-1) 24 代碼 class Solution {public boolean judgePoint24(int[] n…

Linux郵件系統整合windows 2008 R2 AD域認證更新

1. 安裝只要執行install.sh即可。(安裝包約40幾M) 2.文檔更新功能 (原v1.0文檔鏈接:http://godoha.blog.51cto.com/108180/691376) 本文轉自 godoha 51CTO博客,原文鏈接:http://blog.51cto.com/…

004:神秘的數組初始化_使容器神秘化101:面向初學者的深入研究容器技術

004:神秘的數組初始化by Will Wang王Will 介紹 (Introduction) Regardless of whether you are a student in school, a developer at some company, or a software enthusiast, chances are you heard of containers. You may have also heard that containers are lightweig…

php js動態顯示系統時間,PHP+JS動態顯示當前時間

header("content-type:text/html;charsetgb2312");date_default_timezone_set("PRC");echo var dayNames new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六&…

代碼整潔之道,clean code

一、注釋 1、不準確的注釋比沒有注釋更令人頭疼 盡量用語義化的代碼來解釋你的意圖,而不是依賴注釋來解釋一段代碼 原因很簡單:程序員不能堅持維護注釋。 代碼在后期維護中,不斷的優化、變動,很有可能最初的注釋已和現有的代碼沒…

java 獲取手機歸屬地,引起net.UnknownHostException錯誤

這個問題是請求,重定向了,跟入源碼。修改了地址,變成302 Connection connect Jsoup.connect(url);connect.header("Host", "http://info.bet007.com");connect.header("User-Agent", " Mozilla/5.0 (Wi…

leetcode713. 乘積小于K的子數組(雙指針)

給定一個正整數數組 nums。 找出該數組內乘積小于 k 的連續的子數組的個數。 示例 1: 輸入: nums [10,5,2,6], k 100 輸出: 8 解釋: 8個乘積小于100的子數組分別為: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。 需要注意的是 [10,5,2] 并不是乘積小于100的子數…

Scrum Guides 2017年最新修改

采用Scrum中增加章節\\最初Scrum是為了管理與開發產品而開發的。從90年代早期開始,Scrum已經在全球范圍內得到廣泛應用:\\研究及識別可行的市場、技術與產品能力;\\t開發產品及增強功能;\\t每天多次頻繁發布產品及增強功能&#x…

這是我最喜歡的使用React Native創建生產級應用程序的技巧

Trust me when I say this, React Native is hard. And it’s not the usual hard of what we think hard is. It is hard in terms of working with in general. In this blog post, I’ll go over some tips and tricks and eventually the best practices I’ve deployed fo…

HTTP 協議 -- 瀏覽器緩存機制

瀏覽器緩存機制瀏覽器緩存機制主要是 HTTP 協議定義的緩存機制。HTTP 協議中有關緩存的緩存信息頭的關鍵字有 Cache-Control,Pragma,Expires,Last-Modified/ETag 等。瀏覽器請求流程瀏覽器第一請求流程:瀏覽器再次請求流程&#x…

php 獲取實例的類名,PHP類名獲取方式及單例模式實現

類名是什么意思?顧名思義就是各類起了一個名字,java中有兩種數據類型,基本數據類型和引用數據類型,這里類就是引用數據類型,我們在定義一個類的時候必須給類起一個名字,一邊后面的使用比如:int …

CAP理論的理解

CAP理論作為分布式系統的基礎理論,它描述的是一個分布式系統在以下三個特性中: 一致性(Consistency)可用性(Availability)分區容錯性(Partition tolerance)最多滿足其中的兩個特性。也就是下圖所…

開啟真我新格調 期待絢麗的未知

我們每天都在朝幸福努力著,而眼光看的太遠,往往會忘記自己究竟要的是什么。人想要幸福,就得活出真我,當人不能放心大膽地活出自己時,內心會有不安和痛苦。為何要隱藏真正的自己?外界的評判真的那么重要?真我新格調&a…

vuex構建vue項目_如何使用Vue.js,Vuex,Vuetify和Firebase構建單頁應用程序

vuex構建vue項目如何使用Vuetify和Vue路由器安裝Vue并構建SPA (How to install Vue and build an SPA using Vuetify and Vue Router) Do you want to learn how to use Vue.js? Want to create a realistic website using Vue.js? In this tutorial, I will teach you how t…