Alter clustered index column - sql

I have a clustered index on a table indexing on the text column. I want to switch that column with a different column like ID, how do I alter the index?
I can't drop and recreate because this is running on Azure and the table needs to have clustered index all the time.
The SQL command and the syntax for changing index columns in an index.
alter index ?

Try this:
create clustered index [your_index_name]
on [your_table]
([ID])
with (drop_existing = on)

You cannot alter a clustered index.
The only option is to drop it and re-create it with the new column.
In your case, you'll probably have to re-create the table with the new clustered index on ID and then copy the data over.

Related

Alter a table which has non-unique non-clustered index to add a column

I have a table named Person which already have few non-unique non-clustered index and one clustered index (based on primary key).
I have to write two queries:
I need to alter the table to add column birthplace. Do I need to check any index while writing the alter table person add birthplace varchar(128) not null?
My next requirement is I have another column named cityCode narvar(10). This I need to include in my existing non-unique, non-clustered index. How should I write my script? should I drop my index and recreate it?
Please suggest. I am able to find some related questions, but not clear about non-unique non-clustered index.
alter table person add birthplace varchar(128) not null
Be aware when you specify not null without default value you will get error. It's recommended to add default value. But in case you can not consider any, then add column as nullable then update it later. At the end make it NOT NULL using below code:
alter table person Alter column birthplace varchar(128) not null
And if you want to involve new column to an existing index then use CREATE INDEX with DROP_EXISTING option. The performance is more efficient. To do so,
generate a create index script then modify script by adding new column to the column list of index and then add this at the end of CREATE INDEX statement.
WITH (DROP_EXISTING = ON)

Why my mdf file from new filegroup not growing?

I create a draft database with 2 tables: dbo.D and dbo.F, next I create a new filegroup for dbo.F and a file for this.
USE DEV
ALTER DATABASE DEV
ADD FILEGROUP [BLOB]
ALTER DATABASE DEV
ADD FILE
(
NAME= 'blob',
FILENAME = 'D:\MS SQL\DB\blob.mdf'
)
TO FILEGROUP [BLOB]
Next, I drop clustered index and recrete it, specifying a filegroup name.
ALTER TABLE F
DROP CONSTRAINT [F_PK] WITH (MOVE TO BLOB)
ALTER TABLE F
ADD CONSTRAINT [F_PK] PRIMARY KEY CLUSTERED
(
ID
)
WITH (IGNORE_DUP_KEY = OFF) ON BLOB
CREATE UNIQUE CLUSTERED INDEX F_PK
ON dbo.F(ID)
WITH DROP_EXISTING
ON [BLOB]
Next, I create more then 2k INSERT's queries and full in dbo.F with random binary data.
Question!
Why on this picture my new filegroup's file weighs so little unlike in the default filegroup's file?
Without seeing the full schema of your tables... you only have ID in your clustered index which means that all of the data you inserted is still in your primary filegroup. The only thing in blob is the index of your ID values which I would assume is not going to be anywhere near as large as the binary data you are inserting. I'm basing my assumption on ID being an INT column...
This, of course, is irrelevant if ID is the column in which you are storing your binary data, but I assume that's not the case if you're using it as a PK and clustered index.

Set identity off in azure sql table

I have a table which includes an identity column but i cant remove the identity property.
Is there a way to disable it? Or a way to make a copy of the entire table without identity property?
Note that you may not be able to drop the column if it referenced by a clustered index, and you can't drop all clustered indexes for a table because SqlAzure tables must always have a clustered index.
This means that you may have to jump through the following hoops (for at least your last clustered index, which may well be your primary key):
rename your clustered index
create a temp version of the table (with a new clustered index)
copy the data from the current table
drop the current table
rename the temp table to the current name
This roughly looks like this:
-- Rename clustered index
EXECUTE sp_rename N'PK_My_Current_PK', N'PK_My_Current_PK_OLD', 'OBJECT'
-- If you have any FK constraints on the table, then drop them
ALTER TABLE dbo.MyTable DROP CONSTRAINT FK_My_Foreign_Key
-- Create the new version of your table - because this is SQLAzure it must have a clustered index
CREATE TABLE dbo.tmp_MyTable (
MyID int NOT NULL,
CONSTRAINT PK_My_Current_PK PRIMARY KEY CLUSTERED (MyID)
)
-- Copy the data into the temp table from the old table
INSERT INTO dbo.tmp_MyTable (MyID)
SELECT MyID FROM dbo.MyTable
-- Drop the old table
DROP TABLE dbo.MyTable
-- Rename the new table
EXECUTE sp_rename N'tmp_MyTable', N'MyTable', 'OBJECT'
-- Recreate any foreign key constraints
ALTER TABLE dbo.MyTable WITH CHECK ADD FK_My_Foreign_Key FOREIGN KEY (MyID)
REFERENCES dbo.MyForeignTable (MyID)
Hope that helps
A
Edit: As #PhilBolduc pointed out SqlAzure tables require a clustered index, not a primary key. I've amended the terminology above accordingly - the principle of the answer still remains.
You can not remove an Identity column without dropping it unfortunately. Alternetivly add a new column with a temp name, update the new column value and then drop the previous column.
ALTER TABLE dbo.tablename ADD newcolumnname INT
UPDATE dbo.tablename SET newcolumnname = oldcolumnname FROM dbo.tablename
ALTER TABLE dbo.tablename DROP COLUMN oldcolumnname
that should do it. unless i have misunderstood your questions?

Drop Default Clustered Index Without Knowing it's Name

I wrote a script that creates some tables (it previously drops them if they exist) and then tries to create two indexes on each table.
The first index uses the Primary Key column to create a non-clustered index, and the second uses another column to create the clustered indexed. This is because the primary key column is a GUID instead of an int.
How can I drop the default index if I don't know it's name? or how can I specify a name for the primary key column index so I can drop it? Or better yet, how can I specify the 2 index i need right in the Create Table statement?
SELECT * FROM sys.indexes
However, I'm not understanding where in your process you actually have to drop an index.
You said you are creating some tables and then creating two indexes on each table.
If you are DROPping existing tables at the beginning, any indexes are automatically dropped.
There is no such thing as a default index.
Tables can either be heaps or clustered indexes. If you drop the clustered index, the table will be converted to a heap and any non-clustered indexes will have to be updated to point to the data in the unordered heap.
You can create like this all at once:
CREATE TABLE dbo.tbl
(
Id int NOT NULL IDENTITY (1, 1) CONSTRAINT UK_ID UNIQUE CLUSTERED,
SomeUUID UNIQUEIDENTIFIER NOT NULL CONSTRAINT PK_SomeUUID PRIMARY KEY NONCLUSTERED
)
Here's a SQLFiddle: http://sqlfiddle.com/#!6/d759e/12
You can define the two indices right after you create the table:
CREATE TABLE dbo.YourTable ( ...... )
GO
ALTER TABLE dbo.YourTable
ADD CONSTRAINT PK_YourTable PRIMARY KEY NONCLUSTERED (YourGuidColumn)
****************
this is crucial ! Otherwise, your PK will be clustered!
CREATE CLUSTERED INDEX IX01_YourTable ON dbo.YourTable(YourOtherColumn)
or even better:
CREATE UNIQUE CLUSTERED INDEX IX01_YourTable ON dbo.YourTable(YourOtherColumn)
That should create a non-clustered primary key and a separate (preferably unique) clustered index on a separate column.

converting clustered index into non-clustered index?

Is it possible to convert a clustered index to non clustered index or
non clustered index to clustered index in sql server 2005.
Please convert this query into clustered index:
create index index1 on mytable(firstcolumn)
Please convert this query into non clustered index:
create clustered index clusindex1 on mytable(cluscolumn)
There is more to it than meets the eye
to create a clustered index
drop index mytable.clusindex1
go
create clustered index clusindex1 on mytable(cluscolumn)
to create a non clustered index
drop index mytable.clusindex1
go
create index clusindex1 on mytable(cluscolumn) --non clustered is default
having said that, you can only have one clustered index per table, so if you try to drop an index and recreate it as a clustered index it will fail if you already have a clustered index. Whenever you drop a clustered index all non clustered indexes will also be dropped and recreated pointing to the heap, and then again dropped and recreated when you create the clustered index, now pointing to the clustered index (look up the WITH DROP_EXISTING clause)
I would say lookup how indexing works in Books On Line before you start dropping and recreating indexes
Those aren't queries; they are DDL commands.
Drop and recreate the indexes as desired, like so:
drop index mytable.index1
go
create nonclustered index index1 on mytable (firstcolumn asc)
go
I also wanted to know whether a clustered index could be converted (altered) to be a non-clustered index. I don't believe this can be done. The existing clustered index has to first be dropped and then the new non-clustered index (possibly with the same name as the clustered index) has to be created. The same is true for converting a non-clustered index to a clustered index.
I have no idea why you're asking for the 'queries' to be converted, but #Tahbaza is correct in that the code you included in your question aren't really queries. They are T-SQL statements for making changes to 'data definitions' (i.e. the schema [structure] of your database).