February 19, 2017 at 1:26 am
Hi Expert,
I tried to create a table with foreign key but I am not sure why it is giving me error -
Usually last time, I just have to write foreign key(departmentId) references tablename(departmentId).
Hope someone can advise me on this part how to write it so that there is no invalid column etc.
CREATE TABLE [dbo].[ePMT_Master_Employee](
[SGGId] [int] IDENTITY(1,1) unique not NULL,
[UserName] [nvarchar](50) NULL,
[RoleId] [int] not null,
[DeptId][int]not null,
primary key(SGGID),
foreign key(roleId) references (ePMT_Master_Role),
foreign key(departmentId)references (ePMT_Master_Department));
February 19, 2017 at 7:46 am
karenworld - Sunday, February 19, 2017 1:26 AMHi Expert,
I tried to create a table with foreign key but I am not sure why it is giving me error -Usually last time, I just have to write foreign key(departmentId) references tablename(departmentId).
Hope someone can advise me on this part how to write it so that there is no invalid column etc.
CREATE TABLE [dbo].[ePMT_Master_Employee](
[SGGId] [int] IDENTITY(1,1) unique not NULL,
[UserName] [nvarchar](50) NULL,
[RoleId] [int] not null,
[DeptId][int]not null,
primary key(SGGID),
foreign key(roleId) references (ePMT_Master_Role),
foreign key(departmentId)references (ePMT_Master_Department));
It looks like you need to specify what column contains the referenced primary key. I've also changed the name of your foreign key columns to match the columns names you create.
foreign key (RoleID) references dbo.ePMT_Master_Role(RoleID),
foreign key (DeptID) references dbo.ePMT_Master_Department (DepartmentID));
February 19, 2017 at 11:38 am
Normally when I create tables with foreign keys, I'll specify the foreign key as part of the column definition. Keeps everything in one place.
egCREATE TABLE Stations (
StationID INT IDENTITY PRIMARY KEY,
StarSystemID INT FOREIGN KEY REFERENCES dbo.StarSystems (StarSystemID) NOT NULL,
OfficialName VARCHAR(50) NOT NULL,
CommonName VARCHAR(50),
Planet TINYINT NOT NULL,
Location VARCHAR(15) NOT NULL
);
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 19, 2017 at 12:57 pm
Probably a silly question, but if you don't name the foreign key, how do you drop it (the easy way)? Can you name the constraint when you declare it that way?
February 19, 2017 at 4:25 pm
pietlinden - Sunday, February 19, 2017 12:57 PMProbably a silly question, but if you don't name the foreign key, how do you drop it (the easy way)? Can you name the constraint when you declare it that way?
I had the same question. I have a standard naming convention. I know SQL is happy to assign a name, but I'd rather name it.
February 19, 2017 at 4:56 pm
Ed,
Do you have an example of creating constraints the way Gail does, but naming them? The only examples I have seen require an ALTER TABLE ADD CONSTRAINT style... I searched around and didn't see any.
Thanks!
Pieter
February 19, 2017 at 9:02 pm
pietlinden - Sunday, February 19, 2017 4:56 PMEd,
Do you have an example of creating constraints the way Gail does, but naming them? The only examples I have seen require an ALTER TABLE ADD CONSTRAINT style... I searched around and didn't see any.
Thanks!
Pieter
Absolutely. Other than the name, the difference is that the constraint is defined separately from the column.
Of course, the real difference is that Gail's example of Stations and Star Systems is much cooler than my boring Employees table. 😛
IF OBJECT_ID('dbo.Employees', 'u') IS NOT NULL DROP TABLE dbo.Employees;
IF OBJECT_ID('dbo.Departments', 'u') IS NOT NULL DROP TABLE dbo.Departments;
CREATE TABLE dbo.Departments (
ID Integer not null identity (1, 1),
constraint Employees_PK PRIMARY KEY (ID),
Name Varchar(32) not null,
EntryDate Datetime,
UpdateDate Datetime);
CREATE TABLE dbo.Employees (
ID Integer not null identity (1, 1),
constraint EmployeesNVC_PK PRIMARY KEY (ID),
FirstName Nvarchar(32) not null,
LastName Nvarchar(32) not null,
DepartmentID Integer not null,
CONSTRAINT Employees_Departments_FK
FOREIGN KEY (DepartmentID)
REFERENCES dbo.Departments (ID),
HireDate Datetime not null);
February 20, 2017 at 12:08 am
pietlinden - Sunday, February 19, 2017 12:57 PMCan you name the constraint when you declare it that way?
Yes you can. Just add CONSTRAINT <constrain name> in front of FOREIGN KEY
CREATE TABLE StarSystems (
StarSystemID INT IDENTITY PRIMARY KEY,
SectorID INT CONSTRAINT fk_blahblahblah FOREIGN KEY REFERENCES dbo.Sectors (SectorID) NOT NULL,
OfficialName VARCHAR(50) NOT NULL,
CommonName VARCHAR(50),
GalacticLatitude NUMERIC(5,2) NOT NULL,
GalacticLongitude NUMERIC(5,2) NOT NULL,
DistanceFromSol NUMERIC(7,2) NOT NULL,
SpectralType CHAR(2) NOT NULL,
NumberOfPlanets SMALLINT NOT NULL,
Magnitude NUMERIC(4,2) NOT NULL,
IsVariable BIT NOT NULL DEFAULT 0
);
Ed Wagner - Sunday, February 19, 2017 9:02 PMOf course, the real difference is that Gail's example of Stations and Star Systems is much cooler than my boring Employees table. 😛
It's a new sample DB that I created for blog posts, articles, conference presentations, etc. Been in planning a long time, I finally say down last month and knocked it out.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 20, 2017 at 12:34 am
Oh, like that... (feeling stupid)... definitely easier to see if they're all together, but I get it. Thanks!
February 21, 2017 at 1:55 pm
karenworld - Sunday, February 19, 2017 1:26 AMHi Expert,
I tried to create a table with foreign key but I am not sure why it is giving me error -Usually last time, I just have to write foreign key(departmentId) references tablename(departmentId).
Hope someone can advise me on this part how to write it so that there is no invalid column etc.
CREATE TABLE [dbo].[ePMT_Master_Employee](
[SGGId] [int] IDENTITY(1,1) unique not NULL,
[UserName] [nvarchar](50) NULL,
[RoleId] [int] not null,
[DeptId][int]not null,
primary key(SGGID),
foreign key(roleId) references (ePMT_Master_Role),
foreign key(departmentId)references (ePMT_Master_Department));
Thinkabout "role_id"; what kind of role is there in this model?You are using very generic and therefore improper data element names.If you take a course in basic data modeling they would have beatenthis out of you during the first week of the class.
CREATETABLE Personnel
(user_idCHAR(10) NOT NULL PRIMARY KEY,
user_nameNVARCHAR(50) NOT NULL,
foobar_roleCHAR(4) NOT NULL
CHECK(foobar_role IN (..)),
dept_idCHAR(10) NOT NULL REFERENCES (Departments)
);
Unfortunately,this is still a mess. Have you ever read a book on RDBMS thatincluded the chapter on normalization? This table is not a realtable; users have a relationship with departments and departments arenot an attribute of a user! Think about it! Does a book grow out ofthe chest of an author? No, of course not! The author and his bookshave a relationship called authorship. Likewise personnel has arelationship with the departments to which each individual employeeis assigned (1:many relationship), called job assignment or whateveryour use it in your company.
Noticehow I use the CHECK() construct to assure data integrity. My rule isthat if the list of legal values and a constraint is short (whateverthat means currently) and static (whatever that currently means),then we use this construct. If the domain is dynamic or large, thenwe use a references. I seriously doubt that the departments in yourcompany should actually be in a references clause, but I needed toshow this for my example..
Noticehow I created the user ID as opposed to a count of physical insertionattempts on one machine, to one table on one product, etc. as you didin your original.
Please post DDL and follow ANSI/ISO standards when asking for help.
February 21, 2017 at 2:12 pm
Please disregard Joe Celko's rant. His design, although theorically correct, miss the physical design considerations. Using a CHAR(10) would add 6 bytes of storage to each row on Personnel and every time a column needs to reference this table or even more depending on the indexes. IDENTITY is part of the RDBMS, it's available and should be used appropriately.
The CHECK constraint is another subject that has been discussed with him and shown that it has many disadvantages.
He loves to berate about not following best practices, but there's so many things that can be improved in the code that he posts that I won't even start with the corrections.
February 21, 2017 at 8:37 pm
Joe I think you need to purchase a new keyboard. Your space bar seems to be broken. At least after berating the OP for an improper design you posted a complete disaster of a "correct" one. None of your code posted here would work and the lack of spaces in your text makes your entire post lose any credibility.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 22, 2017 at 6:11 am
Luis Cazares - Tuesday, February 21, 2017 2:12 PMPlease disregard Joe Celko's rant. His design, although theorically correct, miss the physical design considerations. Using a CHAR(10) would add 6 bytes of storage to each row on Personnel and every time a column needs to reference this table or even more depending on the indexes. IDENTITY is part of the RDBMS, it's available and should be used appropriately.
The CHECK constraint is another subject that has been discussed with him and shown that it has many disadvantages.
He loves to berate about not following best practices, but there's so many things that can be improved in the code that he posts that I won't even start with the corrections.
Let me second what Luis said. There is an IDENTITY and it makes an excellent clustering key. It's what's known as an artificial key because it has nothing to do with the data. The other, a natural key, identifies the row just by being what it is. Granted, the natural key you pick may not be any good a few years down the road and it'll almost certainly add more than 4 or 8 bytes like Luis said. When you consider that every row of every nonclustered index inherits the bytes of the clustering key, keeping the clustering keys small becomes important. Remember, you have to read them and they have to be in memory to be able to use them.
I speak practical a lot better than I speak theoretical.
February 22, 2017 at 8:21 am
Ed, I agree with you 100%. Mr. Celko posts in a manner that makes people feel disrespected, and frankly, I'm sick of seeing it. It's a shame this forum did not retain the ability to filter out the posts of certain users. Yeah, you MIGHT miss something, but in this case, nothing of value. "Standards" in a field like RDBMS's are guidelines, and not necessarily rigid rules that must be followed or the death penalty is applied. He really needs to get off his high horse and deal with the real world as it is, instead of how he thinks it should be. And maybe, ... just maybe, he should think first instead of just rigidly follow his own "rules". Note that his profile says he's an author. Hope he makes enough to get by, because as a contractor doing actual database work, he wouldn't last long.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
February 22, 2017 at 8:35 am
sgmunson - Wednesday, February 22, 2017 8:21 AMEd, I agree with you 100%. Mr. Celko posts in a manner that makes people feel disrespected, and frankly, I'm sick of seeing it. It's a shame this forum did not retain the ability to filter out the posts of certain users. Yeah, you MIGHT miss something, but in this case, nothing of value. "Standards" in a field like RDBMS's are guidelines, and not necessarily rigid rules that must be followed or the death penalty is applied. He really needs to get off his high horse and deal with the real world as it is, instead of how he thinks it should be. And maybe, ... just maybe, he should think first instead of just rigidly follow his own "rules". Note that his profile says he's an author. Hope he makes enough to get by, because as a contractor doing actual database work, he wouldn't last long.
LOL. You might look into Joe's resume before you say he wouldn't last long. 😉 From what everyone says he is the nicest person in real life. And he intentionally has an online persona of a derogatory arrogant bully. He has stated that is because of his way of thinking that coincides with some spiritual way of learning or some such rubbish. I think he truly means well but he has repeatedly been bashed by many people over the years to tone down his posts but to no avail. As they say, don't feed the trolls and they will eventually look for food elsewhere.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply