1. 程式人生 > >Day054--MySQL, 建立使用者和授權, 資料型別, 列舉和集合, 約束,唯一, 主鍵,外來鍵

Day054--MySQL, 建立使用者和授權, 資料型別, 列舉和集合, 約束,唯一, 主鍵,外來鍵

建立使用者和授權

1.建立使用者:
# 指定ip:192.118.1.1的mjj使用者登入
create user 'mjj'@'192.118.1.1' identified by '123';
# 指定ip:192.118.1.開頭的mjj使用者登入
create user 'mjj'@'192.118.1.%' identified by '123';
# 指定任何ip的mjj使用者登入
create user 'mjj'@'%' identified by '123';

2.刪除使用者
drop user '使用者名稱'@'IP地址';


3.修改使用者
rename user '使用者名稱'@'IP地址' to '新使用者名稱'@'IP地址';

4.修改密碼
set password for '使用者名稱'@'IP地址'=Password('新密碼');

*****進入mysql庫, use mysql; 輸入
select * from user\G;
檢視所有使用者*****

對當前的使用者授權管理
檢視許可權
show grants for '使用者'@'IP地址';

#授權 mjj使用者僅對db1.t1檔案有查詢、插入和更新的操作
grant select ,insert,update on db1.t1 to "mjj"@'%';

# 表示有所有的許可權,除了grant這個命令,這個命令是root才有的。mjj使用者對db1下的t1檔案有任意操作
grant all privileges  on db1.t1 to "mjj"@'%';
#mjj使用者對db1資料庫中的檔案執行任何操作
grant all privileges  on db1.* to "mjj"@'%';
#mjj使用者對所有資料庫中檔案有任何操作
grant all privileges  on *.*  to "mjj"@'%';

遠端連線:
mysql -uskx -P3306 -h 192.168.15.113 -p123

複製表

#即複製表結構 又複製記錄
create table t2 select * from db1.t1;

# 只複製表結構,不復制記錄
create table t2 select * from db1.t1 where 1>3; #只要後面條件不成立,表示只複製表結構
create table t2 like db1.t1;
 

資料型別

整型 預設是有符號

資料型別 無符號(unsigned)和有符號 用0填充 zerofill

約束的作用: 保證資料的完整性和一致性

  • tinyint [-128~127] 小整數
  • int 整數
  • bigint 極大整數

  • create table t1(id int(4) unsigned,name char(20));


浮點型

  • float 單精度 隨著小數位數的增多,不準確
  • double 雙精度 隨著小數位數的增多.不準確,比float要準確
  • decimal 小數 精準的小數

日期型別

year 年份 (1901~2155)

date 年月日

time 時分秒

datetime 年月日 時分秒 ***

now() sql語言中自帶的內容函: 獲取當前的時間(根據資料型別)

create table t10(born_year year,intClass datetime);

語法:
        YEAR
            YYYY(1901/2155)

        DATE
            YYYY-MM-DD(1000-01-01/9999-12-31)

        TIME
            HH:MM:SS('-838:59:59'/'838:59:59')

        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 年某時)

字元型別

  • char 定長,簡單粗暴,浪費空間,存取速度快
  • varchar 變長,精準,節省空間,存取速度慢
  • text 長文字

    length():檢視位元組數
    char_length():檢視字元數

列舉和集合

 create table consumer(
     id int,
     name varchar(50),
     sex enum('male','female','other') default 'male',
     level enum('vip1','vip2','vip3','vip4'),#在指定範圍內,多選一
     fav set('play','music','read','study') #在指定範圍內,多選多
    );

注意:在sql中使用tinyint(1)來表示boolean型別

完整性約束

作用: 保證資料的完整性和一致性

not null 與 default

  • 如果單獨設定not null 不能插入空值
  • 如果即設定了not null,又指定default,可以插入空值,會走default

unique 唯一

單列唯一

create table t4(
    id int not null,
    name char(20) unique
);

create table t4(
    id int not null,
    name char(20),
    unique(name)
);
insert into t4(id,name) values(1,'alex');
insert into t4(id,name) values(1,'wusir');

多列唯一

  • 只要有一列相同,不能插入

    create table t5(
    id int,
    name char(20),
    unique(id),
    unique(name)
    );

聯合唯一 ***

  • 多列相同時,不能插入

    create table t6(
    id int,
    name char(20),
    unique(id,name)
    );

應用場景: 選課系統,一個學生可以選擇多個課程,一個課程可以被多個學生選擇,

student_id course_name

100 '生物'

101 '生物'

100 '化學

primary key

MySQL為什麼需要一個主鍵?

https://blog.csdn.net/shellching/article/details/77367557

化學反應: not null + unique

Mysql版本一個表中只能有一個主鍵,可以有聯合主鍵,但不能有多列主鍵

單列主鍵 不能為空 並且是唯一

# primary key 索引(針對於大量資料) 查詢速度要快
create table t7(
    id int primary key,
    name varchar(10) unique
);

create table t8(
    id int not null unique,
    name varchar(10) unique
);

複合主鍵

create table t9(    
    id int,    
    name varchar(10),
    primary key(id,name)
);

auto_increment 自增量,升序排列,初始值為1,步長為1

create table student(
    id int primary key auto_increment,
    name varchar(20) not null,
    sex enum('male','female') default 'male', 
    ip varchar(20) unique
);

insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');

auto_increment_increment 設定自增值, 也就是步長

auto_increment_offset 設定偏移值,也就是起始量

檢視可用的 開頭auto_inc的詞
mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
rows in set (0.02 sec)
# 步長auto_increment_increment,預設為1
# 起始的偏移量auto_increment_offset, 預設是1

 # 設定步長 為會話設定,只在本次連線中有效
 set session auto_increment_increment=5;

 #全域性設定步長 都有效。
 set global auto_increment_increment=5;

 # 設定起始偏移量
 set global  auto_increment_offset=3;

#強調:If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored. 
翻譯:如果auto_increment_offset的值大於auto_increment_increment的值,則auto_increment_offset的值會被忽略 

# 設定完起始偏移量和步長之後,再次執行show variables like'auto_inc%';
發現跟之前一樣,必須先exit,再登入才有效。

mysql> show variables like'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 3     |
+--------------------------+-------+
rows in set (0.00 sec)

#因為之前有一條記錄id=1
mysql> select * from student;
+----+---------+------+
| id | name    | sex  |
+----+---------+------+
|  1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)
# 下次插入的時候,從起始位置3開始,每次插入記錄id+5
mysql> insert into student(name) values('ma1'),('ma2'),('ma3');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+---------+------+
| id | name    | sex  |
+----+---------+------+
|  1 | xiaobai | male |
|  3 | ma1     | male |
|  8 | ma2     | male |
| 13 | ma3     | male |
+----+---------+------+

auto_increment_increment和 auto_increment_offset

清空表區分delete和truncate的區別:

delete from t1; #如果有自增id,新增的資料,仍然是以刪除前的最後一樣作為起始。

truncate table t1;資料量大,刪除速度比上一條快,且直接從零開始。

foreign key ***

外來鍵

# 先建立主表
create table dep(
    id int primary key auto_increment,
    name char(10) unique,
    dep_desc varchar(50) not null
);
# 校區表
create table school(
    id int primary key auto_increment,
    addr varchar not null
)



# 建立從表
create table emp(
    eid int primary key auto_increment,
    name char(10) not null,
    age int not null,
    dep_id int,
    school_id int,
    constraint fk_dep foreign key(dep_id) references dep(id) 
    on delete cascade 
    on update cascade,
     constraint fk_school foreign key(school_id) references school(id) 
    on delete cascade 
    on update cascade
);

insert into dep(name,dep_desc) values('校長部','校長管理有限部門'),('公關部','公關管理有限部門'),('IT部門','IT技術有限部門'),('財務部','管錢很多部門');
insert into emp(name,age,dep_id) 
    values
    ('alex',18,1),
    ('wusir',30,2),
    ('吳老闆',20,3),
    ('馬老闆',18,4),
    ('邱老闆',20,2),
    ('女神',16,3);