How can I delete unique constraints of thousands of indices in psql? - database

I accidentally set alter:true in the ORM and now it's generated thousands of rows that look like this:
"users_email_key12076" UNIQUE CONSTRAINT, btree (email)
"users_email_key12077" UNIQUE CONSTRAINT, btree (email)
"users_email_key12078" UNIQUE CONSTRAINT, btree (email)
"users_email_key12079" UNIQUE CONSTRAINT, btree (email)
"users_email_key1208" UNIQUE CONSTRAINT, btree (email)
"users_email_key12080" UNIQUE CONSTRAINT, btree (email)
"users_email_key12081" UNIQUE CONSTRAINT, btree (email)
"users_email_key12082" UNIQUE CONSTRAINT, btree (email)
"users_email_key12083" UNIQUE CONSTRAINT, btree (email)
"users_email_key12084" UNIQUE CONSTRAINT, btree (email)
"users_email_key12085" UNIQUE CONSTRAINT, btree (email)
"users_email_key12086" UNIQUE CONSTRAINT, btree (email)
"users_email_key12087" UNIQUE CONSTRAINT, btree (email)
"users_email_key12088" UNIQUE CONSTRAINT, btree (email)
"users_email_key12089" UNIQUE CONSTRAINT, btree (email)
"users_email_key1209" UNIQUE CONSTRAINT, btree (email)
"users_email_key12090" UNIQUE CONSTRAINT, btree (email)
"users_email_key12091" UNIQUE CONSTRAINT, btree (email)
"users_email_key12092" UNIQUE CONSTRAINT, btree (email)
"users_email_key12093" UNIQUE CONSTRAINT, btree (email)
"users_email_key12094" UNIQUE CONSTRAINT, btree (email)
"users_email_key12095" UNIQUE CONSTRAINT, btree (email)
"users_email_key12096" UNIQUE CONSTRAINT, btree (email)
"users_email_key12097" UNIQUE CONSTRAINT, btree (email)
"users_email_key12098" UNIQUE CONSTRAINT, btree (email)
"users_email_key12099" UNIQUE CONSTRAINT, btree (email)
"users_email_key121" UNIQUE CONSTRAINT, btree (email)
"users_email_key1210" UNIQUE CONSTRAINT, btree (email)
"users_email_key12100" UNIQUE CONSTRAINT, btree (email)
"users_email_key12101" UNIQUE CONSTRAINT, btree (email)
"users_email_key12102" UNIQUE CONSTRAINT, btree (email)
I can only delete each index like this:
alter table users drop constraint users_email_key12001;
so I copied and pasted it onto the psql command line, and I'm only able to delete 18 lines at a time, on each command line. I may have thousands of these indices. any way to get rid of them all at once?

You can do it in a single query.
The idea is to identify the constraints to be removed, to build a list of command to remove them and to execute them. For this, you can use psql /gexec functionality.
SELECT format('alter table %I drop constraint %I;', rel.relname, conname)
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = 'public'
AND rel.relname = 'users'
AND conname ilike 'users_email_key%';\gexec
PS: there is no undo... you may want to print the commands (i.e. run without the \gexec flag) first.

Related

SQL Server : optimize constraint for perform a query bases on 2 columns

I am creating a table Brands with the following schema :
UserId
CarId
Brand
The UserId references the id of an user in the user table
The CarId references the id of a car in the car table
The only query that I will use is a search bases on these 2 columns, to get the corresponding brand.
So my question was about the constraint part, as I am a beginner, I would like to know which type of constraint to use (index, primary key, clustered or non clustered, on each field or on the 2 fields together) to have my query the more optimized possible.
This is my script right now :
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Brands]') AND type in (N'U'))
BEGIN
CREATE TABLE [Brands]
(
UserId BIGINT NOT NULL
CONSTRAINT [FK_Brands_Users]
FOREIGN KEY REFERENCES [Users] (UserId),
CarId BIGINT NOT NULL
CONSTRAINT [FK_Brands_Cars]
FOREIGN KEY REFERENCES [Cars] (CarId),
Brand nvarchar(15),
);
END
GO
ALTER TABLE [Brands]
ADD CONSTRAINT [Unique_UserId_BrandId] UNIQUE(UserId, BrandId);
GO
I would create a primary key including both fields. When you define a primary key it automatically create a clustered index. Also your primary key has a unique constraint build in. Your table is now a heap the way you wrote it here above which is not good. You can additionally create an extra non-clustered index on CarId. Having an additional non-clustered index on UserId is not usefull I think. The column UserId can use the clustered index because it's the first field in the clustered index but I'm not sure about that.

Are foreign keys that are part of a composite key automatically indexed in Oracle?

For instance, if I have a students table:
CREATE TABLE student (
student_id NUMBER(8),
student_name VARCHAR2(30),
CONSTRAINT pk_student PRIMARY KEY (student_id)
);
And a subject table:
CREATE TABLE subject (
subject_id NUMBER(8),
subject_name VARCHAR2(30),
CONSTRAINT pk_subject PRIMARY KEY (subject_id)
);
And then I create a third table of student's favorites subjects:
CREATE TABLE fav_sub (
student_id NUMBER(8),
subject_it NUMBER(8),
CONSTRAINT pk_fav_sub PRIMARY KEY (student_id, subject_id),
CONSTRAINT fk_1_fav_sub FOREIGN KEY (student_id) REFERENCES student(student_id),
CONSTRAINT fk_2_fav_sub FOREIGN KEY (subject_id) REFERENCES subject(subject_id)
);
Do I then need to manually create indexes for the foreign keys in the fav_sub table such as:
CREATE INDEX in_1_fav_sub ON fav_sub(student_id);
CREATE INDEX in_2_fav_sub ON fav_sub(subject_id);
Or are the indexes for the foreign keys automatically created by the database, since they're part of the composite key?
Edit: Just to clarify, I'm not asking if an index for a foreign key is automatically created, I'm asking if an index for a foreign key is created WHEN it's part of a composite key, since Oracle automatically indexes primary keys.
The creation of the primary key indexes the combination of [student_id, subject_id]. Adding foreign key constraints to each individual column does not create an index on them, and if you want it (which is probably a good idea), you'd have to manually create it.

Two connected tables in which same record has same Foreign Key

I have a SQL table Users with some users with PK's. I need to create second table UserInfo with same users from table Users. I am connect these tables with FK's. The difficulty is that I need to both table's users have SAME PRIMARY KEY. Like User "Peter" with (UserId 5) MUST have (UserInfoId 5) in UserInfo table . Is that possible and if it is, how can i do that?
Either there is a one to one relationship between Users and UserInfo, in that case UserInfo.UserID is PRIMARY KEY and FOREIGN KEY at the same time.
CREATE TABLE UserInfo (
UserID int PRIMARY KEY,
Info varchar(max),
CONSTRAINT FK_UserInfo_User FOREIGN KEY (UserID) REFERENCES Users(UserID)
ON DELETE CASCADE
);
OR
You have a one to many relationship between Users and UserInfo, in that case UserInfo.UserID is a FOREIGN KEY and you need a separate PRIMARY KEY UserInfo.UserInfoID.
CREATE TABLE UserInfo (
UserInfoID int PRIMARY KEY,
UserID int NOT NULL,
Info varchar(max),
CONSTRAINT FK_UserInfo_User FOREIGN KEY (UserID) REFERENCES Users(UserID)
ON DELETE CASCADE
);

Should my intermediate table contain a composite PK or a unique index

Let's say I have a Products table, ProductsCategory table and a Category table.
The ProductsCategory table has two columns: ProductID and CategoryID. Should I be using a composite primary key or a unique index on the two columns?
Additionally if I use the index, I can make it a unique index or a key.
Might as well use a composite key - no need to add a unique index when you already have the uniqueness semantics of a composite primary key.
You must create two way foreign key both table with on delete cascade option.
Because if you delete one of categories then it must remove relational rows on ProductCategory.
i mean you can use like this :
alter table ProductsCategory add constraint ForeignKey1 foreign key (ProductId) references Products (ID) ON DELETE CASCADE;
alter table ProductsCategory add constraint ForeignKey2 foreign key (CategoryId) references Category (ID) ON DELETE CASCADE;

Adding foreign key constraint to 'partial' primary key in PostgreSQL

I am trying to add a foreign key constraint to a table in my PostgreSQL 8.4 database, but it is failing as the target field, though part of a multi-column primary key, is not in itself unique.
The database has the following structure:
Table 1 (names of primary IDs):
PrimaryType, Name
[Primary key = "PrimaryType"]
Table 2 (names of child IDs for each type of primary ID):
PrimaryType, SubType, Name
[Primary key = "PrimaryType, SubType"]
[Foreign key = "Table2.PrimaryType = Table1.PrimaryType"]
Table 3 (logs which include a primary and child ID):
PrimaryType, SubType, DATA1, DATA2, ..., DATAN
[Foreign key = "Table3.PrimaryType = Table1.PrimaryType" AND "Table3.SubType = Table2.SubType"]
Obviously, the second part of the foreign key for table 3 is what is causing the problem. I just need to ensure that the primary and subtype ID pair in the log is a valid combination.
Thanks in advance.
For table 3's foreign key, change:
foreign key PrimaryType references Table1(PrimaryType)
foreign key SubType references Table2(Subtype)
to
foreign key (PrimaryType, SubType) references Table2(PrimaryType, SubTYpe)

Resources