1. 程式人生 > >約束條件與資料型別的關係

約束條件與資料型別的關係

建立表___約束條件(語法):
約束條件與資料型別的寬度一樣,都是可選引數

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

一: not null: 不允許傳空

default: 預設值

比如建立一個為t11的表:

create table t11(
    id int,
    name char(6),
    sex enum('male','female') not null default 'male'
    );
 
#sex選項只能選擇一個,而且不能為空,如果不選預設就指定為male   

往t11表裡面插入資料:

insert into t11(id,name) values(1,'egon');
insert into t11(id,name) values(2,'alex');
注意:在建立表時有多個欄位,插入資料時要對應著插,
當建立表出現enum時,插入資料的時候需指定.比如id,name指定,sex設定為預設就不用指定.

用select * from t11; 語句查詢:

mysql> select * from t1
+------+------+------+
| id   | name | sex  |
+------+------+------+
|    1 | egon | male |
|    2 | alex | male |
+------+------+------+
結果顯示:

二: unique:限制欄位傳入的值是唯一,不能重複

1. 設定唯一約束:

建立表時:

方式一:
create table department(
    id int unique,
    name char(10)unique);
方式二:
create table department(
    id int unique,
    name char(10),
    unique(id),;
    unique(name)
    );

插入資料時方式一樣:須確保插入的資料的唯一性.

insert into department values
(1,'IT'),
(2,'Sale');

結果顯示如下:

mysql> select * from department;
+------+------+
| id   | name |
+------+------+
|    1 | IT   |
|    2 | Sale |
+------+------+
保證了id,name欄位的資料唯一性

==- 延伸擴充套件: not null + unique 的結合使用==

mysql> create table t1(id int not null unique);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

2:聯和唯一:

單獨某一個欄位重複可以,只要合在一起就不能重複,而且單獨和聯和唯一可以結合在一起使用.

先來建立表:

create table services(
    id int,
    ip char(15),
    port int,
    unique(id),      #也可單獨建立時設定唯一約束
    unique(ip,port)  #建立時聯和起來用,
    );
                     #PS:socket應用時ip,port是聯和在一起運用的

插入資料:

 insert into services values
 (1,'192.168.11.10',80),
 (2,'192.168.11.10',87),
 (3,'192.168.11.113',80);

結果顯示:

mysql> select * from  services;
+------+----------------+------+
| id   | ip             | port |
+------+----------------+------+
|    1 | 192.168.11.10  |   80 |
|    2 | 192.168.11.10  |   87 |
|    3 | 192.168.11.113 |   80 |
+------+----------------+------+

三:primary key (主鍵) primary又分單列主鍵,複合主鍵

作用:組織表的資料,提高查詢速度

從約束的角度講就是: not null unique欄位的值不為空且唯一

從儲存角度來說我們用的儲存引擎,預設用的是innodb

對於innodb儲存引擎來說,一張表內必須有一個主鍵

primary key又分單列主鍵,複合主鍵

那麼如果建primary key(主鍵):

1.單列主鍵:

建立: 通常id設為主鍵


 create table t17(
     id  int primary key, #意思是說id不能為空
     name char(16)
     );

插入資料:

 insert into t17 values(1,'egon'),(2,'alex');
 
 insert into t17(name) values('wxx'); #這裡在插入資料的時候沒有插入id號.

顯示結果:

mysql> select * from t17;
+----+------+
| id | name |
+----+------+
|  0 | wxx  |
|  1 | egon |
|  2 | alex |
從結果來看:在建立表時.id欄位設定為 primary key 不能為空
          在插入資料wxx時,又沒有插入id號
結果來看,在沒有傳入id值時,會預設的加一個id號為0進去
注意: BUT如果你再次插入資料的時候,還是沒有將id的值傳入時,
     那麼就會插入資料失敗,因為在不傳值給id時,他只會預設將值為0,賦值一次.

mysql> desc t17;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   | PRI | NULL    |       |
| name  | char(16) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

2.複合主鍵:

mysql> insert into t18(name) values
    -> ('alex77888');
ERROR 1062 (23000): Duplicate entry '0' for key 'id'
mysql>  create table t19(
    ->     ip char(15),
    ->     port int,
    ->     primary key (ip,port)
    ->     );
Query OK, 0 rows affected (0.09 sec)

mysql>
mysql>
mysql> insert into t19  values
    -> ('1.1.1.1',80),
    -> ('1.1.1.1',79);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t19;
+---------+------+
| ip      | port |
+---------+------+
| 1.1.1.1 |   79 |
| 1.1.1.1 |   80 |
+---------+------+
2 rows in set (0.00 sec)

==總結:通常對於innodb儲存引擎來說,一張表內必須有一個主鍵,一般設定id欄位為主鍵.==

四:auto_increment

約束欄位為自動增長,被約束的欄位必須同時被key約束

1.不指定id,則自動增長

mysql>
mysql> create table t20(
    ->     id int primary key auto_increment,
    ->     name  char(18)
    ->     );
Query OK, 0 rows affected (0.23 sec)

mysql>
mysql>
mysql>
mysql> insert into t20(name) values
    -> ('egon'),
    -> ('alex'),
    -> ('wxx');
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql>
mysql> desc t20;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(18) | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> select * from t20;
+----+------+
| id | name |
+----+------+
|  1 | egon |
|  2 | alex |
|  3 | wxx  |
+----+------+

2.指定id:


mysql> insert into t20(id, name) values
    -> (7,'哈哈哈');   #指定id,讓從7開始
Query OK, 1 row affected (0.01 sec)

#再次插入資料時,還是從指定id號7開始往後自增
mysql> insert into t20(name) values
    -> ('egon1'),
    -> ('egon2'),
    -> ('egon3');
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

看結果:
mysql> select * from t20;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | egon      |
|  2 | alex      |
|  3 | wxx       |
|  7 | 哈哈哈    |
|  8 | egon1     |
|  9 | egon2     |
| 10 | egon3     |
+----+-----------+
7 rows in set (0.00 sec)

==注意:==

對於自增的欄位,在用delete刪除後,再插入值,該欄位仍按照刪除前的位置繼續增長

應該用truncate清空表,比起delete一條一條地刪除記錄,truncate是直接清空表,在刪除大表時用它.


mysql> delete from t20;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t20;
Empty set (0.00 sec)

mysql>  insert into t20(name) values('xqw');
Query OK, 1 row affected (0.07 sec)

mysql> select * from t20;
+----+------+
| id | name |
+----+------+
| 11 | xqw  |
+----+------+
1 row in set (0.00 sec)

mysql> truncate t20;  
Query OK, 0 rows affected (0.10 sec)

mysql>  insert into t20(name) values('xqw');
Query OK, 1 row affected (0.07 sec)

mysql> select * from t20;
+----+------+
| id | name |
+----+------+
|  1 | xqw  |
+----+------+
1 row in set (0.00 sec)

3.瞭解知識點:

輸入指令:  show variables like 'auto_inc%'; #檢視msyql增加配置的變數,像以auto_inc開頭的任意字元的所有變數,
會出現:
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1    |    #auto_increment_increment  步長預設為1         
| auto_increment_offset    | 1     |   #auto_increment_offset起始偏移量預設為1
+--------------------------+-------+


如何設定步長:
set  session  auto_increment_increment= 1;  #本次設定有效的步長,
set  global  auto_increment_increment= 5;  #本次全域性有效步長,前提是要所有的會話要重新登入,載入一遍才有效.

如果設定起始偏移量:
 set  global auto_increment_offset =3;
 
強調: 起始偏移量要 <= 步長

以下例項,在命令列輸入指令設定:
set  global  auto_increment_increment= 5;
set  global auto_increment_offset =3;

之後退出: exit
然後在登入mysql檢視是否設定成功:

mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 5     |
| auto_increment_offset    | 3     |
+--------------------------+-------+
#顯示已經設定成功

下面來進行驗證:
建立t21的表:
mysql> create table t21(
    ->     id int primary key auto_increment,
    ->     name  char(18)
    ->     );
Query OK, 0 rows affected (0.10 sec)


插入資料:
mysql> insert into t21(name) values
    -> ('egon3'),
    -> ('alex'),
    -> ('egon7'),
    -> ('wxx');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

驗證插入的資料,id資料資訊;
mysql> select * from t21;
+----+-------+
| id | name  |
+----+-------+
|  3 | egon3 |
|  8 | alex  |
| 13 | egon7 |
| 18 | wxx   |
+----+-------+
#從來結果來看已經改變id的起始值和步長數.

那麼問題來了,一般情況不這麼設定id,假如別人這麼設定我如何改回來呢?
直接設定設定起始偏移量和步長為1就好啦,記得要退出重新登入mysql才有效哦!

五: foreign key 作用,建立表之間的關聯

放在另外一篇.因內容有點多.