1. 程式人生 > 其它 >Java入門姿勢:【流程控制篇】

Java入門姿勢:【流程控制篇】

資料庫約束是對錶中的資料進行進一步的限制,保證資料的正確性、有效性和完整性。約束用於限制加入表的資料的型別,可以在建立表時規定約束(通過 CREATE TABLE 語句),或者在表建立之後也可以(通過 ALTER TABLE 語句)

1、主鍵約束PRIMARY KEY

  • 每張資料表只能存在一個主鍵

  • 主鍵保證記錄的唯一性,主鍵的值不重複

  • 主鍵自動為 NOT NULL

  • 可以使用一列作為主鍵,也可以使用兩列作為主鍵,稱之為符合主鍵,必須通過兩個列的資料才能進行整體定位的表可以使用。

    # 建立表的時候增加主鍵約束
    mysql> create table user_key (
        -> id int(11),# id int(11) primary key,
        -> age int(3),
        -> name varchar(30),
        -> primary key (id));
    Query OK, 0 rows affected (0.03 sec)
    
    mysql> desc user_key;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | NO   | PRI | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(30) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    # 給已存在列新增主鍵約束
    mysql> alter table user_key modify id int(11) primary key; # alter table user_key add primary key(id);
    Query OK, 0 rows affected (0.05 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> desc user_key;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | NO   | PRI | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(30) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    
    # 刪除主鍵約束
    mysql> alter table user_key drop primary key;
    Query OK, 0 rows affected (0.04 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> desc user_key;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | NO   |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(30) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    # 刪除NOT NULL約束
    mysql> alter table user_key modify id int(11);
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> desc user_key;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | YES  |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(30) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    

2、唯一約束UNIQUE KEY

  • 唯一約束可以保證記錄的唯一性
  • 唯一約束的欄位可以為空值(NULL)
  • 每張資料表可以存在多個唯一約束
# 建立表的時候新增唯一約束
mysql> create table user_un(
    -> id int unique,
    -> name char(10));
Query OK, 0 rows affected (0.02 sec)

mysql> desc user_un;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  | UNI | NULL    |       |
| name  | char(10) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

# 已存在的表新增唯一約束
# alter table user_un modify phone int unique;修改
mysql> alter table user_un add unique(name);   # 多個欄位用逗號隔開
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_un;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  | UNI | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

# 刪除唯一約束
mysql> alter table user_un drop index phone;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_un;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  | UNI | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
| phone | int(11)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

3、預設約束DEFAULT

  • 當插入記錄時,如果沒有明確為欄位賦值,則自動賦予預設值
# 建立表的時候新增約束
mysql> create table user_de(
    -> id int primary key,
    -> name char(10) not null,
    -> age int not null default '0');  # age預設為0
Query OK, 0 rows affected (0.03 sec)

mysql> desc user_de;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | NO   | PRI | NULL    |       |
| name  | char(10) | NO   |     | NULL    |       |
| age   | int(11)  | NO   |     | 0       |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

# 給已存在的表新增預設值約束
mysql> alter table user_de alter column address set default 'abc';
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_de;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| name    | char(10)     | YES  |     | NULL    |       |
| age     | int(11)      | YES  |     | 1       |       |
| address | varchar(100) | YES  |     | abc     |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

# 修改預設值
mysql> desc user_de;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| name    | char(10)     | YES  |     | NULL    |       |
| age     | int(11)      | NO   |     | 3       |       |
| address | varchar(100) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table user_de modify age int default '1';
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_de;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| name    | char(10)     | YES  |     | NULL    |       |
| age     | int(11)      | YES  |     | 1       |       |
| address | varchar(100) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


# 移除預設值約束
# alter table user_de age int drop default;
mysql> alter table user_de modify name char(10);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_de;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| name    | char(10)     | YES  |     | NULL    |       |
| age     | int(11)      | NO   |     | 0       |       |
| address | varchar(100) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

#新增一條資料不給age值,age預設儲存0
mysql> insert into user_de (id,name) values (1,"張三");
Query OK, 1 row affected (0.00 sec)

mysql> select * from user_de;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
|  1 | 張三   |   0 |
+----+--------+-----+
1 row in set (0.00 sec)

4、非空約束NOT NULL

  • 強制列不能為 NULL 值,約束強制欄位始終包含值
  • 這意味著,如果不向欄位新增值,就無法插入新記錄或者更新記錄
# 建立表時增加非空約束
mysql> create table user_no(
    -> id int not null,
    -> name varchar(255),
    -> age int);
Query OK, 0 rows affected (0.02 sec)、

# 已存在的表增加非空約束
mysql> alter table user_no modify name varchar(255) not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

# 取消非空約束
mysql> alter table user_no modify name varchar(255) null;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

5、外來鍵約束FOREIGN KEY

  • 保持資料的一致性,完整性
  • 實現1對1或1對n關係
# 班級表
mysql> desc class;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(50) | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# 建立表時新增外來鍵約束
mysql> create table student(
    -> id int,
    -> name varchar(50),
    -> c_id int,
    -> c_name varchar(50),
    -> constraint fk_cid foreign key(c_id) references class(id));
Query OK, 0 rows affected (0.03 sec)

# 已存在的表新增外來鍵約束
mysql> ALTER TABLE stu ADD CONSTRAINT fk_cid FOREIGN KEY(c_id) REFERENCES class(id);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

# 刪除外來鍵約束
# 先檢視外來鍵名
mysql> show create table stu;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                        |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stu   | CREATE TABLE `stu` (
  `id` int(11) DEFAULT NULL,
  `name` char(50) DEFAULT NULL,
  `c_id` int(11) DEFAULT NULL,
  KEY `fk_cid` (`c_id`),
  CONSTRAINT `fk_cid` FOREIGN KEY (`c_id`) REFERENCES `class` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
# fk_cid為外來鍵名稱
mysql> alter table student drop foreign key fk_cid; 
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

6、自增約束AUTO_INCREMENT

  • 可以約束任何一個欄位
  • 不一定是PRIMARY KEY欄位,也就是說自增的欄位並不等於主鍵欄位
  • 但是PRIMARY_KEY約束的主鍵欄位,一定是自增欄位,即PRIMARY_KEY 要與AUTO_INCREMENT一起作用於同一個欄位
# 建立表示新增自增約束
mysql> create table zz(
    -> id int auto_increment primary key,
    -> name char(50));
Query OK, 0 rows affected (0.02 sec)

# 已存在表新增自增約束
mysql> alter table zz modify id int(11) auto_increment;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

# 刪除自增約束
mysql> desc zz;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(50) | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> alter table zz modify id int;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

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