SQL Server Compact alter column size with constraint? - sql-server

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.

Related

Add primary key to the table coulmn(please see the attached image for more clarity)

Creating table without using SQL table feature:
I'm trying to create table going to "Tables","Create Table" feature. How do I add primary and unique keys using this feature.
Right click on the selected column and set the option primary key from the context menu.
It has been shown below.
The documentation covers this nicely: From Create Primary Keys
Using SQL Server Management Studio
To create a primary key
In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.
In Table Designer, click the row selector for the database column you want to define as the primary key. If you want to select multiple
columns, hold down the CTRL key while you click the row selectors for
the other columns.
Right-click the row selector for the column and select Set Primary Key.
Or, if you want to use Transact-sQL
Using Transact-SQL
To create a primary key in an existing table
The following example creates a primary key on the column
TransactionID in the AdventureWorks database.
ALTER TABLE Production.TransactionHistoryArchive
ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);
It also goes on to explain how you can use T-SQL to create a NONCLUSTERED Primary key, and then add a further CLUSTERED INDEX on the table:
To create a primary key with clustered index in a new table
The following example creates a table and defines a primary key on the
column CustomerID and a clustered index on TransactionID in the
AdventureWorks database.
-- Create table to add the clustered index
CREATE TABLE Production.TransactionHistoryArchive1
(
CustomerID uniqueidentifier DEFAULT NEWSEQUENTIALID()
, TransactionID int IDENTITY (1,1) NOT NULL
, CONSTRAINT PK_TransactionHistoryArchive1_CustomerID PRIMARY KEY NONCLUSTERED (CustomerID)
)
;
-- Now add the clustered index
CREATE CLUSTERED INDEX CIX_TransactionID ON Production.TransactionHistoryArchive1 (TransactionID);

POSTGRESQL- Make foreign key column point to another table

I'm using psql and I want to change one of the columns of my table.
At the moment this column is a foreign key of Table 2 but I would like to make it point to Table 3.
Is this possible or should I delete the column and add a new one?
There's no need to add & remove the column. You can remove/disable the constraint to one table and add it for the other table.
The command for doing the former is:
alter table Table1 drop constraint if exists name_of_constraint_on_Table_1_column
The command for doing the latter is:
alter table Table1
add constraint name_of_constraint_on_Table_1_column
foreign key (column) references Table3 (other_column) match full
You need to find the name of the foreign key constraint if you haven't named it explicitly. You can do so via the \d command:
\d Table1
You should read about alter table cause there's a lot of things you can do to change the table.

Drop column with PK constraint in empty table in a script

Scenario
A table in SQL Server has two or more columns, but the original column with the primary key constraint is no longer needed. So now you want to write a script to drop the original column w/ a PK constraint and put the PK constraint on a different column.
In this example, the table is empty.
Problem
You can't drop the first column without first dropping the PK constraint.
And you can't drop the PK constraint in SQL Server without the exact name of it. (more info here)
....But you don't know the automatically generated name of the PK constraint.
NOTE: If the table is not empty, see this solution:
SQL Server 2008 Script to Drop PK Constraint that has a System Generated Name
(In most cases, this is the best solution.)
Question
The above solution will work, but what is another way to script dropping a column with a PK constraint when you don't know the constraint's name in an empty table?
Another strategy -- besides figuring out the system generated name of the PK constraint so you can drop it as described here -- is to drop the empty table and recreate it without the original column with the primary key constraint, instead putting it on the new column.
To drop the column with an unknown PK constraint name:
Generate a script to drop the table and re-create it from scratch
Remove the OriginalColumn column from the CREATE TABLE query
Put the PK constraint on the NewColumn column in the script
Run the script to drop and re-create it without the original column -- effectively dropping OriginalColumn and "moving" the PK constraint from OriginalColumn to NewColumn
???
Profit!

Foreign Key constraint on set of rows of a table data 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.

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.

Resources