Click here to Skip to main content
15,914,162 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
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
Posted

1 solution

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
 
Share this answer
 
Comments
ruby kaur 10-Jul-12 3:24am    
Thanks a lot
ruby kaur 10-Jul-12 5:49am    
Is there any other way of doing this in same table
__TR__ 10-Jul-12 7:27am    
You can use only Work_Type table as shown below

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


INSERT INTO Work_Type
SELECT 'Civil_work','road construction' UNION
SELECT 'Civil_work','Bridge construction' UNION
SELECT 'Civil_work','pond construction'


SELECT * FROM Work_Type

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900