SQLベース基礎となるオペレーティング制約

制約は、次のとおりです。

  • 1.主キー制約はprimary key
  • 2.非ヌル制約not null;
  • 3.唯一の制約unique
  • 4.外部キー制約foreign key

ここで主キー制約は、空でないと唯一の制約の制約を含みます。

私はテーブルを使用します。

mysql> create table stu(
    ->          id int,
    ->          name varchar(20),
    ->          age int,
    ->          score double(4,2),
    ->          address_id int
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table address(
    ->          id int primary key auto_increment, # id自增
    ->          description varchar(20)
    -> );
Query OK, 0 rows affected (0.02 sec)

insert into address(description)values('France'),('England'),('China');

非空制約

初出:

  • テーブルを作成するときにNOT NULL制約を追加しますname varchar(20) not null
  • 作成した後に追加alter table stu modify name varchar(20) not null

削除:

  • alter table stu modify name varchar(20)

例:

创建表以后:
mysql> alter table stu modify name varchar(20) not null;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

创建表时:
mysql> create table stu(
    ->          id int,
    ->          name varchar(20) not null,
    ->          age int,
    ->          score double(4,2),
    ->          address_id int)
    -> ;
Query OK, 0 rows affected (0.02 sec)
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

删除非空约束:
mysql> alter table stu modify name varchar(20);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| name       | varchar(20) | YES  |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec

主キー制約

初出:

  • 主キー制約上、直接テーブルを作成、書き込み:id int primary keyまたは再書き込み決勝primary key (id)
  • その後、テーブルを作成するために、将来的には、主キー制約を追加しますalter table stu modify id int primary keyalter table stu add primary key (id)
    注:使用primary key(id)時間は、0のデフォルト値を設定します。

削除:

  • 使用drop primary key、使用現時点では注意modify変更します。

例:

创建好以后添加主键约束:
方式一:
mysql> alter table stu modify id int primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
方式二:
mysql> alter table stu add primary key (id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | 0       |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
这两种方式一个有默认值,一个没有默认值。

创建时添加主键约束:
mysql> create table stu(
    ->          id int primary key,
    ->          name varchar(20) not null,
    ->          age int,
    ->          score double(4,2),
    ->          address_id int
    -> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc stu;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | NO   | PRI | NULL    |       |
| name       | varchar(20) | NO   |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| score      | double(4,2) | YES  |     | NULL    |       |
| address_id | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

外部キー制約

初出:

  • とき、あなたは創造のテーブルを作成しますconstraint FK_stu_address foreign key (address_id) references address(id)ここで、constraintキー制約がされFK_stu_address、外部キーの名前で、addresss_id外部キー列名は、address主テーブル名で、彼らの実際の状況の変化に応じて、constraint FK_stu_addressこれは書くことはできません。
  • あなたが作成した後に追加しますadd constraint FK_stu_address foreign key (address_id) references address(id)

削除:

  • alter table stu drop foreign key FK_stu_address

カスケード:プライマリ・テーブルを変更する場合、変更対応するデータがテーブルから続く、ラインは直接使用しました。

  • on update cascadeカスケード更新
  • on delete cascade削除カスケード
  • 小さな例を使用します。
mysql> alter table stu add constraint FK_stu_address foreign key (address_id)  REFERENCES  address(id) on update cascade;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

例:

创建以后添加外键:
mysql> alter table stu add constraint FK_stu_address foreign key (address_id)  REFERENCES  address(id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

创建时添加外键:
mysql> create table stu(
    ->          id int,
    ->          name varchar(20),
    ->          age int,
    ->          score double(4,2),
    ->          address_id int,
    ->          primary key(id),
    ->          constraint FK_stu_address # 这一行可以不写
    ->          foreign key(address_id)   # 外键列名
    ->          REFERENCES  address(id)   # 对应主表中的东西
    -> );
Query OK, 0 rows affected (0.02 sec)

删除外键:
mysql> alter table stu drop foreign key FK_stu_address;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
リリース7件のオリジナルの記事 ウォンの賞賛0 ビュー74

おすすめ

転載: blog.csdn.net/LuSHui_1997/article/details/104746744