I am real beginner when it comes to databases, so any advice is appriciated.
There must be a way to combine a UNIQUE constraint and a EXCLUSION constraint in PostgreSQL.
.
The one below written by me is incorrect. (Syntactically for sure, and possible logically, too)
I'm trying to achieve the following:
a single authenticator can belong to 1 useridx at a given time. (so a UNIQUE constraint is needed)
however the authenticator can belong to a different useridx at a different time (so some kind of EXCLUSION constraint is needed)
different time means outside of the interval of validform, and validuntil.
What I've tried:
ALTER TABLE authentication
ADD CONSTRAINT lenient_constraint UNIQUE (useridx, authenticator)
EXCLUDE USING gist(validfrom WITH =, validuntil WITH &&);
If you are asking if you can do it in a single alter table statement the answer is no. If you are asking if you can do it against the same table, the answer is yes. See below:
mydb# alter table test add unique(test);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "test_test_key" for table "test"
ALTER TABLE
mydb# alter table test add exclude (test with =);
NOTICE: ALTER TABLE / ADD EXCLUDE will create implicit index "test_test_excl" for table "test"
ALTER TABLE
Related
I want use a check constraint for a column in any table in SQL Server 2008.
I would like give a qualified name to the check constraint.
I have seen several syntax version on how to create it:
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
ALTER TABLE [dbo].[Roles2016.UsersCRM] WITH CHECK
ADD CONSTRAINT [CK_UsuariosCRM_Estado]
CHECK (([Estado]=(4) OR [Estado]=(3) OR [Estado]=(2) OR [Estado]=(1)))
What's difference ADD CHECK and ADD CONSTRAINT for a check constraint?
It is possible, but a very bad habit to add constraints without a name:
CREATE TABLE tbl(SomeColumn VARCHAR(10) DEFAULT('test'))
will create a CONSTRAINT with a random name. Better use this
CREATE TABLE tbl(SomeColumn VARCHAR(10) CONSTRAINT DF_YourTable_SomeColumm DEFAULT('test'))
This will do the same, but will name the constraint like you want it.
This is extremely important if you run upgrade scripts in deployed environments. Just imagine, you want to change a constraint later and the name of this constraint is all different on your customers machines... That's a real pain!
So: Always name your constraints!
I need to drop a Unique constraint on a previously existing table and create a new unique constraint that will include extra column. Can I use the name of the constraint to drop it? or will the name of the unique constraint change based on Datasource. I need to execute the script on multiple instances of same DB (eg. dev,test,prod)
Yes, you can use the name of the constraint, provided that you previously deleted and it was a named contraint.
I am trying to change a primary key Id to identity to increment 1 on each entry. But the column has been referenced already by other tables. Is there any way to set primary key to auto increment without dropping the foreign keys from other tables?
If the table isn't that large generate script to create an identical table but change the schema it created to:
CREATE TABLE MYTABLE_NEW (
PK INT PRIMARY KEY IDENTITY(1,1),
COL1 TYPEx,
COL2 TYPEx,
COLn
...)
Set your database to single-user mode or make sure no one is in the
database or tables you're changing or change the table you need to
change to READ/ONLY.
Import your data into MYTABLE_NEW from MYTABLE using set IDENTITY_INSERT on
Script your foreign key constraints and save them--in case you need
to back out of your change later and/or re-implement them.
Drop all the constraints from MYTABLE
Rename MYTABLE to MYTABLE_SAV
Rename MYTABLE_NEW to MYTABLE
Run constraint scripts to re-implement constraints on MYTABLE
p.s.
you did ask if there was a way to not drop the foreign key constraints. Here's something to try on your test system. on Step 4 run
ALTER TABLE MYTABLE NOCHECK CONSTRAINT ALL
and on Step 7 ALTER TABLE MYTABLE CHECK CONSTRAINT ALL. I've not tried this myself -- interesting to see if this would actually work on renamed tables.
You can script all this ahead of time on a test SQL Server or even a copy of the database staged on a production server--to make implementation day a no-brainer and gauge your SLAs for any change control procedures for your company.
You can do a similar methodology by deleting the primary key and re-adding it back, but you'll need to have the same data inserted in the new column before you delete the old column. So you'll be deleting and inserting schema and inserting primary key data with this approach. I like to avoid touching a production table if at all possible and having MYTABLE_SAV around in case "anything" unexpected occurs is a comfort to me personally--as I can tell management "the production data was not touched". But some tables are simply too large for this approach to be worthwhile and, also, tastes and methodologies differ largely from DBA to DBA.
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.
What I'm trying to do is make a copy of a table using a SELECT INTO statement.
After the table is created I want to duplicate the indexes as well.
So the code I'm using is as follows:
SELECT * INTO TableCopy FROM Table
Then:
ALTER TABLE TableCopy ADD CONSTRAINT pkGUID PRIMARY KEY ([GUID])
CREATE INDEX ixIndexName ON TableCopy (CountryCode)
When I execute the index SQL, I get an error that the indexes already exist in the catalog. I didn't think index names had to be unique, I thought they could be duplicated across different tables.
And lo and behold if I create the indexes through management studio, it accepts the index names.
What am I missing here?
Thanks.
I didn't think index names had to be unique, I thought they could be duplicated across different tables.
No. They do have to be unique within a table/view.
When you execute within SSMS, it drops the existing index and creates a new one.
From CREATE INDEX (Transact-SQL) on MSDN:
index_name - Is the name of the index. Index names must be unique within a table or view but do not have to be unique within a database.
(emphasis mine)
However, pkGUID is not an index - it is a constraint, and these do have to be unique within a database.