1. 程式人生 > 資訊 >無新增劑:豆本豆唯甄豆奶 1 元/盒限時購

無新增劑:豆本豆唯甄豆奶 1 元/盒限時購

一、MySQL基礎操作

1、連線資料庫

mysql  -h 域名 -u 使用者名稱 -p 密碼 -P 埠  

2、退出

quit 或 Ctrl + D

二、檢視資料庫,建立資料庫,使用資料庫檢視資料庫: show databases;

1、建立資料庫

create database 資料庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;     # utf8編碼
create database 資料庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; # gbk編碼

2、檢視資料庫

show databases;

3、使用資料庫

use 資料庫名;

4、檢視資料庫中的表

show tables;

三、使用者管理

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

四、許可權管理

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  
            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、資料庫幾內部其他許可權:

資料庫名.*           資料庫中的所有
資料庫名.表          指定資料庫中的某張表
資料庫名.儲存過程     指定資料庫中的儲存過程
*.*                所有資料庫

2、使用者和IP的許可權:  

使用者名稱@IP地址         使用者只能在改IP下才能訪問
使用者名稱@192.168.1.%   使用者只能在改IP段下才能訪問(萬用字元%表示任意)
使用者名稱@%             使用者可以再任意IP下訪問(預設IP地址為%)

3、許可權操作

檢視授權
show grants for '使用者'@'IP地址' 

授權
grant  許可權 on 資料庫.表 to   '使用者'@'IP地址'

取消授權
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 表名;

4、清空表內容

delete from 表名
truncate table 表名

5、修改表

新增列:   
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;

6、