#1215 SQL Cannot add foreign key constraint error [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my code. I can't figure out why I can't add the constraint.
Create table customer(
UserId Integer
)
CREATE TABLE Date(
DateID INTEGER,
User1ID INTEGER NOT NULL,
User2ID INTEGER NOT NULL,
Date CHAR(20) NOT NULL,
GeoLocation CHAR(20) NOT NULL,
BookingFee INTEGER NOT NULL,
CustomerRepresentative CHAR(20) NOT NULL,
Comments CHAR(200),
User1Ratings CHAR(20),
User2Ratings CHAR(20),
Primary Key (DateID),
Check ( User1Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
Check ( User2Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
FOREIGN KEY (User1ID) REFERENCES customer(UserID)
)

The most likely explanation for the behavior is that UserID column is not defined as the PRIMARY KEY or a UNIQUE KEY in the customer table
One fix for this would be to re-write the create for the customer table
For SQL Server:
CREATE TABLE customer
( UserId Integer NOT NULL
, CONSTRAINT PK_customer PRIMARY KEY CLUSTERED (UserId)
);
For MySQL:
CREATE TABLE customer
( UserId INTEGER NOT NULL PRIMARY KEY
);

Related

reference primary key from another table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I have to create 2 tables.
the first one
CREATE TABLE orders
( order_id number(10) NOT NULL,
order_name varchar2(50) NOT NULL,
payment_id number(10) NOT NULL,
CONSTRAINT order_id PRIMARY KEY (order_id),
);
and when creating the second one I got this error
ORA-02270: no matching unique or primary key for this column-list
CREATE TABLE payment
(
payments_id number(10) NOT NULL,
payment_name varchar(50) NOT NULL,
CONSTRAINT payments_id PRIMARY KEY (payments_id),
FOREIGN KEY (payments_id) REFERENCES orders(payment_id)
);
not sure what I'm doing wrong
please help
You need to reference a UNIQUE or PRIMARY KEY column. The payment_id column does not have one of those constraints on it.
From the Oracle constraint documentation:
Foreign Key Constraints
A foreign key constraint (also called a referential integrity constraint) designates a column as the foreign key and establishes a relationship between that foreign key and a specified primary or unique key, called the referenced key.
Instead, you can add an order_id column to your table:
CREATE TABLE orders(
order_id NUMBER(10) NOT NULL,
order_name VARCHAR2(50) NOT NULL,
CONSTRAINT orders__order_id__pk PRIMARY KEY (order_id)
);
CREATE TABLE payment(
payments_id NUMBER(10) NOT NULL,
payment_name VARCHAR2(50) NOT NULL,
order_id NOT NULL,
CONSTRAINT payment__payments_id__pk PRIMARY KEY (payments_id),
CONSTRAINT payment__order_id__fk FOREIGN KEY (order_id)
REFERENCES orders (order_id)
);
db<>fiddle here

me I get the error ORA-00904: : invalid identifier (im a new in sql) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
create table Factura
(
FacturaID smallint not null,
FacturaFecha date,
FacturaObservacion varchar(1000),
constraint pkFactura primary key (FacturaID),
constraint fkFactura foreign key (LocalID) references Cliente(ClienteID),
constraint fkFactura foreign key (VendedorID) references Vendedor(VendedorID),
constraint fkFactura foreign key (ClienteID) references Local1(LocalID),
);
create table Local1
(
LocalID smallint not null,
LocalNombre varchar(100),
constraint pkLocal1 primary key (LocalID)
);
create table Vendedor
(
VendedorID smallint not null,
VendedorNombres varchar(40),
VendedorApellidos varchar(40),
constraint pkVendedor primary key (VendedorID)
);
create table Cliente
(
ClienteID smallint not null,
ClienteDireccion varchar(40) not null,
ClienteCiudad varchar(30) not null,
constraint pkCliente primary key (ClienteID)
);
It seems you probably got your code from another database such as MySQL, SQL Server, or DB2.
Oracle doesn't have the SMALLINT data type (that I replaced with NUMBER(6)), and discourages the use of the VARCHAR data type (that I replaced by VARCHAR2). You also forgot to add a few columns.
I modified your SQL statements and now they run in Oracle. See below:
create table Local1
(
LocalID number(6) not null,
LocalNombre varchar2(100),
constraint pkLocal1 primary key (LocalID)
);
create table Vendedor
(
VendedorID number(6) not null,
VendedorNombres varchar2(40),
VendedorApellidos varchar2(40),
constraint pkVendedor primary key (VendedorID)
);
create table Cliente
(
ClienteID number(6) not null,
ClienteDireccion varchar2(40) not null,
ClienteCiudad varchar2(30) not null,
constraint pkCliente primary key (ClienteID)
);
create table Factura
(
FacturaID number(6) not null,
FacturaFecha date,
FacturaObservacion varchar2(1000),
LocalID number(6),
VendedorID number(6),
ClienteID number(6),
constraint pkFactura primary key (FacturaID),
constraint fkFactura1 foreign key (LocalID) references Cliente(ClienteID),
constraint fkFactura2 foreign key (VendedorID) references Vendedor(VendedorID),
constraint fkFactura3 foreign key (ClienteID) references Local1(LocalID)
);
See them running at SQL Fiddle.

SQL Server errors with foreign key conflict [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have this code here:
create database shop11
use shop11
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 khachhang(KhachHangID),
TenDonHang varchar(50),
SoLuong int,
CONSTRAINT pk_DHID primary key (DonHangID)
)
INSERT INTO khachhang
VALUES ('1','TOMMY','VIETNAM','123456789'),('2','TONY','VIETNAM','987654321'),
('3','TOMY','VIETNAM','1234567891'),('4','JOHNNY','VIETNAM','356112789'),
('6','KENNY','VIETNAM','1223236789')
INSERT INTO donhang
VALUES ('1','LAPTOP ACER',100), ('2','LAPTOP ASUS',10),
('3','LAPTOP MSI',5), ('4','ZENPHONE ASUS',10),
('5','NOTEBOOK HP',10), ('6','IPHONE',10),
('7','MACBOOK PRO',10)
Here is the error I get. How can I fix it, and how about this errors with foreign key :( Please help, thanks
enter image description here
You are referencing khachhang.KhachHangID on khachhang.KhachHangID
Values
('5','NOTEBOOK HP',10)
('7','MACBOOK PRO',10)
do not match on khachhang table.

Sql schema error on create table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
it generates an error,i cant understand whats wrong with the script..every suggestion would be helpful thnx
CREATE TABLE ar_abonent
(
ar_nr_klienti_primary_key int,
emri varchar(25),
mbiemri varchar(25)
);
create table ar_celular
(
NR_CEL INT (12),
ar_nr_klienti FOREIGN Key REFERENCES ar_abonent (ar_nr_klienti),int,
identifikues boolean,
data_aktivizimit date,
marka varchar(25)
constraint chk_celular check (NR_CEL IN ('35566%','35567%','35568%','35569%' AND IDENTIFIKUES='TRUE')
);
CREATE TABLE ar_abonent
(
ar_nr_klienti int NOT NULL primary key , --<-- This column needs to be a primary key
emri varchar(25),
mbiemri varchar(25)
);
create table ar_celular
(
NR_CEL INT ,
ar_nr_klienti int FOREIGN Key REFERENCES ar_abonent (ar_nr_klienti) ,
identifikues bit,
data_aktivizimit date,
marka varchar(25),
constraint chk_celular check (NR_CEL IN ('35566%','35567%','35568%','35569%') AND IDENTIFIKUES= 1)
);
I tried correcting it using SQL Fiddle (set to Oracle 11G R2) as I don't own any Oracle servers. This is how it should look to run without issues:
CREATE TABLE ar_abonent
(
ar_nr_klienti int NOT NULL PRIMARY KEY ,
emri VARCHAR2(25),
mbiemri VARCHAR2(25)
);
CREATE TABLE ar_celular
(
NR_CEL int,
ar_nr_klienti int,
identifikues number(1),
data_aktivizimit date,
marka VARCHAR2(25),
CONSTRAINT fk_column FOREIGN KEY (ar_nr_klienti) REFERENCES ar_abonent (ar_nr_klienti),
CONSTRAINT chk_celular CHECK (NR_CEL IN ('35566%','35567%','35568%','35569%') AND IDENTIFIKUES= 1)
);
Sample SQL Fiddle
Turns out Oracle doesn't have a BOOLEAN type (at table level, although it does exist in PL/SQL), and VARCHAR2 might be preferred to VARCHAR (unless nulls are a concern).

How to create Relational Table in sql database(wamp) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two tables named student_table(student_id,student_name),teacher_table(teacher_id,teacher_name). Now I want to create a relational table named teacher_student_table which column will be (id,student_id,teacher_id).
In student_table field named student_id is auto increment and in teacher_table field named teacher_id is auto increment.
And in teacher_student_table field named id is also auto_increment.Now how I can make this type of relational table? need help. I don't know how to create this in sql(wamp localhost)
You also want to have foreign-keys to the other tables.
CREATE TABLE teacher_student_table
(
id int NOT NULL AUTO_INCREMENT,
student_id int NOT NULL,
teacher_id int NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (student_id) REFERENCES student_table(student_id),
FOREIGN KEY (teacher_id) REFERENCES teacher_table(teacher_id)
)
This is very basic SQL and you should easily have been able to look it up in your course litterature.
CREATE TABLE teacher_student_table
(
id int NOT NULL AUTO_INCREMENT,
student_id int NOT NULL,
teacher_id int NOT NULL,
PRIMARY KEY (ID)
FOREIGN KEY (student_id) REFERENCES student_table(student_id),
FOREIGN KEY (teacher_id) REFERENCES teacher_table(teacher_id)
)