Creating multiple indexes on an existing table in Microsoft sql - sql-server

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?

Related

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

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);

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.

Make a varchar(50) column unique

I have a column (which represents an e-mail) in a SQL Server database with varchar(50) as data type and I would like to make it unique (do not allow the same two e-mail addresses). I cannot find a way to make such column unique in SQL Server Management Studio.
How to do that?
In T-SQL it would be
ALTER TABLE MyTable WITH CHECK
ADD CONSTRAINT UQ_MyTable_Email UNIQUE (EmailAddress)
Or as an explicit index
CREATE UNIQUE INDEX IXU_Email ON MyTable (EmailAddress)
Edit: I can't see how to create a constraint in the SSMS GUI: other answers show how to manage indexes. I do only use SQL though, never the GUI for this kind of work
In the Object Explorer under the table right-click the Indexes folder and choose New Index....
In the window that appears enter Index name:, tick the Unique checkbox and add your email field from the Add... button then click OK.
Try this:
ALTER TABLE [dbo].[TableName] ADD CONSTRAINT UNQ__TableName__ColumnName UNIQUE ([ColumnName])
From this:
http://msdn.microsoft.com/en-us/library/ms191166.aspx

Alter Table Drop Column Failed

I'm trying to clean up a database table and I'd really like to drop two columns as they should no longer be being used.
'Property' table
- Unable to modify table.
The index '_dta_index_Property_7_669245439__K1_K9_K8_K24_K4_2_5_6_13_22_25_26_29' is dependent on column 'AveragePriceMta'.
The index '_dta_index_Property_7_669245439__K1_K9_K8_K24_2_4_5_6_7_13_22_25_26_29' is dependent on column 'AveragePriceMta'.
ALTER TABLE DROP COLUMN AveragePriceMta failed because one or more objects access this column.
I've gone and looked at the indexes for this table found the particular columns I want to delete in a greyed out field of "Included columns". Obviously I don't want to just drop these indexes - but is there a way to refresh the index so that I can remove the columns in question out of the non-editable included columns field?
Using SQL Server 2008 but database is 2005.. in case that matters.
Thanks for your help! :)
You can't add or remove columns to an Index. You will have to drop the index and the re-create it.
You can you use Create Index along with the Drop_Existing clause to do this.
MS Help on Create Index

SQL Server Create multiple foreign keys

The table contains a PRIMARY KEY column and another column which is FOREIGN KEY. This works fine. When I attempt to add another column as a FOREIGN KEY I get the following message:
Link to pic
This is unrelated to the PKs or FKs.
There is a setting in SQL Server 2008 SSSM to prevent table rebuilds via the designer.
Tools..Options..Designers... clear the option “Prevent saving changes that require table re-creation”.

Resources