How to create a check constraint between two columns in SQL? - oracle

I am trying to create a Basic pay (BP) table with
CREATE TABLE bp (
bpid VARCHAR(5),
FOREIGN KEY (bpid) REFERENCES designation(desigid),
upperlimit DECIMAL(10,2) NOT NULL,
lowerlimit DECIMAL(10,2) NOT NULL,
increment DECIMAL(10,2) NOT NULL
CONSTRAINT llvalid CHECK (upperlimit > lowerlimit)
);
As you can see near the ending, I want to check if upperlimit is greater than lowerlimit, how can I do that?

It might (probably does) depend on the data base you use.
Comparing to the oracle syntax (e.g. here: http://www.techonthenet.com/oracle/check.php), what you are missing might be a ',' between NULL and CONSTRAINT

The problem is that you have defined it as a column level constraint but it references other columns. You must define a constraint at the table level.
ALTER TABLE bp
ADD CONSTRAINT CK_limit CHECK ( upperlimit > lowerlimit)

Here's proper the SQL query...
CREATE TABLE bp (bpid VARCHAR(5),
FOREIGN KEY (bpid) REFERENCES designation(desigid),
upperlimit DECIMAL(10,2) NOT NULL,
lowerlimit DECIMAL(10,2) NOT NULL,
increment DECIMAL(10,2) NOT NULL,
CONSTRAINT llvalid CHECK (upperlimit > lowerlimit));
Note the comma after NOT NULL and CONSTRAINT in the last line.

Related

How can i drop a check constraint without deleting and recreating the table in sqlplus

I have this table and i added a check to the column duree
with this condition alter table operation modify Duree integer check(Duree>=2);
but in the homework it's specified that it needs to be like (duree>=44) isntead
how this be fixed.
create table Operation (
CodeOP varchar(20) not null ,
Duree integer ,
Chef integer ,
DateDeb Date ,
Budget float ,
CodeS varchar (20),
constraint pk_operation primary key (CodeOP),
constraint fk_operation foreign key(CodeS)REFERENCES Service(CodeS) on delete set null
i tried alter table operation modify Duree integer check(Duree>=44);
but i'm getting this message :
ERROR at line 1: ORA-02293: cannot validate (TPBDD2.SYS_C008292) - check constraint violated

having error for missing left parenthesis?

CREATE TABLE Transaction
(Portfolio_Number NUMBER(7,0) CONSTRAINT NN_Portfolio_Number NOT NULL,
CONSTRAINT FK_Portfolio_Number FOREIGN KEY
(Portfolio_Number) REFERENCES Portfolio(Portfolio_Number),
CONSTRAINT PK_Portfolio_Number PRIMARY KEY,
Stock_Code VARCHAR2(50) CONSTRAINT NN_Stock_Code NOT NULL,
CONSTRAINT FK_Stock_Code FOREIGN KEY
(Stock_Code) REFERENCES Stock(Stock_Code),
CONSTRAINT PK_Stock_Code PRIMARY KEY,
Transaction_Date DATE DEFAULT SYSDATE
CONSTRAINT PK_Transaction_Date PRIMARY KEY
CONSTRAINT NN_Transaction_Date NOT NULL,
Exchange_Code VARCHAR2(4) CONSTRAINT NN_Exchange_Code NOT NULL,
CONSTRAINT FK_Exchange_Code FOREIGN KEY
(Exchange_Code) REFERENCES Exchange(Exchange_Code),
Broker_Number NUMBER(7,0) CONSTRAINT NN_Broker_Number NOT NULL,
CONSTRAINT FK_Broker_Number FOREIGN KEY
(Broker_Number) REFERENCES Broker(Broker_Number),
Buy_Sell CHAR(1) CONSTRAINT NN_Buy_Sell NOT NULL,
Quantity NUMBER(7,0) CONSTRAINT CK_Quantity CHECK (Quantity > 0)
CONSTRAINT NN_Quantity NOT NULL,
Price_Per_Share NUMBER(6,2) CONSTRAINT NN_Price_Per_Share NOT NULL);
It seems that you got lost in too many CONSTRAINTs scattered throughout that piece of code.
a table can have only one primary key constraint; you have 3 of them. If it is a composite primary key, all columns should be placed into the same CONSTRAINT clause (see my example)
you can't mix constraints with columns; if you use them, put all those outline constraints to the end of the script (like I did)
I can't run it as I miss all tables you're referencing, but - this code is formatted properly so I hope it should compile.
CREATE TABLE Transaction
(
Portfolio_Number NUMBER (7, 0) CONSTRAINT NN_Portfolio_Number NOT NULL,
Stock_Code NUMBER (7, 0) CONSTRAINT NN_Stock_Code NOT NULL,
Transaction_Date DATE
DEFAULT SYSDATE
CONSTRAINT NN_Transaction_Date NOT NULL,
Exchange_Code VARCHAR2 (4) CONSTRAINT NN_Exchange_Code NOT NULL,
Broker_Number NUMBER (7, 0) CONSTRAINT NN_Broker_Number NOT NULL,
Buy_Sell CHAR (1) CONSTRAINT NN_Buy_Sell NOT NULL,
Quantity NUMBER (7, 0)
CONSTRAINT CK_Quantity CHECK (Quantity > 0)
CONSTRAINT NN_Quantity NOT NULL,
Price_Per_Share NUMBER (6, 2) CONSTRAINT NN_Price_Per_Share NOT NULL,
--
CONSTRAINT PK_Transaction_Date PRIMARY KEY
(portfolio_number, transaction_date, stock_code),
CONSTRAINT FK_Portfolio_Number FOREIGN KEY
(Portfolio_Number)
REFERENCES Portfolio (Portfolio_Number),
CONSTRAINT FK_Stock_Code FOREIGN KEY
(Stock_Code)
REFERENCES Stock (Stock_Code),
CONSTRAINT FK_Exchange_Code FOREIGN KEY
(Exchange_Code)
REFERENCES Exchange (Exchange_Code),
CONSTRAINT FK_Broker_Number FOREIGN KEY
(Broker_Number)
REFERENCES Broker (Broker_Number)
);
You are trying to create a primary key without specifying column name
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY,
which is wrong. The right would be something like that
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY(Portfolio_Number),
Next thing to note is you're trying to create 2 primary keys for same table which is impossible. If you need a primary key to be for both columns, you need to specify them in the same statement
, CONSTRAINT PK_Portfolio_Number PRIMARY KEY(Portfolio_Number, Stock_Code),

Missing parentheses in CREATE TABLE instruction

CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID CHAR(10) NOT NULL,
DESCRIPTION_PROD CHAR(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING INT(1),
PRIMARY KEY (PRODUCT_ID)
);
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Oracle is not MySQL so there is no INT(1) type:
CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID CHAR(10) NOT NULL,
DESCRIPTION_PROD CHAR(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING INT, -- here
PRIMARY KEY (PRODUCT_ID)
);
SqlFiddleDemo
If you don't need constant size of string, consider using VARCHAR2(30).
EDIT:
but i need those values to be between 1-5
So add check constraint:
CONSTRAINT chk_PROD_RATING CHECK (PROD_RATING BETWEEN 1 AND 5),
SqlFiddleDemo2
As already noted INT does not take a length constraint in Oracle - instead you could use NUMBER(1,0) which would restrict it to a single digit (-9 .. +9) and then you can further restrict it using a CHECK constraint.
CHAR(n) will also right pad the value with space (CHR(32)) characters so that it always contains the maximum number of characters. If you did not intend this then you should be using VARCHAR2(n) instead.
You also do not need a NOT NULL constraint on a column that is the PRIMARY KEY.
CREATE TABLE MAJEST_PROD_2015(
PRODUCT_ID VARCHAR2(10) CONSTRAINT MAJEST_PROD_2015__PROD_ID__PK PRIMARY KEY,
DESCRIPTION_PROD VARCHAR2(30),
SEW_DATE DATE,
HARVEST_DATE DATE,
QUANTITY INT,
PROD_RATING NUMBER(1,0) CONSTRAINT MAJEST_PROD_2015__PROD_RAT__CK CHECK ( PROD_RATING BETWEEN 1 AND 5 )
);
(also, should it be SEW_DATE or SOW_DATE? Since the next line talks about harvest then I would have thought "sow" was more apt.)

Error query oracle db in toad

I've executed following query in toad:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
UNIQUE (id)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL;
And got this ORA message:
ORA-00907 missing right parenthesis
Cause: A left parenthesis has been entered without a closing right parenthesis, or extra information was contained in the parentheses. All parentheses must be entered in pairs.
Action: Correct the syntax and retry the statement.
There are a couple of errors in your syntax. The NOT NULL has to occur after the IDENTITY clause. The range has to be specified without a , and the UNIQUE keyword has to appear at the column directly:
CREATE TABLE ACTWEB.usuarios
(
id INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL UNIQUE,
matricula INTEGER,
nome CHAR(50) NOT NULL,
senha CHAR(50),
nivel INTEGER,
maleta INTEGER,
email CHAR(50),
acessos INTEGER,
datacriacao DATE,
dataalteracao DATE
)
LOGGING NOCOMPRESS NOCACHE NOPARALLEL;
You specified unique not right, to solve it you have several ways:
First, create unique key explicitly:
create table tab1(
id number(10),
CONSTRAINT id_uk UNIQUE (id)
)
Second, create unique key as an option of the column:
create table tab1(
id number(10) UNIQUE
)
Third, add the unique key constraint using alter table:
create table tab1(
id number(10)
);
alter table tab1 add constraint id_uk unique(id);
Forth, create unique key implicitly by creating unique index:
create table tab1(
id number(10)
);
CREATE UNIQUE INDEX id_uk ON tab1 (id);

Drop a column from composite primarykey

i want to drop a column from composite primary key(not to delete from table). if my table is like this.
create table "scott"."xyz"(
"column1" not null,
"column2" not null,
"column3" not null,
"column4" not null,
"column5" not null,
"column6",
CONSTRAINT PRIMARY KEY ("column1","column2","column3","column4")
);
i want to alter this primary key to first three column without dropping it. beacuse i don't know CONSTRAINT name.
You don't need the constraint name:
ALTER TABLE "scott"."xyz" DROP PRIMARY KEY;
ALTER TABLE "scott"."xyz" ADD PRIMARY KEY ("column1","column2","column3");
But it is probably a good idea to give your PK a name in the future:
ALTER TABLE "scott"."xyz"
ADD CONSTRAINT pk_xyz PRIMARY KEY ("column1","column2","column3");
And I would not recommend to use quoted identifiers. You will have problems with various tools in the long run.

Resources