How to remove identity on primary key column in SQL Server 2016 [closed] - sql-server-2016

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How to remove identity on primary key column in SQL Server 2016?

Create a new column, copy the data, drop original column
alter table yourtable
add newcolumn int
update yourtable
set newcolumn=oldcolumn
alter table yourtable
drop column oldcolumn

you can't remove Identity on a primary key column. It is by default gets created when you choose a column as Primary. You need to do alternatives which I don't recommend on project tables.
1) Drop the primary key with cascade option (You will lose the links to child tables)
2)
- You need to create another column with a different name in the same table.
- copy that data into newly created column
- Delete the primary key column.
this will leave orphan records in child tables that are being referenced.

Related

How to put AUTO_INCREMENT in id (PRIMARY KEY) in SQL Server? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
enter image description hereI created a table in navicat using SQL Server. but I could not put the AUTO_INCREMENT on the id (primary key). I'm getting an error:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert the value NULL into column 'id', table 'excel_convert.dbo.mundipag_excel'; column does not allow nulls. INSERT fails.
As the database and the table (HUGE) are created... I need to change the id column and put the auto_increment in it! I'm not getting!
Omit the value for id in the INSERT command's VALUES clause. If you are using the syntax that lists columns, i,e, INSERT INTO table_name(id, order_id, ...) VALUES (...) do not specify the identity column name.
Just to clarify a few points, you have an existing table that has a primary key on the id column. There are a lot of rows already in the table which means that the id column already has values (as a primary key value cannot be NULL) from previous inserts as you said the table is huge.
You're trying to add auto increment functionality to an existing primary key. On the assumption that this is what you're trying to achieve, then unfortunately, you cannot alter a table to add identity functionality to an existing primary key column (and I'm nearly sure this extends to any column).
As potential solutions, you could add an additional column with the identity statement. If that doesn't suit, then you could rebuild the table by copying the contents from the existing table to another table with the primary key now set with the auto increment on the id column in the new table. However, you would need to be aware of any foreign keys in this instance as these will greatly complicate any potential solution.

SQL Riddle on relations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Give an example of a pair of relations with integrity constraints, and inserts into both of them such that, regardless of whichever insert is first, the integrity constraints are violated by the first insert, although they are satisfied after the second insert. (To handle such situations integrity constraints are only checked at the end of the transaction).
CREATE TABLE a (
id int,
other_id int REFERENCES b(id)
);
CREATE TABLE b (
id int,
other_id int REFERENCES a(id)
);
INSERT INTO a VALUES (1,1);
INSERT INTO b VALUES (1,1);

How to use primary key to many table SQL Server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to use one primary key to many table (foreign key) for e.g
Table 1:
id --P K
1
2
3
Table 2:
id -- F K
Table 3:
id -- F K
If I insert value to table 2 by using a stored procedure. it showing error
constraint "FK_tbl_table2_tbl_table1_reg". The conflict occurred in database "BPMC", table "dbo.tbl_table1", column 'ID'.
where's error...thanks..
sorry i made error in sp datatype size....
Check this:table2 ID Column should be same datatype of table1I could not add as a comment so add it in answer section.
To avoid this error from happening, make sure that the value you are inserting into a column that references another table exists in that table. If the value does not exist in the primary table, insert to that table first before doing the insert on the second table.
Referential Integrity

Add multiple values in table's column against specific id [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone tell how can add multiple values against one id?
e.g I want to add multiple phone numbers against one id.
How can I do this? Give scenario should create two table or one?
Give Scenario should create two table or one?
Yes, you should create a separate table for this data, where multiple rows contain separate phone numbers. There would then be a foreign key on the id linking these rows back to the single row with a specific id in the first table.
create table T (
ID char(8) not null,
/* other columns */
constraint PK_T PRIMARY KEY (ID)
)
create table PhoneNumbers (
ID char(8) not null,
PhoneType varchar(12) not null,
Number varchar(15) not null,
constraint PK_PhoneNumbers PRIMARY KEY (PhoneNumbers,PhoneType),
constraint CK_PhoneNumber_Types CHECK (PhoneType in ('Home','Work','Mobile')),
constraint FK_PhoneNUmbers_T FOREIGN KEY (ID) references T(ID)
)

Unique index on nullable column [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a table with many records. Column X is nullable and a lot of records have NULL in this column. Can I create a UNIQUE non-clustered index on this column? Aren't those null values violating the unique constraint?
If you're no SQL Server 2008 or newer - you can set up a filtered, unique index to exclude / ignore all the NULL values.
CREATE UNIQUE NONCLUSTERED INDEX uixYourIndexName
ON dbo.YourTableName(YourColumnName)
WHERE YourColumnName IS NOT NULL
That would essentially "filter out" all NULL values - not including them into the index at all. Any query that uses the same WHERE clause can use this filtered index to find those rows.
Without the filtered index, you could not create an UNIQUE INDEX on your column since with a UNIQUE index, you're only allowed to have a single row that has NULL in that column.