MySQL(一)

來源:互聯網
上載者:User

標籤:default   body   文章   bms   creat   許可權管理   set   man   ons   

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)(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、基礎資料型別 (Elementary Data Type)

MySQL的資料類型大致分為:數值、時間和字串

 View Codemysql表內容操作

 表內容操作無非就是增刪改查,當然用的最多的還是查,而且查這一塊東西最多,用起來最難,當然對於大神來說那就是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 > 1select 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之前

-此文章轉載-轉載自:http://www.cnblogs.com/aylin/p/5744312.html

MySQL(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.