如何在SQL Server 2008中创建子字段 [英] how to create sub fields in sql server 2008

查看:106
本文介绍了如何在SQL Server 2008中创建子字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个表,其中有一个名为"Work_Type_Name"的字段,该字段又具有"sub_work_Name".
例如,列名是"Civil_work",并且在此工作类型中有:
道路建设,
桥梁建设,
池塘建设,那里还有更多的子专栏.

像这样,有更多的列字段具有子列.

我的问题是如何创建具有这种类型的结构的这种类型的表.请帮我.

在此先感谢


i have a table in which i have a field named "Work_Type_Name" this field again has "sub_work_Name".
For example column name is "Civil_work" and in this work type there are:
road construction,
Bridge Construction,
pond Construction, and few more sub column are there.

like this there are more column fields which are having sub column.

my question is that how to create this type of table which is having this type of structure. pl.help me.

thanks in advance

推荐答案

如果我正确理解这一点,则您正在尝试将特定工作类型的所有子工作名称存储在表中.

您可以设计表,如以下示例所示.

所有工作类型都将存储在表Work_Type

所有子工作类型将存储在表Sub_Work_Type中.

使用Sub_Work_Type表中的Work_Type_ID列将子工作类型"链接到工作类型"


If i understand this correctly, you are trying to store all the sub work Names of a particular Work Type in a table.

You can design your tables as shown in the below sample.

All the Work types will be stored in table Work_Type

All the Sub Work Types will be stored in table Sub_Work_Type.

Sub Work type is linked to Work type using Work_Type_ID column in Sub_Work_Type Table


CREATE TABLE Work_Type
(
	Work_Type_ID INT IDENTITY(1,1) CONSTRAINT PK_Work_Type PRIMARY KEY,
	Work_Type_Name VARCHAR(100)
)


CREATE TABLE Sub_Work_Type
(
	Sub_Work_Type_ID INT IDENTITY(1,1) CONSTRAINT PK_Sub_Work_Type PRIMARY KEY,
	Sub_Work_Type_Name VARCHAR(100),
	Work_Type_ID INT NOT NULL CONSTRAINT FK_Sub_Work_Type_Work_Type FOREIGN KEY REFERENCES Work_Type(Work_Type_ID)
)



INSERT INTO Work_Type
SELECT ''Civil_work''

DECLARE @Work_Type_ID INT
SET @Work_Type_ID = SCOPE_IDENTITY()

INSERT INTO Sub_Work_Type
SELECT ''road construction'',@Work_Type_ID UNION 
SELECT ''Bridge construction'',@Work_Type_ID UNION 
SELECT ''pond construction'',@Work_Type_ID 


SELECT Work_Type_Name, Sub_Work_Type_Name FROM Work_Type W
INNER JOIN Sub_Work_Type S ON W.Work_Type_ID = S.Work_Type_ID


这篇关于如何在SQL Server 2008中创建子字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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