SQL Server Foreign Key Constraint References Invalid Table - sql

I have the following code:
CREATE TABLE _CLIENT
(
client_id int ,
client_name varchar(50),
type varchar(50),
constraint _CLIENT_pk PRIMARY KEY(client_id),
constraint _CLIENT_ch CHECK (client_id>0),
typee_id INT NOT NULL REFERENCES CLIENT_TYPE(typee_id)
)
CREATE TABLE CLIENT_TYPE
(
typee_id int NOT NULL,
name_type varchar(50),
constraint CLIENT_TYPE_pk PRIMARY KEY(typee_id)
)
The foreign key throws an error saying:
Foreign key 'FK__Number__Name__1CF15040' references invalid table 'Users.Name'
what's the wrong here?

I don't know what exact error message you are getting, but you have error in the current script and I think you mean this error:
Foreign key 'FK___CLIENT__typee_i__55BFB948' references invalid table
'CLIENT_TYPE'.
You should first create CLIENT_TYPE table, so the script should look like:
CREATE TABLE CLIENT_TYPE
(
typee_id INT NOT NULL ,
name_type VARCHAR(50) ,
CONSTRAINT CLIENT_TYPE_pk PRIMARY KEY ( typee_id )
)
CREATE TABLE _CLIENT
(
client_id INT ,
client_name VARCHAR(50) ,
type VARCHAR(50) ,
CONSTRAINT _CLIENT_pk PRIMARY KEY ( client_id ) ,
CONSTRAINT _CLIENT_ch CHECK ( client_id > 0 ) ,
typee_id INT NOT NULL
REFERENCES CLIENT_TYPE ( typee_id )
)
As a general rule, you should first create base tables and then tables which depend on those ones.

Related

multiple one-to-one relationship mysql

is it possible to create table with multiple one-to-one relationships?
I am tring to obtain it via below query but get SQL error.
Tables are:
Order:
id
cart id (1:1)
user id (1:1)
payment_method_id
shipping_method_id
total price
User
id
email
phone
first name
last name
address
post code
city
password NULL
Cart:
id
cookie
cartItem_id(1:many)
grandTotal
I would like to create table Order with two columns having one-to-one association.
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(user_id)
, primary key(cart_id));
I copied the below query but I am getting error and don't know why.
create table order(id int auto_increment, cart_id int, user_id int, payment_method_id int, shipping_method_id int, total_price int, primary key(id), foreign key (user_id) references user(id), foreign key (cart_id) references cart(id));
it says:
[42000][1064] You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'order(id int auto_increment, cart_id int, user_id int,
payment_method_id int, sh' at line 1
What is it I cannot see? I only changed Id into id.
Firstly, You can't have more than one Primary key on a Table.
Secondly, To have a relation you need to use Foreign Key
Try below Query:
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(id)
, FOREIGN KEY (user_id) REFERENCES User(Id)
, FOREIGN KEY (cart_id) REFERENCES Cart(Id));

ALTER TABLE statement conflicted with the FOREIGN KEY constraint

When creating the foreign key I came across this error. Below is my code.
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int
)
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
alter table tblPerson add constraint tblPerson_GenderId_FK
foreign key (GenderId) references tblGender(ID)
You want to identify any "do not align" rows....
I have made the below.
You won't be able to add the FK constraint, if any rows come back from the SELECT query.
I have also removed the hungarian notation for "tbl". I would advise against it.
create table dbo.Person(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int )
create table dbo.Gender (
ID int not null primary key,
Gender varchar(50) not null
)
/* any rows below? your FK creation will fail */
Select *, p.GenderId as 'HoustonWeHaveAProblemValue' from dbo.Person p Where Not Exists (Select 1 from dbo.Gender g where g.ID = p.GenderId)
alter table dbo.Person add constraint Person_GenderId_FK
foreign key (GenderId) references dbo.Gender(ID)
Hi please use the following code to achieve your goal :
1-First create tblGender:
create table tblGender (
ID int not null primary key,
Gender varchar(50) not null
)
2-Then create table tblPerson with the relationship between 2 tables since the beginning:
create table tblPerson(
ID int not null primary key,
Fullname varchar(50) not null,
Email varchar(50) not null,
GenderId int references tblGender(ID)
)
works fine.

Relationship 1 to n

create database shop1
use shop1
create table mathang
(
MatHangID INT primary key not null,
TenMatHang varchar(50),
SoLuong int not null,
Price int not null,
)
create table nhacungcap
(
MatHangID INT foreign key references mathang,
TenNhaCungCap varchar(50) ,
DiaChi varchar(100),
SoDienThoai int ,
CONSTRAINT pk_M_CC primary key (MatHangID)
)
create table khachhang
(
KhachHangID int not null primary key,
TenKhachHang varchar(50) not null,
[DiaChi] varchar(100) not null,
[SoDienThoai] varchar(50) not null,
)
create table donhang
(
DonHangID int references to khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
I can not find errors with this, it shows the foreign key error to add this relation ship. Any one please help.
Remove the to in
...
DonHangID int references to khachhang(KhachHangID)
...
to get
...
DonHangID int references khachhang(KhachHangID)
...
. The to doesn't belong there syntactically.

SQL Server : there are no primary or candidate keys in the referenced table

I using SQL Server 2008, and when I try create a new table, in existing DB, this error appears:
There are no primary or candidate keys in the referenced table 'parceria_conta_corrente_ao' that match the referencing column list in the foreign key 'R_795'.
This table exists:
And I try create a new table with this code:
CREATE TABLE parceria_item_resgate_rateio_aux
(
id_parceria_item_resgate_rateio_aux int NOT NULL IDENTITY,
dt_conta_corrente DATETIME NOT NULL ,
id_periodo BIGINT NOT NULL ,
id_ao bigint NOT NULL ,
id_gr_cliente int NOT NULL ,
id_cliente BIGINT NOT NULL ,
data_importacao_cli_gr_cli DATETIME NOT NULL ,
hp2 varchar(50) NOT NULL ,
hp2_filho varchar(50) NOT NULL ,
valor_nc decimal(18,2) NULL ,
datetime_inclusion datetime NOT NULL ,
status int NULL ,
CONSTRAINT XPKparceria_item_resgate_ PRIMARY KEY CLUSTERED
(id_parceria_item_resgate_rateio_aux ASC,
dt_conta_corrente ASC,
id_periodo ASC,
id_ao ASC,
id_gr_cliente ASC,
id_cliente ASC,
data_importacao_cli_gr_cli ASC,
hp2 ASC),
CONSTRAINT R_795 FOREIGN KEY(dt_conta_corrente, id_periodo, id_ao, id_gr_cliente, id_cliente, data_importacao_cli_gr_cli, hp2)
REFERENCES parceria_conta_corrente_ao(dt_conta_corrente, id_periodo, id_ao, id_gr_cliente, id_cliente, data_importacao_cli_gr_cli, hp2)
ON DELETE CASCADE
ON UPDATE CASCADE
)
go
Where is the problem?
You need to create a unique index on the referenced table:
CREATE UNIQUE INDEX UX_parceria_conta_corrente_ao
ON parceria_conta_corrente_ao
(
dt_conta_corrente,
id_periodo,
id_ao,
id_gr_cliente,
id_cliente,
data_importacao_cli_gr_cli,
hp2
)
EDIT:
I guess the columns are not in the same order, columns in primary key must be in the same order than the columns in the foreing key.
If you execute the following:
CREATE TABLE T
(
C1 int NOT NULL,
C2 int NOT NULL,
PRIMARY KEY (C1, C2)
)
CREATE TABLE T2
(
id INT NOT NULL,
C1 int NOT NULL,
C2 int NOT NULL,
CONSTRAINT FK1 FOREIGN KEY (C2, C1) REFERENCES T(C2, C1)
)
You get the following error:
Msg 1776, Level 16, State 0, Line 9 There are no primary or candidate
keys in the referenced table 'T' that match the referencing column
list in the foreign key 'FK1'. Msg 1750, Level 16, State 0, Line 9
Could not create constraint or index. See previous errors.

Foreign Key creation in Oracle [duplicate]

This question already has an answer here:
ORA-00907: missing right parenthesis
(1 answer)
Closed 8 years ago.
I tried to create Foreign key and PRIMARY KEY
My code is ok with Table1
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Table2:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
error :-ORA-00907: missing right parenthesis
Don't know what the error is.
When you declare the FK "inline" you must not specify the foreign key keyword:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
Btw: you don't even need to list the column name when using an inline definition, P_Id int REFERENCES Persons would be enough.
That will generate a system-named constraint (e.g. SYS_C0066866), so it's generally better to use a format where you can specify the name of the constraint:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
constraint fk_orders_person foreign key (p_id) REFERENCES Persons(P_Id)
);
If you're inlining your foreign key definition in the column's definition, you don't need the foreign key phrase:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int REFERENCES Persons(P_Id)
);
For MySql
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int,
FOREIGN KEY(P_Id) REFERENCES Persons(P_Id)
);
For SQL
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int(10),
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
);
Check Manual