Why wouldn't I be able to delete this row? SQL 2008 - sql-server

I just have a table that has two rows of data and want to delete the rows and SQL Server Management Studio is choking on my delete big time. Here is the error pop up.
All I'm doing is highlighting the row and clicking the delete key. This error doesn't make any sense to me at all! The only way I've been able to get rid of the rows is to drop and recreate the tables but that isn't acceptable down the road when I only want to remove one or two rows.

You have to add Primary Key to your table. Run this two queries
First add a candidate column:
ALTER TABLE dbo.tbl_admin ADD TempID int IDENTITY(1, 1);
Then make this column the table primary key
ALTER TABLE dbo.tbl_admin
ADD CONSTRAINT pk_tbl_admin
PRIMARY KEY (TempId);

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.

Deleting a large number of rows by copying rows to keep into temporary table, dropping original table, and copying rows back

I have a table in an SQL Server 2012 database that has over 30 million rows, most of which I need to delete. However, deleting those rows takes far too long (we're talking multiple days).
The most likely solution I've found online is one where I use a SELECT INTO query to grab all the rows I want to keep and put them in a temporary table, the DROP the original table before using another SELECT INTO statement to repopulate the original table from the temporary table.
However, I've run into the problem where I can't drop the original table because it is referenced by foreign key constraints. I've used the following query:
SELECT * FROM [sys].[foreign_keys] WHERE referenced_object_id = object_id('OriginalTable')
To get the foreign key constraints, but when I try ALTER TABLE OriginalTable NOCHECK CONSTRAINT [Constraint name], I get a message saying that the constraint does not belong to the table.
My goal is to remove these 30 million rows without altering the state of my database, so I want to keep any foreign keys intact. Am I going about this the right way? If so, how can I drop the original table?
I've come up with what appears to be a working solution:
Step 1: I use a SELECT INTO statement to back up the desired rows into a temporary table.
Step 2: I use SQL Server Management Studio's "Script Key As" functions to make DROP and CREATE sql scripts for the foreign key constraints.
Step 3: I drop the foreign key constraints using the created scripts.
Step 4: I TRUNCATE the original table.
Step 5: I use an INSERT INTO statement to copy all rows from my temporary table back into the truncated original table.
Step 6: I recreate the foreign key constraints using the created scripts.
Step 7: I drop the temporary table.
I believe this effectively deletes the rows I don't want, and leaves the database in the same state it was in at the start of the process (minus the rows I don't want, of course).

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!

Creating multiple indexes on an existing table in Microsoft sql

I am converting some sql dump from mysql to run on microsoft sql, but I'm facing an issue with this query:
ALTER TABLE cms_download_subfolder ADD PRIMARY KEY (folder_id), ADD KEY parent_folder_id (parent_folder_id), ADD KEY client_id (client_id), ADD KEY folder_id (folder_id);
I get an error on the second Add: ADD KEY parent_folder_id (parent_folder_id).
I understand that in order to add alter a table and add in an index in mssql I would need a CREATE INDEX query, however is there a way that I can add multiple indexes to the same table instead of adding one by one?

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