Multiple constraints on single column - sql

Can we add multiple constraints on a single column?
like-
create table x(x varchar2(20), y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id),
constraint null_cons not null(x)
)
this query is returning error ora-00904: invalid identifier....

You have wrong syntax when create null_cons constraint:
Use this (table level check constraint):
CREATE TABLE x(
x VARCHAR2(20),
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id),
CONSTRAINT null_cons CHECK(x IS NOT NULL)
)
Or (use NOT NULL constraint on column):
CREATE TABLE x(
x VARCHAR2(20) NOT NULL,
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
Or (Use column level check constraint):
CREATE TABLE x(
x VARCHAR2(20) CHECK (X IS NOT NULL),
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)

create table x(x varchar2(20), y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id),
constraint null_cons check(x is not null)
)

If the RDBMS is SQL Server, you can define multiple constraints at the column by doing this (I changed some of the column names to make the constraint naming strategy more obvious - naming is subjective):
CREATE TABLE SomeTable(
User_Id VARCHAR(20) CONSTRAINT FK_SomeTable_User_Id FOREIGN KEY REFERENCES dbo.User_Info(User_Id) CONSTRAINT UIX_NC_SomeTable_User_id NONCLUSTERED NOT NULL,
SomeOtherColumn DECIMAL(2, 0) NOT NULL
)

create table x(x varchar2(20) not null, y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id)
)

Related

Oracle SQL missing smth

Trying to build these tables and there's always an error or missing something, can someone help me?
Where's max_mice i'm checking if it's inbetween those values and doesn't work why?
Don't know what's wrong, already searched everywhere, don't know why they won't be created...
updated: now i got problems on table incidents...
update: the error was that i had: CONSTRAINT fun_maxmi_ch CHECK (200 > max_mice >= min_mice) instead of the code below.
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
BUT still have a problem creating table Incidents don't know what's the problem!!!
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
Here's the full code:
CREATE TABLE Enemies (
enemy_name VARCHAR2(15),
hostility_degree NUMBER(2) CONSTRAINT hos_degree_ch CHECK (hostility_degree BETWEEN 1 AND 10),
species VARCHAR2(15),
bride VARCHAR2(20),
CONSTRAINT ene_name_pk PRIMARY KEY(enemy_name)
);
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
CREATE TABLE Bands (
Band_no NUMBER(2) CONSTRAINT ban_no_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT ban_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT ban_site_un UNIQUE,
band_chief VARCHAR(15) CONSTRAINT ban_chief_un UNIQUE
);
CREATE TABLE Cats (
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gen_ch CHECK (gender IN('M', 'W')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10),
chief VARCHAR2(15),
in_herd_since DATE DEFAULT SYSDATE CONSTRAINT cat_inherd_nn NOT NULL,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2),
CONSTRAINT cat_banno_fk FOREIGN KEY (band_no) REFERENCES Bands(band_no),
CONSTRAINT cat_chief_fk FOREIGN KEY (chief) REFERENCES Cats(nickname),
CONSTRAINT cat_fun_fk FOREIGN KEY (function) REFERENCES Functions(function)
);
ALTER TABLE Bands
ADD CONSTRAINT ban_chief_fk FOREIGN KEY (band_chief) REFERENCES Cats(nickname);
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
here's max_mice i'm checking if it's in between those values and doesn't work why?
When it comes to the first table in your code, Functions, the problem is with the declaration of the last check constraint:
max_mice NUMBER(3) CONSTRAINT fu_maxmi_ch CHECK (200 > max_mice >= min_mouse)
Issues:
the double inequality condition is not supported
a multi-column check constraint need to be declared at table level rather than at column level
This works:
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fu_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fu_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fu_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
Note: I would not recommend naming a column function, since it obviously conflicts with a SQL keyword.

Is it possible to create to tables that reference each other without using ALTER? [duplicate]

This question already has an answer here:
Is there any other way to create constraints during SQL table creation?
(1 answer)
Closed 2 years ago.
I am new to databases in general and would like to have an answer to a problem that I am facing.
I would like to know if there is away two create two tables that reference each other without creating one and Altering it later and adding the reference.
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2) CONSTRAINT cat_bno REFERENCES Bands(band_no))
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
As far as I remember I can not do it; I am right?
No, it cannot be done. Whichever table you CREATE first cannot have a foreign key to the other table as that other table does not exist yet; instead you need to create the tables and then use ALTER TABLE ADD CONSTRAINT to create the constraint from the first table to the second.
For example:
CREATE TABLE Functions ( function VARCHAR2(10) CONSTRAINT fn_fn PRIMARY KEY );
CREATE TABLE Cats(
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gd CHECK(gender IN('W', 'M')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10) CONSTRAINT cat_fn REFERENCES Functions(function),
chief VARCHAR2(15) CONSTRAINT cat_chf REFERENCES Cats(nickname),
in_herd_sinnce DATE DEFAULT SYSDATE,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2)
);
CREATE TABLE Bands(
band_no NUMBER(2) CONSTRAINT bd_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT bd_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT bd_site_un UNIQUE,
band_chief VARCHAR2(15) CONSTRAINT bd_chf_un UNIQUE
CONSTRAINT bd_chf_nm REFERENCES Cats(nickname)
);
ALTER TABLE CATS ADD CONSTRAINT cat_bno FOREIGN KEY ( band_no )
REFERENCES Bands(band_no);
db<>fiddle here

Oracle Missing expression

I only have this error now.. I cant seem to see where it is too long (ERROR at bottom of page)
CREATE TABLE Menu_Item_Ingredient
(
Menu_Item_Number Number(5,0)CONSTRAINT NN_MenuItemIngredient_MenuItemNumber Not null,
CONSTRAINT FK_MenuItemIngredient_MenuItemNumber Foreign Key(Menu_Item_Number) References Bill_Item(Menu_item_Number),
Ingredient_Number Number(5,0) CONSTRAINT NN_MenuItemIngredient_IngredientNumber Not null,
CONSTRAINT FK_MenuItemIngredient_IngredientNumber Foreign Key(Ingredient_Number) References Ingredient(Ingredient_Number),
Quantity_Needed Number(5,2) DEFAULT 0 CONSTRAINT NN_MenuItemIngredient_QuantityNeeded Not null
CONSTRAINT CK_MenuItemIngredient_QuantityNeeded CHECK(Quantity_Needed >= 0),
CONSTRAINT PK_BillItem_Ingredient Primary key(Menu_Item_Number,Ingredient_Number)
)
Error report -
SQL Error: ORA-00972: identifier is too long
00972. 00000 - "identifier is too long"
*Cause: An identifier with more than 30 characters was specified.
*Action: Specify at most 30 characters.
Do something below. In your query, a comma is missing when you are declaring discount column in bill_item table.
CREATE TABLE Bill_Item3
(
Bill_Number NUMBER(6,0) CONSTRAINT NN_BillItem_BillNumber NOT NULL,
Menu_Item_Number NUMBER(5,0) CONSTRAINT NN_BillItem_MenuItemNumber NOT NULL,
Discount NUMBER(5,2) CONSTRAINT N_BillItem_Discount NULL,
--colum Discount format '%'00.00,
Quaintity_Sold NUMBER(3,0)CONSTRAINT NN_BillItem_QuaintitySold NOT NULL,
Selling_Price NUMBER(6,2) default 0 CONSTRAINT NN_BillItem_SellingPrice NOT NULL,
CONSTRAINT CK_BillItem_SellingPrice CHECK (Selling_Price >= 0 ),
CONSTRAINT PK_Bill_MenuItem PRIMARY KEY (Bill_Number,Menu_Item_Number),
CONSTRAINT FK_BillItem_Bill FOREIGN KEY (Bill_Number) REFERENCES bill (Bill_Number),
CONSTRAINT FK_BillItem_MenuItemNumber FOREIGN KEY (Menu_Item_Number) REFERENCES Menu_Item_Ingredient(Menu_Item_Number),
CONSTRAINT CK_BillItem_Discount CHECK (Discount BETWEEN 0 AND 100)
);
Your problem is with the constraints,
A foreign key constraint will be used to give reference to a column in another table, in your queries you have given constraints like below -
"CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number), what are you trying to do with this?
The syntax you need to use should be -
CONSTRAINT constraint_name FOREIGN KEY (column_in_current_table) REFERENCES Other_Table_Name(column_name_you_want_to_refer_in_other_table)
As far as I've seen, this will not work, you cannot use DEFAULT in a check constraint.
constraint CK_BillItem_SellingPrice check (Selling_Price >= 0 OR DEFAULT = 0)
If you want to enforce a DEFAULT constraint, you have to use something as below -
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
Your final table can be as follows -
CREATE TABLE Bill_Item
(
Bill_Number Number(6,0) constraint NN_BillItem_BillNumber not null,
--CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number),
Menu_Item_Number Number(5,0) CONSTRAINT NN_BillItem_MenuItemNumber Not null,
--CONSTRAINT FK_BillItem_MenuItemNumber Foreign Key (Menu_Item_Number) References Primary(Menu_Item_Number),
Discount Number(5,2) CONSTRAINT N_BillItem_Discount Null
CONSTRAINT CK_BillItem_Discount Check (Discount BETWEEN 0 and 100),
--colum Discount format '%'00.00,
Quaintity_Sold Number(3,0)CONSTRAINT NN_BillItem_QuaintitySold not null,
Selling_Price Number(6,2) DEFAULT 0.0 CONSTRAINT NN_BillItem_SellingPrice not null, -- You can remove "DEFAULT 0.0" if you do not want to default your Selling_Price to 0.0
constraint CK_BillItem_SellingPrice check (Selling_Price >= 0),
CONSTRAINT PK_Bill_MenuItem Primary key (Bill_Number,Menu_Item_Number)
);
All of your other tables also have the same issue, remove the DEFAULT from check constraint as I did in the above example, and specify "Default" constraint at the column definition itself.
Also, create the table which will be referenced in other tables first, for example, your second table "Bill" has a foreign key constraint which is referencing to "Waiter" table, so you have to create "Waiter" table first.
If you are still having concerns let me know.

Oracle SQL Create Table Issue

When I try to create a test table with some test rows, SQL Plus gives me the following error:
SQL> CREATE TABLE test_table
2 (
3 test_data0 NUMBER(3) CONSTRAINT test_data0_pk PRIMARY KEY,
4 test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2, CONSTRAINT test_data1_condition NOT NULL,
5 test_data2 DATE CONSTRAINT test_data2_condition NOT NULL,
6 test_data3 NUMBER(2),
7 test_data4 NUMBER(2) DEFAULT 0
8 );
test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2, C
ONSTRAINT test_data1_condition NOT NULL,
*
ERROR at line 4:
ORA-00904: : invalid identifier
If I only put one of the two constraints on line 4 I don't get any error. What is the problem?
You don't need the constraint key word for inline constraints. Of course, you can use it. But, in particular, not null is usually expressed without a constraint. So:
CREATE TABLE test_table (
test_data0 NUMBER(3) CONSTRAINT test_data0_pk PRIMARY KEY,
test_data1 NUMBER(3) NOT NULL CONSTRAINT test_data1_fk REFERENCES test_table2,
test_data2 DATE NOT NULL,
test_data3 NUMBER(2),
test_data4 NUMBER(2) DEFAULT 0
);
If you do use constraint more than once, you need to leave out the comma. That is the error you are getting. So that line would be:
test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2
CONSTRAINT test_data1_condition NOT NULL,
I often leave out the constraint itself for the inline constraints:
CREATE TABLE test_table (
test_data0 NUMBER(3) PRIMARY KEY,
test_data1 NUMBER(3) NOT NULL REFERENCES test_table2,
test_data2 DATE NOT NULL,
test_data3 NUMBER(2),
test_data4 NUMBER(2) DEFAULT 0
);
Admittedly, you lose the name of the constraint when violated.

Oracle: Many to Many: Requires two Foreign Key Constraints?

Im very new to SQL and i tried to create a many to many relationship:
CREATE TABLE HOUSE_USER
(
USER_ID NUMBER(10) NOT NULL,
USER_EMAIL VARCHAR(255) NOT NULL,
USER_PASSWORD VARCHAR(255) NOT NULL,
CONSTRAINT USER_PK PRIMARY KEY(USER_ID),
CONSTRAINT PROFILE_FK FOREIGN KEY(PROFILE_ID) REFERENCES HOUSE_PROFILE(PROFILE_ID)
);
CREATE TABLE HOUSE_USER_GROUPE
(
USER_GROUPE_ID NUMBER(10) NOT NULL,
USER_GROUPE_NAME VARCHAR(255) NOT NULL,
CONSTRAINT USER_GROUPE_PK PRIMARY KEY(USER_GROUPE_ID)
);
CREATE TABLE HOUSE_USER_USER_GROUPE
(
USER_ID NUMBER(10) NOT NULL,
USER_GROUPE_ID NUMBER(10) NOT NULL,
CONSTRAINT USER_USER_GROUPE_PK PRIMARY KEY(USER_ID, USER_GROUPE_ID),
CONSTRAINT USER_FK FOREIGN KEY(USER_ID) REFERENCES HOUSE_USER(USER_ID),
CONSTRAINT USER_GROUPE_FK FOREIGN KEY(USER_GROUPE_ID) REFERENCES HOUSE_USER_GROUPE(USER_GROUPE_ID)
);
I need to ask now if these two constraints:
CONSTRAINT USER_FK FOREIGN KEY(USER_ID) REFERENCES HOUSE_USER(USER_ID),
CONSTRAINT USER_GROUPE_FK FOREIGN KEY(USER_GROUPE_ID) REFERENCES
are neccessary or not. I ask because i have another many to many relationship:
CREATE TABLE HOUSE_USER_GROUPE
(
USER_GROUPE_ID NUMBER(10) NOT NULL,
USER_GROUPE_NAME VARCHAR(255) NOT NULL,
CONSTRAINT USER_GROUPE_PK PRIMARY KEY(USER_GROUPE_ID)
);
CREATE TABLE HOUSE_ACCESSR
(
ACCESSR_ID NUMBER(10) NOT NULL,
ACCESSR_NAME VARCHAR(255) NOT NULL,
CONSTRAINT ACCESSR_PK PRIMARY KEY(ACCESSR_ID)
);
CREATE TABLE HOUSE_USER_GROUPE_ACCESR
(
USER_GROUPE_ID NUMBER(10) NOT NULL,
ACCESSR_ID NUMBER(10) NOT NULL,
CONSTRAINT USER_GROUPE_ACCESSR_PK PRIMARY KEY(USER_GROUPE_ID, ACCESSR_ID),
CONSTRAINT USER_GROUPE_FK FOREIGN KEY(USER_GROUPE_ID) REFERENCES HOUSE_USER_GROUPE(USER_GROUPE_ID),
CONSTRAINT ACCESSR_FK FOREIGN KEY(ACCESSR_ID) REFERENCES HOUSE_ACCESSR(ACCESSR_ID)
);
I cant create the second many to many table because i already used the constraint:
CONSTRAINT USER_GROUPE_FK FOREIGN KEY(USER_GROUPE_ID) REFERENCES HOUSE_USER_GROUPE(USER_GROUPE_ID),
I could just rename it but because of that error:
ORA-02264: name already used by an existing constraint
I just was wondering if these constraints are mandatory.
Yes, you should create the foreign key constrain on both tables.
The foreign key constraints are there to maintain referential integrity; ensuring that you can't insert values that don't exist in the parent table.
If you don't add the constraint to HOUSE_USER_GROUPE_ACCESR then you don't get that protection in that table. And you should want that protection everywhere.
Your only apparent mistake is that the constraint names are identical to each other. I traditionally either include No Name (letting Oracle decide on the name, because I never refer to the constraint by name) or use a format something like fk_<table>_<field>.
You need to do the constraints.. create the second constraints with another name.