SQL commamd Line - sql

How to apply Not null constraint in SQL command line in Oracle database

this example shows if you want to give a constraint a name
ALTER TABLE mytable
MODIFY (mynumber NUMBER(8,2) CONSTRAINT my_cons_name NOT NULL);
this example show if you do not want to give a constraint a name
ALTER TABLE mytable
MODIFY (mynumber NUMBER(8,2) NOT NULL);
source: https://community.oracle.com/thread/45653

Related

Alter table in database throwing error in SQL Developer

I have the following query to alter my customer table to add a column for checking if a customer is active or not.
ALTER TABLE COMPANY.CUSTOMER
ADD (isActive VARCHAR2(18 CHAR) DEFAULT 'FALSE' NOT NULL)
CHECK(isActive in ('TRUE','FALSE'));
I get the following error:
RA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
I tried to change the order and still did not work. can you help me with why it is failing to alter the table?
Also how to ensure if TRUE or FALSE is always uppercase when inserting?
You can split up the CHECK constraint from the ADD COLUMN for one route...
/* drop table customer_so purge; */
create table customer_so (id integer, name varchar2(50));
ALTER TABLE CUSTOMER_SO
ADD (ISACTIVE VARCHAR2(20) DEFAULT 'FALSE' );
ALTER TABLE CUSTOMER_SO
ADD CONSTRAINT CUSTOMER_SO_CHK1 CHECK
(ISACTIVE IN ('TRUE', 'FALSE'))
ENABLE;
Or, like you had, all in one go -
/* drop table customer_so purge; */
create table customer_so (id integer, name varchar2(50));
ALTER TABLE CUSTOMER_SO
ADD (ISACTIVE VARCHAR2(20) DEFAULT 'FALSE' constraint CUSTOMER_SO_CHK1 CHECK
(ISACTIVE IN ('TRUE', 'FALSE')));
So basically end of the day, you're missing the 'CONSTRAINT' keyword.
Since you tagged oracle-sqldeveloper, you should know the EDIT TABLE ddialog lets you click through these changes, and will generate the DDL for you -
Finally, by default, strings in Oracle are case-sensitive, so your check constraint will work as desired.

Unable to assign default constraint in an existing column in SQL Server

I am using SQL Server and have a table Employee. I want to change the constraint of Starting_Date from NULL to NOT NULL and add a DEFAULT with GETDATE() function. Please check the screenshot and suggest how to solve the error I get:
Try this TSQL command for add, not null constraint
ALTER TABLE [dbo].[Employee] ALTER COLUMN [Starting_Date] DataTime NOT NULL;
If your table had any rows with null Starting_Date run this query before adding the constraint
UPDATE [dbo].[Employee] SET Starting_Date = [dbo].[GetDate]() WHERE Starting_Date = NULL;
and then this, for default constraint from your function output result
ALTER TABLE [dbo].[Employee]
ADD CONSTRAINT df_Starting_Date
DEFAULT([dbo].[GetDate]())FOR Starting_Date;

SQL Table Restriction

How can I specify, 5 distinct values for a varchar column in Oracle Application Express?
I need a column called tipo_conta (varchar) that only accepts 'Conta a ordem', 'Multibanco', 'Rendimento', 'Jovem', 'Rendimento-Habitacao' as possible values.
I tried this but I get this error - ORA-00907: missing right parenthesis.
What am I doing wrong?
CREATE TABLE contas
(
id_conta NUMBER(6),
tipo_conta VARCHAR2(20),
CONSTRAINT id_conta PRIMARY KEY(id_conta),
CONSTRAINT tipo_conta UNIQUE (tipo_conta)
CONSTRAINT chk_tipo_conta CHECK (Frequency IN ('Conta a ordem', 'Multibanco', 'Rendimento', 'Jovem', 'Rendimento-Habitacao'))
);
Actually it looks like you are missing a comma in your CONSTRAINT CLAUSES over here:
CONSTRAINT tipo_conta UNIQUE (tipo_conta)
should instead be:
CONSTRAINT tipo_conta UNIQUE (tipo_conta),
Also your CHECK does not reference the column properly:
Instead of CONSTRAINT chk_tipo_conta CHECK (Frequency IN ...
try CONSTRAINT chk_tipo_conta CHECK (tipo_conta IN ...

Drop "not null" Constraint Error in Oracle 11g

I'm using the following code in Oracle 11g
alter table account_creation
drop constraint branch_code;
I get an error saying cannot drop constraint non existent constraint but when I check the table the NULL constraint still exists for that particular column
Am I using the correct syntax?
Use this syntax:
ALTER TABLE account_creation
MODIFY (branch_code varchar(10) NULL);

CHECK CONSTRAINT on multiple columns

I use SQL Server 2008
I use a CHECK CONSTRAINT on multiple columns in the same table to try to validate data input.
I receive an error:
Column CHECK constraint for column
'AAAA' references another column,
table 'XXXX'.
CHECK CONSTRAINT does not work in this way.
Any other way to implement this on a single table without using FK?
Thanks
Here an example of my code
CREATE TABLE dbo.Test
(
EffectiveStartDate dateTime2(2) NOT NULL,
EffectiveEndDate dateTime2(2) NOT NULL
CONSTRAINT CK_CmsSponsoredContents_EffectiveEndDate CHECK (EffectiveEndDate > EffectiveStartDate),
);
Yes, define the CHECK CONSTRAINT at the table level
CREATE TABLE foo (
bar int NOT NULL,
fred varchar(50) NOT NULL,
CONSTRAINT CK_foo_stuff CHECK (bar = 1 AND fred ='fish')
)
You are declaring it inline as a column constraint
...
fred varchar(50) NOT NULL CONSTRAINT CK_foo_fred CHECK (...)
...
Edit, easier to post than describe. Fixed your commas.
CREATE TABLE dbo.Test
(
EffectiveStartDate dateTime2(2) NOT NULL,
EffectiveEndDate dateTime2(2) NOT NULL, --need comma
CONSTRAINT CK_CmsSponsoredContents_EffectiveEndDate CHECK (EffectiveEndDate > EffectiveStartDate) --no comma
);
Of course, the question remains are you using a CHECK constraint where it should be an FK constraint...?
Check constraints can refer to a single column or to the whole record.
Use this syntax for record-level constraints:
ALTER TABLE MyTable
ADD CONSTRAINT MyCheck
CHECK (...your check expression...)
You can simply apply your validation in a trigger on the table especially that either way the operation will be rolled back if the check failed.
I found it more useful for CONSTRAINT using case statements.
ALTER TABLE dbo.ProductStock
ADD
CONSTRAINT CHK_Cost_Sales
CHECK ( CASE WHEN (IS_NOT_FOR_SALE=0 and SAL_CPU <= SAL_PRICE) THEN 1
WHEN (IS_NOT_FOR_SALE=1 ) THEN 1 ELSE 0 END =1 )