What should be the sql command(oracle) to add 'NOT NULL' constraint, with a constraint name, to a column of existing table?
alter table t add constraint my_named_not_null check ( x is not null );
Related
I want to add the 'check' constraint to some table, but firstly I have to check if that constraint exists. I have some error in my SQL script. What is the corret way to do it ?
ALTER TABLE public.ELEMENTS ADD CONSTRAINT IF NOT EXISTS elements_check CHECK ((t1_id IS NOT NULL) OR (t2_id IS NOT NULL));
According to the manual :
ALTER TABLE ... DROP CONSTRAINT IF EXISTS do exist
ALTER TABLE ... ADD CONSTRAINT IF NOT EXISTS doesn't exist
so you have to execute 1. before 2. :
ALTER TABLE public.ELEMENTS DROP CONSTRAINT IF EXISTS elements_check ;
ALTER TABLE public.ELEMENTS ADD CONSTRAINT elements_check CHECK ((t1_id IS NOT NULL) OR (t2_id IS NOT NULL));
I have the below query from oracle query point of view is that I have created a constraint on table BOA_INVOICE as shown below
ALTER TABLE BOA_INVOICE ADD CONSTRAINT CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS'));
Now this constraint is added successfully , but later on i want to modify same constraint add two values as shown below
ALTER TABLE BOA_INVOICE ADD CONSTRAINT CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS','PER','AWE'));
Please advise what will be the query to achieve the same
You need to drop the constraint first and then create it again.
ALTER TABLE BOA_INVOICE DROP CONSTRAINT CK_INVOICE_SOURCE_SYSTEM;
Then Create it again:
ALTER TABLE BOA_INVOICE ADD CONSTRAINT
CK_INVOICE_SOURCE_SYSTEM CHECK ( SOURCE_SYSTEM IN ('PCE','PDS','PER','AWE'));
I know how to change the length of a column, but my SQL statement fails because the column I'm trying to change is a PK, so I get the following error:
Msg 5074, Level 16, State 1, Line 1
The object 'PK_TableName' is dependent on column 'PersonID'.
PersonID = PK.
I've read What is the sql to change the field length of a table column in sql server which only applies to non-PK columns.
I tried this:
ALTER TABLE table_name
ALTER COLUMN column_name <new datatype>
See below sample example how to increase size of the primary column
Create a sample table
create table abc (id varchar(10) primary key)
Find primary constraint in key constraints tables
select object_name(object_id),* from sys.key_constraints where object_name(parent_object_id) = 'abc
Drop constraint
ALTER TABLE abc
DROP CONSTRAINT PK__abc__3213E83F74EAC69B
(Replace PK__abc__3213E83F74EAC69B with constraint name you receive.)
Add not null
ALTER TABLE abc alter column id varchar(20) NOT NULL;
Add primary key again
ALTER TABLE abc
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (id)
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE table_name
ALTER COLUMN column_name datatype
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY (<Column1>,<Column2>)
SQLServer 2008 did not allow me to change a primary key with data so I deactivated all the constraints, performed the command and activated all the constraints again. The commands are:
EXEC sp_MSforeachtable #command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
-- commands here
EXEC sp_MSforeachtable #command1="ALTER TABLE ? CHECK CONSTRAINT ALL"
I have the following table in SQL Server 2008 R2
Now I need to write a script to add a new column cusomerVLANID as part of the primary key, so that the three columns becomes the primary key, is there a way to write such script.
Second thing I want to write a script to remove the Allow Null, check box from the CustomerVLANID columns ?
Thanks
ALTER TABLE <Table_Name>
DROP CONSTRAINT <constraint_name>
ALTER TABLE <Table_Name>
ADD CONSTRAINT <constraint_name> PRIMARY KEY ([ID], [CustomerName], [CustomerVLANSID])
Run this statement separately to set up the NOT NULL constraint:
ALTER TABLE <Table_Name>
ALTER COLUMN [CustomerVLANSID] INT NOT NULL
alter table TABLE1
alter column [CustomerVLANID] int not null
I hope this helps,
-Thomas
RosSQL.blogspot.com
I'm trying to add the NOT_NULL constraint to a column in an SQL h2 database, using
ALTER TABLE CHARACTERS ADD CONSTRAINT nn_PID NOT_NULL (PLAYER_ID);
This follows the pattern I found here:
ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
Except I change the constraint, table and column names. But I get this error:
Syntax error in SQL statement "ALTER TABLE CHARACTERS ADD CONSTRAINT NN_PID NOT_NULL[*] (PLAYER_ID) "; expected "., COMMENT, PRIMARY, INDEX, KEY, CHECK, UNIQUE, FOREIGN"; SQL statement:
ALTER TABLE CHARACTERS ADD CONSTRAINT nn_PID NOT_NULL (PLAYER_ID) [42001-168] 42001/42001 (Help)
How can I add the NOT_NULL constraint?
From H2 SQL Grammar:
ALTER TABLE TEST ALTER COLUMN NAME SET NOT NULL;
So we can use:
ALTER TABLE CHARACTERS ALTER PLAYER_ID SET NOT NULL;