Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
chinnanu143
Starting Member
13 Posts |
Posted - 2010-11-22 : 16:20:03
|
| hi these are tables from book iam new and want practise more to get perfection well quetion ishere are 4 table which related to each otherif i create table like this it generates error because of foreign key relations..so how to do it with out errorCREATE TABLE EMPLOYEES (EMPLOYEEID NUMERIC(9) PRIMARY KEY,FIRSTNAME VARCHAR(10),LASTNAME VARCHAR(20),DEPTCODE CHAR(5) FOREIGN KEY (DEPTCODE) REFERENCES DEPARTMENTS(CODE),SALARY NUMERIC(9,2))CREATE TABLE DEPARTMENTS(CODE CHAR(5) PRIMARY KEY,NAME VARCHAR(30),MANAGERID NUMERIC(9) FOREIGN KEY (MANAGERID) REFERENCES EMPLOYEES(EMPLOYEEID),SUBDEPTOF CHAR(5) FOREIGN KEY (SUBDEPTOF) REFERENCES DEPARTMENTS(CODE))CREATE TABLE PROJECTS (PROJECTID CHAR(8) PRIMARY KEY,DEPTCODE CHAR(5) FOREIGN KEY (DEPTCODE) REFERENCES DEPATMENTS(CODE),DESCRIPTION VARCHAR(200),STARTDATE DATE,ENDDATE DATE,REVENUE NUMERIC(12,2))CREATE TABLE WORKSON(EMPLOYEEID NUMERIC(9) FOREIGN KEY (EMPLOYEEID) REFERENCES EMPLOYEES(EMPOYEEID),PROJECTID CHAR(8) FOREIGN KEY (PROJECTID) REFERENCES PROJECTS(PROJECTID),ASSIGNEDTIME DECIMAL(3,2))can i create table first add all values later then give relations/keys |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-22 : 16:30:25
|
| yes - or you can change the order of the createsActually you have a circular referenceDepartments references employees references departmentsSo you could never insert a row into either table without a foreign key error unless you allowed nulls to avoid the check.To add fk after table createalter table xxx add constraint xxxx foreign key (xxx) references xxxx (xxx)==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-22 : 16:31:28
|
quote: Originally posted by chinnanu143 can i create table first add all values later then give relations/keys
That is one way. The other, more proper, way is to create the tables. Then create the relationships/keys. Finally, you add the data to the tables so that DIR (declarative referential integrity) can be enforced. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-11-22 : 16:51:22
|
quote: Originally posted by X002548 You're assuming that hey will be doing loads...
The OP Said: "can i create table first add all values later then give relations/keys"Not much of an ssumption there. Besides, what else are you going to do with empty tables? |
 |
|
|
chinnanu143
Starting Member
13 Posts |
Posted - 2010-11-23 : 06:34:46
|
quote: Originally posted by Lamprey
quote: Originally posted by chinnanu143 can i create table first add all values later then give relations/keys
That is one way. The other, more proper, way is to create the tables. Then create the relationships/keys. Finally, you add the data to the tables so that DIR (declarative referential integrity) can be enforced.
yeah this is correct way...in this way only we can have use fkthank you |
 |
|
|
|
|
|
|
|