SQL 外鍵約束怎么用語句寫出來 謝謝 最好詳細點
create table student
(
ID int identity(1,1) primary key,
Name varchar(20) null default('')
)
go
create table score
(
studentId int foreign key references student(id) not null, -- 這行即 外間約束
score int
)
foreign key references userinfo(id)
外鍵是 student 表的 id 字段
sql中怎樣創建外鍵約束
添加外鍵 ,alter table B 語法:alter table 表名 add constraint 外鍵約束名 foreign key(列名) references 引用外鍵表(列名) 如: alter table Stu_PkFk_Sc add constraint Fk_s foreign key (sno) references Stu_PkFk_S(sno) --cc是外鍵約束名,不能重復,也不能是int類型(如1,2,3) add constraint cc --B表里的需要約束的字段(id) foreign key (id) --A表后的(id)可省略 references A (id) 擴展資料:數據查詢語言,其語句,也稱為“數據檢索語句”,用以從表中獲得數據,確定數據怎樣在應用程序給出。
保留字SELECT是DQL(也是所有SQL)用得最多的動詞,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。這些DQL保留字常與其他類型的SQL語句一起使用。
參考資料:結構化查詢語言_百度百科。
sql創建外鍵語句
create table emp(
emp_id char(8) primary key, //員工號
emp_name char(30) //員工名
);
create table salary(
id char(8), //員工號(可以用emp_id我為了給你區別所以用了和上表不同的)
salary flaot, //薪資
foreign key (id) references emp (emp_id) //添加外鍵約束
);
這些代碼拷過去直接運行就行!!!
sql 設置主外鍵關系語句
--表tb_user
create table tb_user (
id int not null,
username varchar(32) not null
);
--給tb_user添加主鍵
alter table tb_user add constraint pk_users primary key (id);
--表tb_course
create table tb_course (
id int not null primary key,
name varchar(20) not null
);
--表tb_mark
create table tb_mark (
uid int not null,
cid int not null,
mark int not null,
primary key (uid, cid)
);
--給tb_mark添加外鍵
alter table tb_mark add constraint fk_uid foreign key(uid) references tb_user(id);
alter table tb_mark add constraint fk_cid foreign key(cid) references tb_course(id);
SQL語句的外鍵約束
兩個表格的結構將會是如下:
CUSTOMER 表格
欄位名 性質
SID 主鍵
Last_Name
First_Name
ORDERS 表格
欄位名 性質
Order_ID 主鍵
Order_Date
Customer_SID 外來鍵
Amount
在以上的例子中,ORDERS 表格中的 customer_SID 欄位是一個指向 CUSTOMERS 表格中 SID 欄位的外來鍵。
以下列出幾個在建置 ORDERS 表格時指定外來鍵的方式:
MySQL:
CREATE TABLE ORDERS
(Order_ID integer,
Order_Date date,
Customer_SID integer,
Amount double,
Primary Key (Order_ID),
Foreign Key (Customer_SID) references CUSTOMER(SID)); (注:這句就是外鍵的約束)
Oracle:
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date date,
Customer_SID integer references CUSTOMER(SID),
Amount double);
SQL Server:
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date datetime,
Customer_SID integer references CUSTOMER(SID),
Amount double);
以下的例子則是藉著改變表格架構來指定外來鍵。這里假設 ORDERS 表格已經被建置,而外來鍵尚未被指定:
MySQL:
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid);
Oracle:
ALTER TABLE ORDERS
ADD (CONSTRAINT fk_orders1) FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid);
SQL Server:
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid);
SQL語句的外鍵約束是什么
create table score。
SQL的主鍵和外鍵的作用:外鍵取值規則:空值或參照的主鍵值。
(1)插入非空值時,如果主鍵表中沒有這個值,則不能插入。
(2)更新時,不能改為主鍵表中沒有的值。
(3)刪除主鍵表記錄時,你可以在建外鍵時選定外鍵記錄一起級聯刪除還是拒絕刪除。
(4)更新主鍵記錄時,同樣有級聯更新和拒絕執行的選擇。
簡而言之,SQL的主鍵和外鍵就是起約束作用。
alter table 外鍵表名 add constraint 約束名稱 foreign key (外鍵字段) references 主鍵表名(約束列名)。
如果表A中的Ids是主鍵,要約束表B中得Aid列,那么語句應該是:alter table B add constraint A_B_Ids foreign key(Aid) references A(Ids)。
SQL語句基礎學習外鍵是怎樣的
外來鍵是一個(或數個)指向另外一個表格主鍵的欄位。
外來鍵的目的是確定資料的參考完整性(referential integrity)。換言之,只有被準許的資料值才會被存入資料庫內。
舉例來說,假設我們有兩個表格:一個 CUSTOMER 表格,里面記錄了所有顧客的資料;另一個 ORDERS 表格,里面記錄了所有顧客訂購的資料。 在這里的一個限制,就是所有的訂購資料中的顧客,都一定是要跟在 CUSTOMER 表格中存在。
在這里,我們就會在 ORDERS 表格中設定一個外來鍵,而這個外來鍵是指向 CUSTOMER 表格中的主鍵。這樣一來,我們就可以確定所有在 ORDERS 表格中的顧客都存在 CUSTOMER 表格中。
換句話說,ORDERS表格之中,不能有任何顧客是不存在于 CUSTOMER 表格中的資料。 這兩個表格的結構將會是如下: CUSTOMER 表格 欄位名性質 SID主鍵 Last_Name First_Name ORDERS 表格 欄位名性質 Order_ID主鍵 Order_Date Customer_SID外來鍵 Amount 在以上的例子中,ORDERS 表格中的 customer_SID 欄位是一個指向 CUSTOMERS 表格中 SID 欄位的外來鍵。
以下列出幾個在建置 ORDERS 表格時指定外來鍵的方式: MySQL: CREATE TABLE ORDERS (Order_ID integer, Order_Date date, Customer_SID integer, Amount double, Primary Key (Order_ID), Foreign Key (Customer_SID) references CUSTOMER(SID)); Oracle: CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date date, Customer_SID integer references CUSTOMER(SID), Amount double); SQL Server: CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date datetime, Customer_SID integer references CUSTOMER(SID), Amount double); 以下的例子則是藉著改變表格架構來指定外來鍵。 這里假設 ORDERS 表格已經被建置,而外來鍵尚未被指定: MySQL: ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid); Oracle: ALTER TABLE ORDERS ADD (CONSTRAINT fk_orders1) FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid); SQL Server: ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(sid);。
SQL語句 “外鍵的外鍵”
create table XK
(
Sno varchar(15) not null foreign key references Student(Sno), --學號
Cno varchar(10) not null , --課程號
Tno varchar(15) not null , --工號
foreign key(Tno,Cno) references RK(Tno,Cno),
primary key (Sno,Cno,Tno) --主鍵
)