資料庫 約束

2021-08-17 04:08:09 字數 1189 閱讀 4034

約束說白了就是限制條件

當你不給某引數賦值時,該引數會採用預設值。

例子:

--建立資料庫

create database tx

--選擇使用的資料庫

use tx

go--goods表

create table goods

(goodsid nvarchar(50) primary key,--主鍵

goodsname nvarchar(80) not null,--不為空

unitprice numeric(10,2)check (unitprice>0.0),--check

category nvarchar(3) check(category in('實物','日用品')),

provider nvarchar(50)

)--customer表

create table customer

(customerid nvarchar(50) primary key,

customername nvarchar(50) not null,

[address] nvarchar(100),

email nvarchar(100) unique,--唯一

*** nchar(1) check(*** in('男','女')) default '男',--預設

idcard char(18)

)--purchase表

create table purchase

(customerid nvarchar(50) foreign key references customer(customerid),--外來鍵

goodsid nvarchar(50) foreign key references goods(goodsid),

nums int check(nums>0)

)

資料庫約束

mysql有兩種常用的引擎型別 myisam和innodb。目前只有innodb引擎型別支援外來鍵約束。innodb中外鍵約束定義的語法如下 constraint symbol foreign key index name index col name,references tbl name ind...

資料庫約束

約束的簡介 資料的完整性是指資料的正確性和一致性,可以通過定義表時定義完整性約束,也可以通過規則,索引,觸發器等。約束分為兩類 行級和表級,處理機制是一樣的。行級約束放在列後,表級約束放在表後,多個列共用的約束放在表後。完整性約束是一種規則,不占用任何資料庫空間。完整性約束存在資料字典中,在執行sq...

資料庫約束

概念 對錶中的資料進行限定,保證資料的正確性,有效性和完整性。1.非空約束 not null 1.1 建立表時新增約束 create table 表名 id int,name varchar 10 not null 1.2 建立表完後,新增非空約束 alter table 表名 modify nam...