Are indexes on temporary tables deleted when the table is deleted? - tsql

Would the following SQL remove also the index - or does it have to be removed separately?
CREATE TABLE #Tbl (field int)
CREATE NONCLUSTERED INDEX idx ON #Tbl (field)
DROP TABLE #Tbl

Yes they are. You can search in MSSQL help for CREATE INDEX article it is said there:
"Indexes can be created on a temporary
table. When the table is dropped or
the session ends, all indexes and
triggers are dropped."

It will be removed automatically, as there is nothing left to index. Think of it as a child object in this respect.

The drop table will remove the index. Drop Index takes the index name and the table name.
In this case would be DROP INDEX idc ON #tbl
which can be called if you want to drop the index but leave the table.

Related

Can't drop indexes with schema name

Created index on partition table movies.actors_2010(name) with schemaname movies.actors_2010_name_idx
CREATE INDEX CONCURRENTLY "movies.actors_2010_name_idx" ON movies.actors_2010(name);
Now when I try to drop the index, output shows index doesn't exist and index remain same, can't drop it.
drop index IF EXISTS movies.actors_2010_name_idx;
Any suggestions are helpful.
The problem appears to be that you quoted the identifier "movies.actors_2010_name_idx". That created an index with a . in the name, not an index named "actors_2010_name_idx" in the movies schema. To do that, you should have used
CREATE INDEX CONCURRENTLY "movies"."actors_2010_name_idx" ON "movies"."actors_2010"(name);
or
CREATE INDEX CONCURRENTLY movies.actors_2010_name_idx ON movies.actors_2010(name);
Now to do delete the index with the broken name, you'd use
DROP INDEX IF EXISTS "movies.actors_2010_name_idx";
or possibly
DROP INDEX IF EXISTS "movies"."movies.actors_2010_name_idx";

Delete duplicates from a huge table in Postgresql

I have an unusual problem: I need to delete duplicate records from a table in Postgresql. As i have duplicate records so i dont have primary key and unique index in this table. The table conatins like 20million records and it has duplicate records in it. While i am trying the below query it is taking too long time.
'DELETE FROM temp a using temp b where a.recordid=b.recordid and a.ctid < b.ctid;'
So what should be a better approach to handle such huge table with no index in it?
Appreciate for help.
if you have enough empty space, your can copy table without duplicates, then remove old table and rename new table
like this
INSERT INTO new_table
VALUES
SELECT
DISTINCT ON (column)
*
FROM old_table
ORDER BY column ASC
Use COPY TO to dump the table.
Then Unix sort -u to de-duplicate it.
Drop or truncate the table in Postgres, use COPY FROM to read it back in.
Add a primary key column.

Unable to drop index on a db2 table

I have a table MAIN_SCHEMA.TEST in which I created a Index on a column CHECK_ID.
CHECK_ID is also a FOREIGN_KEY constraint in TEST table.
This table contains only 50 records.
By Mistake the index got created in Default schema DEFAULT_SCHEMA.CHECK_ID_IDX.
CREATE INDEX DEFAULT_SCHEMA.CHECK_ID_IDX(CHECK_ID ASC);
So I am trying to drop this index but the drop query gets stuck for long time.
DROP INDEX DEFAULT_SCHEMA.CHECK_ID_IDX.
there are no locks on this table when I checked.
Instead of dropping and recreating the index with the right schema, could you just try to RENAME the index? It requires the existing SCHEMA.NAME pair together with the new as input. It will not move any data, but just update the metadata.

Will postgresql generate index automatically?

is there automatic index in Postgresql or need users to create index explicitly? if there is automatic index, how can I view it? thanks.
An index on the primary key and unique constraints will be made automatically. Use CREATE INDEX to make more indexes. To view existing database structure including the indexes, use \d table.
A quick example of generating an index would be:
CREATE INDEX unique_index_name ON table (column);
You can create an index on multiple columns:
CREATE INDEX unique_index_name ON table (column1, column2, column3);
Or a partial index which will only exist when conditions are met:
CREATE INDEX unique_index_name ON table (column) WHERE column > 0;
There is a lot more you can do with them, but that is for the documentation (linked above) to tell you. Also, if you create an index on a production database, use CREATE INDEX CONCURRENTLY (it will take longer, but not lock out new writes to the table). Let me know if you have any other questions.
Update:
If you want to view indexes with pure SQL, look at the pg_catalog.pg_indexes table:
SELECT *
FROM pg_catalog.pg_indexes
WHERE schemaname='public'
AND tablename='table';

How to add a new identity column to a table in SQL Server?

I am using SQL Server 2008 Enterprise. I want to add an identity column (as unique clustered index and primary key) to an existing table. Integer based auto-increasing by 1 identity column is ok. Any solutions?
BTW: my most confusion is for existing rows, how to automatically fill-in new identity column data?
thanks in advance,
George
you can use -
alter table <mytable> add ident INT IDENTITY
This adds ident column to your table and adds data starting from 1 and incrementing by 1.
To add clustered index -
CREATE CLUSTERED INDEX <indexName> on <mytable>(ident)
have 1 approach in mind, but not sure whether it is feasible at your end or not. But let me assure you, this is a very effective approach. You can create a table having an identity column and insert your entire data in that table. And from there on handling any duplicate data is a child's play. There are two ways of adding an identity column to a table with existing data:
Create a new table with identity, copy data to this new table then drop the existing table followed by renaming the temp table.
Create a new column with identity & drop the existing column
For reference the I have found 2 articles : http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/
http://cavemansblog.wordpress.com/2009/04/02/sql-how-to-add-an-identity-column-to-a-table-with-data/
Not always you have permissions for DBCC commands.
Solution #2:
create table #tempTable1 (Column1 int)
declare #new_seed varchar(20) = CAST((select max(ID) from SomeOtherTable) as varchar(20))
exec (N'alter table #tempTable1 add ID int IDENTITY('+#new_seed+', 1)')