SQL Server add primary key - sql-server-2005

I have a table that needs to be given a new primary key, as my predecesor used a varchar(8) row as the primary key, and we are having problems with it now. I know how to add the primary key, but am not sure of the correct way to add this new primary key to other tables that have the foreign key. Here is what I have:
users table:
old_user_id varchar(8)
...
...
new_user_id int
orders table:
order_id int
...
...
old_user_fk varchar(8)
new_user_fk int(11)
I need to get the same results whether I join the tables on users.old_user_id=orders.old_user_fk or users.new_user_id=orders.new_user_fk. Any help is appreciated.

What is int(11)? SQL Server only has tinyint, smallint, int and bigint
I would suggest you add the new columns to all the tables then update the values so that they match....run a couple of queries to make sure it all works. drop the PK and FK constraints and add new PK and FK constraints using the new columns
of course I would back up all these tables just in case
select * into backup_ orders from orders
This way you always have that data in case you need to roll back

Replace your varchar with int, do not keep duplicates in the same table.
Write some TSQL similar this code I used recently:
BEGIN TRANSACTION
-- temp tables to hold data
DECLARE #HeaderTemp table
(vid varchar(8),
[Name] varchar (50) )
DECLARE #SecondaryTemp table
(vid_fk varchar(8),
[Value] varchar (50) )
-- store table data
INSERT INTO #HeaderTemp
SELECT * FROM [Header]
INSERT INTO #SecondaryTemp
SELECT * FROM [Secondary]
-- empty data from tables
DELETE FROM [Secondary]
DELETE FROM [Header]
-- drop constraints
ALTER TABLE [SECONDARY] DROP CONSTRAINT [FK_SECONDARY_HEADER]
ALTER TABLE [dbo].[HEADER] DROP CONSTRAINT [PK_HEADER]
-- convert varchar to int
ALTER TABLE [SECONDARY] ALTER COLUMN VID_FK INT NOT NULL
ALTER TABLE [HEADER] ALTER COLUMN VID INT NOT NULL
-- re-create constraints
ALTER TABLE [dbo].[HEADER] ADD CONSTRAINT [PK_HEADER] PRIMARY KEY CLUSTERED
(
[vid] ASC
)
ALTER TABLE [dbo].[SECONDARY] WITH CHECK ADD CONSTRAINT [FK_SECONDARY_HEADER]
FOREIGN KEY([vid_fk]) REFERENCES [dbo].[HEADER] ([vid])
-- put data back
INSERT INTO [Header]
SELECT * FROM #HeaderTemp
INSERT INTO [Secondary]
SELECT * FROM #SecondaryTemp
COMMIT TRANSACTION

Related

How to index a SQL table on a column in another table

I have the schema below - Let's pretend that there are 2 countries, A and B.
Country A has 1000 teams whereas country B has 100,000,000 - If I want to quickly query results based off which country the team is in, how would I construct my index?
Teams cannot change country if that helps.
Indexing a table depend upon knowing real schema.
For this simple table schema, I will create only Trusted FK between tables, at least this will be my first try.
Assuming Countryid,Teamid,Resultid are auto increment.
CREATE TABLE Country
(
id INT IDENTITY(1, 1) PRIMARY KEY,
CountryName VARCHAR(100) NOT NULL
);
CREATE TABLE Team
(
id INT IDENTITY(1, 1) PRIMARY KEY,
TeamName VARCHAR(100) NOT NULL,
CountryID INT NOT NULL
);
ALTER TABLE dbo.Team WITH CHECK
ADD CONSTRAINT FK_Team_CountryID
FOREIGN KEY(CountryID) REFERENCES dbo.Country(id);
ALTER TABLE dbo.Team WITH CHECK
CHECK CONSTRAINT FK_Team_CountryID;
--Just verify that newly created FK is trusted or not.
SELECT
name,
is_disabled,
is_not_trusted
FROM
sys.foreign_keys
WHERE
name = 'FK_Team_CountryID';
CREATE TABLE Result
(
id INT IDENTITY(1, 1) PRIMARY KEY,
TeamId INT NOT NULL,
Result INT NOT NULL
);
-- I have no idea how you are storing Result,so ignore it
ALTER TABLE dbo.Result WITH CHECK
ADD CONSTRAINT FK_Result_TeamId
FOREIGN KEY(TeamId) REFERENCES dbo.Team(id);
ALTER TABLE dbo.Result WITH CHECK
CHECK CONSTRAINT FK_Result_TeamId;
May be after seeing query plan of real query, I will De-normalise Result table to add Countryid , but for now it is not require since country table will be small

How to redefine the constraint in a field?

the table was built as follow
create table schema1.detail(
ornum char(6) foreign key references schema1.menu(ornum),
num int,
pdname char(20),
price money check(price>0) default null,
amount int check(amount>0) not null,
primary key (ornum,num)
)
and I want to redefine the field 'amount' as amount>0 and amount<=1000 not null
but I don't know how to do it.
Well, if you already had data in your table you would have needed to keep it so you would have needed to first find out the name of the constraint (since your create script does not provide names to the constraints, SQL Server does it automatically for you), then drop the constraint and create a new one.
To do that, you would need to do something like this to find out the name of the constraints in your table:
SELECT TABLE_NAME,
COLUMN_NAME,
CHECK_CLAUSE,
cc.CONSTRAINT_SCHEMA,
cc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
ON cc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
WHERE TABLE_NAME = 'detail'
AND cc.CONSTRAINT_SCHEMA = 'schema1'
ORDER BY CONSTRAINT_SCHEMA,
TABLE_NAME,
COLUMN_NAME
Then you need to drop the constraint and recreate it:
ALTER TABLE dbo.detail
DROP CONSTRAINT <constraint name> -- you got from the query above
ALTER TABLE detail
ADD CONSTRAINT CK_detail_amount CHECK(amount>0 AND amount<1000)
However, since this is a brand new table, I would suggest drop the table all together and re-create it, this time with proper names for the constraints:
drop table schema1.detail;
create table schema1.detail (
ornum char(6),
num int,
pdname char(20),
price money null, -- there is no need for the default keyword here...
amount int check(amount>0) not null,
constraint PK_detail primary key (ornum,num),
constraint FK_detail_menu foreign key (ornum) references schema1.menu(ornum),
constraint CK_detail_price check(price>0),
constraint CK_detail_amount check(amount>0 and amount <1000)
);
It's best practice to give your constraints meaningful names.
UPDATED
alter table schema1.detail drop constraint [yourConstraintName] ;
alter table schema1.detail add constraint [yourConstraintName] check (amount>0 and amount < 1000)
check constraint like "fieldname BETWEEN 0 and 1000"
or
amount int check (amount>0 AND amount <1000) not null,

SQL Script Invalid Column name

BEGIN TRANSACTION
BEGIN TRY
DECLARE #tblRCatOmitTemp TABLE (ID INT IDENTITY(1,1),
RCatId int NOT NULL,
CountryId int NOT NULL)
INSERT INTO #tblRCatOmitTemp
SELECT RCatId, CountryId
FROM dbo.tblRCatOmit TABLOCKX
DROP TABLE dbo.tblRCatOmit
CREATE TABLE dbo.tblRCatOmit(
ID INT IDENTITY(1,1),
RCatId INT NOT NULL,
CountryId INT NOT NULL) ON [PRIMARY]
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)
REFERENCES dbo.tblRCat (RCatID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)
REFERENCES dbo.tblCountry (CountryID)
SET IDENTITY_INSERT dbo.tblRCatOmit ON
INSERT INTO
dbo.tblRCatOmit(
ID,
RCatID,
CountryID)
SELECT
ID,
RCatID,
CountryID
FROM
#tblRCatOmitTemp
SET IDENTITY_INSERT dbo.tblRCatOmit OFF
COMMIT TRANSACTION tblRCatOmit;
END TRY
BEGIN CATCH
PRINT N'Something Happend!'
ROLLBACK;
END CATCH
I have to add a new IDENTITY field as well as PKs and FKs's to a table. I'm trying to save the data into a temp table, drop the original table and then recreate it.
When I run this script I get an error:
Invalid column name 'ID'
I'm guessing it is doing a pre-compile and letting me know that ID doesn't exist in the current table.
What do I need to do to modify this script so it runs?
When I double click the Invalid column name 'ID' message it brings me to this part of the script:
INSERT INTO
dbo.tblRCatOmit(ID,
RCatID,
CountryID)
BEGIN TRANSACTION
BEGIN TRY
IF OBJECT_ID('tempdb..#tblRCatOmitTemp') IS NOT NULL
BEGIN
DROP TABLE #tblRCatOmitTemp
END
CREATE TABLE #tblRCatOmitTemp (
RCatId int NOT NULL,
CountryId int NOT NULL)
INSERT INTO #tblRCatOmitTemp
SELECT RCatId, CountryId
FROM dbo.tblRCatOmit TABLOCKX
DROP TABLE dbo.tblRCatOmit
CREATE TABLE dbo.tblRCatOmit(
ID INT IDENTITY(1,1),
RCatId INT NOT NULL,
CountryId INT NOT NULL) ON [PRIMARY]
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)
REFERENCES dbo.tblRCat (RCatID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)
REFERENCES dbo.tblCountry (CountryID)
INSERT INTO
dbo.tblRCatOmit(
RCatID,
CountryID)
SELECT
RCatID,
CountryID
FROM
#tblRCatOmitTemp
COMMIT TRANSACTION tblRCatOmit;
END TRY
BEGIN CATCH
PRINT N'Something Happend!'
ROLLBACK;
END CATCH
If you have any amount of data at all use a Temp Table not a table variable, because performance will be a lot better!
Next don't build the ID on the temp table it is arbitrary if you do that and on your insert back to your newly recreated table it will build it there for you. Also means no need to set identity insert on.
If you don't want to use a temp table and add that insert you could also use sp_rename to rename your existing table....
And if you don't care about ordinal position of the columns then simply add the column and then the keys.
ALTER TABLE dbo.tblRCatOmit
ADD ID INT IDENTITY(1,1)
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT PK_RCatOmit PRIMARY KEY CLUSTERED (ID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_rcatId FOREIGN KEY (RCatId)
REFERENCES dbo.tblRCat (RCatID);
ALTER TABLE dbo.tblRCatOmit
ADD CONSTRAINT FK_RCatOmit_countryId FOREIGN KEY (CountryId)
REFERENCES dbo.tblCountry (CountryID)

How to change data type of columns without dropping it

CREATE TABLE DEPARTMENT(
DEPARTMENT_NAME VARCHAR(25) NOT NULL PRIMARY KEY ,
BUDGET_CODE INT NOT NULL,
OFFICE_NUM INT NOT NULL,
PHONE NUMERIC NOT NULL ,
);
I have this table that I am creating but i want to change budget code and office number in varchar instead of int. Is there a way to do this. this table is linked to other tables with foreign key. So i can't drop it and create a new one.
Try this
ALTER TABLE dbo.DEPARTMENT
ALTER COLUMN BUDGET_CODE VARCHAR(100)
ALTER COLUMN OFFICE_NUM VARCHAR(100)

How do I create a foreign key in SQL Server?

I have never "hand-coded" object creation code for SQL Server and foreign key decleration is seemingly different between SQL Server and Postgres. Here is my sql so far:
drop table exams;
drop table question_bank;
drop table anwser_bank;
create table exams
(
exam_id uniqueidentifier primary key,
exam_name varchar(50),
);
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint question_exam_id foreign key references exams(exam_id)
);
create table anwser_bank
(
anwser_id uniqueidentifier primary key,
anwser_question_id uniqueidentifier,
anwser_text varchar(1024),
anwser_is_correct bit
);
When I run the query I get this error:
Msg 8139, Level 16, State 0, Line 9
Number of referencing columns in
foreign key differs from number of
referenced columns, table
'question_bank'.
Can you spot the error?
And if you just want to create the constraint on its own, you can use ALTER TABLE
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn ) references MyOtherTable(PKColumn)
I wouldn't recommend the syntax mentioned by Sara Chipps for inline creation, just because I would rather name my own constraints.
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint fk_questionbank_exams foreign key (question_exam_id) references exams (exam_id)
);
You can also name your foreign key constraint by using:
CONSTRAINT your_name_here FOREIGN KEY (question_exam_id) REFERENCES EXAMS (exam_id)
I like AlexCuse's answer, but something you should pay attention to whenever you add a foreign key constraint is how you want updates to the referenced column in a row of the referenced table to be treated, and especially how you want deletes of rows in the referenced table to be treated.
If a constraint is created like this:
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn )
references MyOtherTable(PKColumn)
.. then updates or deletes in the referenced table will blow up with an error if there is a corresponding row in the referencing table.
That might be the behaviour you want, but in my experience, it much more commonly isn't.
If you instead create it like this:
alter table MyTable
add constraint MyTable_MyColumn_FK FOREIGN KEY ( MyColumn )
references MyOtherTable(PKColumn)
on update cascade
on delete cascade
..then updates and deletes in the parent table will result in updates and deletes of the corresponding rows in the referencing table.
(I'm not suggesting that the default should be changed, the default errs on the side of caution, which is good. I'm just saying it's something that a person who is creating constaints should always pay attention to.)
This can be done, by the way, when creating a table, like this:
create table ProductCategories (
Id int identity primary key,
ProductId int references Products(Id)
on update cascade on delete cascade
CategoryId int references Categories(Id)
on update cascade on delete cascade
)
create table question_bank
(
question_id uniqueidentifier primary key,
question_exam_id uniqueidentifier not null constraint fk_exam_id foreign key references exams(exam_id),
question_text varchar(1024) not null,
question_point_value decimal
);
--That will work too. Pehaps a bit more intuitive construct?
To Create a foreign key on any table
ALTER TABLE [SCHEMA].[TABLENAME] ADD FOREIGN KEY (COLUMNNAME) REFERENCES [TABLENAME](COLUMNNAME)
EXAMPLE
ALTER TABLE [dbo].[UserMaster] ADD FOREIGN KEY (City_Id) REFERENCES [dbo].[CityMaster](City_Id)
If you want to create two table's columns into a relationship by using a query try the following:
Alter table Foreign_Key_Table_name add constraint
Foreign_Key_Table_name_Columnname_FK
Foreign Key (Column_name) references
Another_Table_name(Another_Table_Column_name)
Like you, I don't usually create foreign keys by hand, but if for some reason I need the script to do so I usually create it using ms sql server management studio and before saving then changes, I select Table Designer | Generate Change Script
This script is about creating tables with foreign key and I added referential integrity constraint sql-server.
create table exams
(
exam_id int primary key,
exam_name varchar(50),
);
create table question_bank
(
question_id int primary key,
question_exam_id int not null,
question_text varchar(1024) not null,
question_point_value decimal,
constraint question_exam_id_fk
foreign key references exams(exam_id)
ON DELETE CASCADE
);
Necromancing.
Actually, doing this correctly is a little bit trickier.
You first need to check if the primary-key exists for the column you want to set your foreign key to reference to.
In this example, a foreign key on table T_ZO_SYS_Language_Forms is created, referencing dbo.T_SYS_Language_Forms.LANG_UID
-- First, chech if the table exists...
IF 0 < (
SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'T_SYS_Language_Forms'
)
BEGIN
-- Check for NULL values in the primary-key column
IF 0 = (SELECT COUNT(*) FROM T_SYS_Language_Forms WHERE LANG_UID IS NULL)
BEGIN
ALTER TABLE T_SYS_Language_Forms ALTER COLUMN LANG_UID uniqueidentifier NOT NULL
-- No, don't drop, FK references might already exist...
-- Drop PK if exists
-- ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT pk_constraint_name
--DECLARE #pkDropCommand nvarchar(1000)
--SET #pkDropCommand = N'ALTER TABLE T_SYS_Language_Forms DROP CONSTRAINT ' + QUOTENAME((SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
--WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
--AND TABLE_SCHEMA = 'dbo'
--AND TABLE_NAME = 'T_SYS_Language_Forms'
----AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms'
--))
---- PRINT #pkDropCommand
--EXECUTE(#pkDropCommand)
-- Instead do
-- EXEC sp_rename 'dbo.T_SYS_Language_Forms.PK_T_SYS_Language_Forms1234565', 'PK_T_SYS_Language_Forms';
-- Check if they keys are unique (it is very possible they might not be)
IF 1 >= (SELECT TOP 1 COUNT(*) AS cnt FROM T_SYS_Language_Forms GROUP BY LANG_UID ORDER BY cnt DESC)
BEGIN
-- If no Primary key for this table
IF 0 =
(
SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'T_SYS_Language_Forms'
-- AND CONSTRAINT_NAME = 'PK_T_SYS_Language_Forms'
)
ALTER TABLE T_SYS_Language_Forms ADD CONSTRAINT PK_T_SYS_Language_Forms PRIMARY KEY CLUSTERED (LANG_UID ASC)
;
-- Adding foreign key
IF 0 = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME = 'FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms')
ALTER TABLE T_ZO_SYS_Language_Forms WITH NOCHECK ADD CONSTRAINT FK_T_ZO_SYS_Language_Forms_T_SYS_Language_Forms FOREIGN KEY(ZOLANG_LANG_UID) REFERENCES T_SYS_Language_Forms(LANG_UID);
END -- End uniqueness check
ELSE
PRINT 'FSCK, this column has duplicate keys, and can thus not be changed to primary key...'
END -- End NULL check
ELSE
PRINT 'FSCK, need to figure out how to update NULL value(s)...'
END
I always use this syntax to create the foreign key constraint between 2 tables
Alter Table ForeignKeyTable
Add constraint `ForeignKeyTable_ForeignKeyColumn_FK`
`Foreign key (ForeignKeyColumn)` references `PrimaryKeyTable (PrimaryKeyColumn)`
i.e.
Alter Table tblEmployee
Add constraint tblEmployee_DepartmentID_FK
foreign key (DepartmentID) references tblDepartment (ID)