Sql中的主键和外键 [英] Primary Key and Foreign Key in Sql

查看:92
本文介绍了Sql中的主键和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何在SQl中使用主键和外键?.给出示例解决方案???

问候
Balamurugasn

Hi,
How to use Primary Key and Foreign Key in SQl?.Give Solution with Example???

Regards
Balamurugasn

推荐答案

Google是您的朋友:很好,经常拜访他.与在这里发布问题相比,他可以更快地回答问题.

使用您的问题作为搜索词进行的快速搜索带来了150万次匹配: http://www.w3schools.com/sql/sql_foreignkey.asp [ ^ ]并提供了很好的解释和示例.

将来,请尝试至少自己进行基础研究,不要浪费您或我们的时间.
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search using your question as the search term gave over 1.5 million hits: Google : How to use Primary Key and Foreign Key in SQl[^]
The first hit is W3Schools: http://www.w3schools.com/sql/sql_foreignkey.asp[^] with a good explanation and example.

In future, please try to do at least basic research yourself, and not waste your time or ours.


假设有两个表

创建表empmaster
(
Empid Int identity(1,1)主键,
姓varchar(20),
Empaddress varchar(50),
EMpDoB日期时间
)

创建表empjob
(
jobid int不为null,
empid int不为null,
jobdesc varchar(40),
外键(empid)参考empmaster(empid)
)

在这里,我们指的是主键和外键关系,以获取具有jobdesc的特定员工的详细记录.
希望它能对您有所帮助.
Assume there are two tables

Create table empmaster
(
empid Int identity(1,1) primary key,
Empname varchar (20),
Empaddress varchar(50),
EMpDoB datetime
)

create table empjob
(
jobid int not null,
empid int not null,
jobdesc varchar(40),
FOREIGN KEY (empid) REFERENCES empmaster(empid)
)

here we are refering the primary key and foreign key relationship to get the detail records for particular employee with jobdesc.
Hope its help you.


这篇关于Sql中的主键和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆