How to add constraint to table in sql? - sql

I create a table
create table CARS{
CAR_ID NUMBER(10), CONSTRAINT X_CAR_ID NOT NULL
}
and now I want to change the name of the constraint, so I drop the constraint:
ALTER TABLE CARS DROP CONSTRAINT X_CAR_ID;
This works correclty but, when I tried to add new constraint I have a problem,
my query:
ALTER TABLE CARS ADD CONSTRAINT XX_CAR_ID (CAR_ID) NOT NULL;
I thought that query, will be working correctly, but I get only error report:
Error report -
SQL Error: ORA-00904:
How to add correctly this constraint ?

While I couldn't test it I believe the statement below is what you want:
ALTER TABLE CARS MODIFY CAR_ID CONSTRAINT XX_CAR_ID NOT NULL;
Oracle uses the modify keyword in this context.
To rename it without dropping first you would use:
alter table cars rename constraint x_car_id to xx_car_id;
See the reference for more info.

Related

How to add check constraint on a column in table?

ALTER TABLE Student ADD CONSTRAINT CHK_Student_ID_LENGTH CHECK (LEN([ID]) between 12 and 14);
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = 'CHK_Student_ID_LENGTH';
alter table Student drop constraint CHK_Student_ID_LENGTH;
I tried to add check constraint using the first statement. It gave me the below error.
[23000][547] The ALTER TABLE statement conflicted with the CHECK constraint "CHK_Student_ID_LENGTH". The conflict occurred in database, table "Student", column 'ID'.
I tried to check if there was any existing constraint with the same name. But I did not get any. Still I tried to drop the constraint. But then it gave the error:
CHK_Student_ID_LENGTH is not a constraint.
But still add constraint statement gives error saying it already exists. Where am I going wrong?
The error message is a little badly worded, but isn't saying what you think it's saying.
It's not saying that there's already a constraint with the same name. It's saying that the constraint is being violated. That means that there is data already in the table that doesn't meet the requirements of the new constraint you're trying to introduce.
You could use the NOCHECK option to create the constraint whilst allowing existing data to violate it. But this is frequently the wrong thing to do. It is usually more sensible to fix the existing data.
Specifying NOCHECK means that the constraint can't be used by the optimizer to eliminate redundant actions that the logic of the constraint would preclude.
You probably have some records in Your table that conflict with that new constraint.
Just find them and UPDATE/DELETE before adding a contraint.
SELECT *
FROM Student
WHERE LEN([ID]) between 12 and 14
Or try to add that constraint without checking existing values using WITH NOCHECK
ALTER TABLE Student WITH NOCHECK
ADD CONSTRAINT CHK_Student_ID_LENGTH CHECK (LEN([ID]) between 12 and 14);
Tip: In migration files I usually add this before adding a new constraint:
IF OBJECT_ID('CHK_Student_ID_LENGTH') IS NOT NULL
ALTER TABLE Student DROP CONSTRAINT CHK_Student_ID_LENGTH
IF OBJECT_ID('CHK_Student_ID_LENGTH') IS NULL
ALTER TABLE Student ADD CONSTRAINT ...
More about ALTER TABLE https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=sql-server-ver15

The ALTER TABLE statement conflicted with the CHECK constraint

I want to create a foreign key between two database. Happens that the tables have data, so when I'm making the foreign relational there's giving me error.
I found the error happen when your tables have data. So how to not verify existing data?
ALTER TABLE [contrato_sigob].[relacion_institucion_categoria]
ADD CONSTRAINT CHECK_CATEGORIA
CHECK([dbo].[func_CHECK_CATEGORIA](id_categoria)=1);
The error says:
The ALTER TABLE statement conflicted with the CHECK constraint "CHECK_CATEGORIA". The conflict occurred in database "SIGOB_Contraloria", table "contrato_sigob.relacion_institucion_categoria", column 'id_categoria'.
So how to not verify existing data?
You can create a constraint that will not check your existing records by adding WITH NOCHECK:
ALTER TABLE TABLE_NAME WITH NOCHECK
ADD CONSTRAINT ...

Altering constraint in Derby

I don't understand the logic in Derby syntax.
I'm new to SQL, I'm learning now.
I needed to alter a column constraint. I needed to add "not null".
Since I already did an alter on a column for the constraint unique, I thought the syntax will be the same since they are both constraints.
I finally found for not null this link Cannot alter column in existing table in java Derby database
and as I say the same syntax doesn't work for adding a unique constraint to the same column, so my question is: how is the syntax changed for each constraint?
This works:
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME NOT NULL;
So why does this not work?
ALTER TABLE WALLETUSER ALTER COLUMN WALLETUSERNAME unique;
And why does this work:
alter table customer add constraint cu1 unique (cust_name);
but this does not:
alter table customer add constraint not null (cust_name);

SQL - Add Unique Constraint Failure

Trying to alter a table in SQL Server. I want to add a unique constraint to a column called Names in table ReportingItemNames:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([ReportingItemNames,Name])
But I am getting this error:
Column name 'ReportingItemNames,Name' does not exist in the target table or view
Where am I going wrong?
Use this:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames UNIQUE ([Name])
You can refer to ALTER TABLE (Transact-SQL) documentation for more information.
Shouldn't it be:
ALTER TABLE ReportingItemNames
ADD CONSTRAINT UC_ReportingItemNames$Name UNIQUE ([Name])

Adding column with primary key in existing table

I am trying to add primary key to newly added column in existing table name Product_Details.
New Column added: Product_Detail_ID (int and not null)
I am trying add primary key to Product_Detail_ID (please note: there are no other primary or foreign key assigned to this table)
I am trying with this query but getting error.
ALTER TABLE Product_Details
ADD CONSTRAINT pk_Product_Detils_Product_Detail_ID PRIMARY KEY(Product_Detail_ID)
GO
Error:
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.Product\_Details' and the index name 'pk\_Product\_Detils'. The duplicate key value is (0).
Am I missing something here? I am using SQL Server 2008 R2. I would appreciate any help.
If you want SQL Server to automatically provide values for the new column, make it an identity.
ALTER TABLE Product_Details DROP COLUMN Product_Detail_ID
GO
ALTER TABLE Product_Details ADD Product_Detail_ID int identity(1,1) not null
GO
ALTER TABLE Product_Details
add CONSTRAINT pk_Product_Detils_Product_Detail_ID primary key(Product_Detail_ID)
GO
In mysql, I was able to achieve with following query
ALTER TABLE table_name ADD new_column int NOT NULL AUTO_INCREMENT primary key
Add Primary Key to First Position
ALTER TABLE table_name
ADD column_name INT PRIMARY KEY AUTO_INCREMENT FIRST;
Reference: Stack Overflow | Tech On The Net
You are getting the error because you have existing data that does not fullfill the constraint.
There are 2 ways to fix it:
clean up the existing data before adding the constraint
add the constraint with the "WITH NOCHECK" option, this will stop sql server checking existing data, only new data will be checked
ALTER TABLE Jaya
ADD CONSTRAINT no primary key(No);
here Jaya is table name,
no is column name,
ADD CONSTRAINT is we giving the primary key keyword
If you want to add a new column say deptId to the existing table say department then you can do it using the below code.
ALTER TABLE department ADD COLUMN deptID INT;
it will create your new column named deptID.
now if you want to add constraint also along with new column while creating it then you can do it using the below code. In the below code I am adding primary key as a constraint. you can add another constraint also instead of primary key like foreign key, default etc.
ALTER TABLE department ADD COLUMN deptID INT NOT NULL ADD CONSTRAINT PRIMARY KEY(deptID);
k. friend
command:
sql> alter table tablename add primary key(col_name);
ex: alter table pk_Product_Detils add primary key(Product_Detail_ID);