Foreign Key constraint on set of rows of a table data sql server - sql-server

I have a ValidationTable like
and OrderProcessing table like
I want to create foreign key constraint on OrderProcessing table on column OrderType that references first 3 rows of ValidationTable (of ValidationType = "orders"). Is this possible? I referred similar questions but those did not really help.

I don't believe it is possible to link a specific set of rows.
The constraint has to be for the whole column.

Related

Adding a primary key column to an existing table in SQL Server?

I need to add a new column and make it the primary key in SQL Server; the table in question does not yet have a unique key column.
Here is the sample table http://www.sqlfiddle.com/#!18/8a161/1/0
My goal is simply to have a column ID and insert values from 1 to 1160 (total number of records in this table) and make that the primary key. Also is there a way to automatically add the numbers from 1-1160 without adding each record one by one since there are 1000+ rows in this table?
Thank you!
Simply alter the table and add the column with the appropriate characteristics.
alter table x add id smallint identity(1,1) not null primary key;
Thrown together quickly and you probably should get in the habit of naming constraints. fiddle. I will note that you may or may not want to use an identity column - think carefully about your actual goal and how you want to continue using this table after the alteration.

Change Primary Key with custom identity seed on existing table SQL Server

I very new in SQL Server it's about 3 months. Now, I am stuck with a problem.
I need to change the primary key on a table with lots of data, about 10000 rows. Some other tables have relationship with this table (FK), but some tables stand alone. i wanna change the primary key and change the start identity seed with i want.
I have browsing in google but still no luck.
Can someone in here give me solution.
Thanks..
1)To DROP a PRIMARY KEY Constraint
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
2)Alter table with new primary constarint
ALTER TABLE Persons
ADD PRIMARY KEY (pk_PersonID)

How can I have 2 foreign keys in SQL Server 2008?

Simple question but seems very difficult for me. I need 2 foreign keys in my table and after, will use query builder to get specific columns that I need.
Here is an image:
Currently, the ProductID already has a relation with my Products table. What I want now is to have another relation with my CustomerID to my CustomerProducts table. Any idea or reference on how to do it?
Why not just use code to create the foreign keys?
ALTER TABLE YourTable
ADD CONSTRAINT NameYourFK FOREIGN KEY (YourColumn) REFERENCES YourOtherTable (YourOtherIdentityColumn)

How do you add a unique primary key field automatically in SQL Server?

I am using SQL Server 2012 and need to add a column with a unique primary key. I am about to load several hundred thousand records BULK and just discovered repetition in the field I was going to use. Have seen SEQUENCE and GUID. Need some guidance on the best choice and how to go about setting this up so that the key field is populated during the bulk load.
When you create your table in which you want to insert information create an IDENTITY column. That will serve as an auto-populating column with a unique number for each record.
Here is a link that might help you.
If you have already created your table just change this query to what suits to your table name and run it in order to add the new column you requested.
ALTER TABLE mytable
ADD COLUMN unique_id IDENTITY (1,1)
Just a slight update on what’s already posted that includes details for adding primary key constraint
alter table database.schema.table_t
add ID_column int identity(1,1)
primary key (ID_column)
If you already set the primary key on this table just go and remove it before you execute this statement.

SQL Server Compact alter column size with constraint?

I am trying to modify the size of a column in a table, however that column is a primary key column and is used in other table ( which would need to have its size modified too)
I have a table called table1 with a column named column1 as primary key
I also have table2,table3 and table4 that has table2column1, table3column1 and table4column1 respectively.
table2column1,table3column1 and table4column1 are foreign keys ( reference column1 from table1) and they are also used as a composite primary key in their respective table.
I tried doing this to alter the size of the column
ALTER TABLE UtilisateurNotes ALTER COLUMN IDNotes nvarchar(250)
It did not work.
This is the error message : Cannot alter a column that is part of a key or an index.
Anyone has an idea what I should do? thank you Gibit
You have to drop the foreign keys and primary key index in order to modify the column and then rebuild them after.
If you're in SQL Server Management Studio and you use the Object Explorer then you can right click on the specific Primary Key or Foreign Key specified in the error message and use Script as > CREATE AND DROP to help you generate the necessary scripts for the update.

Resources