SQL Server errors with foreign key conflict [closed] - sql

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.

Related

Add a foreign key when creating a new 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 1 year ago.
Improve this question
I'm trying to create a new table (Customer) and I'm trying to add the foreign key ZipCode, but its definitely not working I tried looking for possible solutions but I couldn't make it work with what I got. Any help is appreciated, thanks!
CREATE TABLE Zip_Code (
ZipCode varchar(10) Not null,
City varchar(20),
StateCode varchar(2)
);
ALTER TABLE Zip_Code
ADD Primary Key (ZipCode);
CREATE TABLE Customer (
CustomerID INT Primary Key,
CustomerName varchar(30),
Address varchar(30),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode),
CreditLimit money,
Balance money,
);
The problem is not the primary key definition. The problem is customer. In order to declare a column as a foreign key, you have to define the column first:
CREATE TABLE Customer (
CustomerID INT Primary Key,
CustomerName varchar(30),
Address varchar(30),
ZipCode varchar(10),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode),
CreditLimit money,
Balance money,
);
Here is a db<>fiddle.
MY SQL:
CREATE TABLE Customer (
CustomerID int NOT NULL,
ZipCode int,
CustomerName varchar(30),
Address varchar(30),
PRIMARY KEY (CustomerID),
FOREIGN KEY (ZipCode) REFERENCES Zip_Code(ZipCode)
);
SQL Server / Oracle:
CREATE TABLE Customer (
CustomerID int NOT NULL PRIMARY KEY,
CustomerName varchar(30),
Address varchar(30),
ZipCode int FOREIGN KEY REFERENCES Zip_Code(ZipCode)
);

ORA-00907: Missing right parenthesis [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 7 years ago.
Improve this question
I have this code (for example)
CREATE TABLE s(
id int,
y int NOT NULL UNQIUE,
x int NOT NULL,
z varchar(50) NOT NULL,
k varchar(50),
l varchar(50) NOT NULL,
m int NOT NULL,
v int NOT NULL,
c int NOT NULL,
r varchar(400),
CONSTRAINT s_pk PRIMARY KEY (id));
But i get ORA-00907: Missing right parenthesis. What I made wrong?
CREATE TABLE s(
id int,
y int NOT NULL UNIQUE,
x int NOT NULL,
z varchar(50) NOT NULL,
k varchar(50),
l varchar(50) NOT NULL,
m int NOT NULL,
v int NOT NULL,
c int NOT NULL,
r varchar(400),
CONSTRAINT s_pk PRIMARY KEY (id));

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

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
);

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 do I make a table that is creating itself from another table in SQL Server [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 9 years ago.
Improve this question
Table of the self creates tRestituiri is tImprumuturi
create table tImprumuturi
(
ID_Imprumut int identity primary key,
DataImprumut date default getdate(),
DataScadenta as (dateadd(day,5,DataImprumut)) persisted,
CodCD char(10) foreign key references tCD(CodCd)not null,
CodCV char(10) foreign key references tCaseteVideo(CodCaseta),
CodAb char(10) foreign key references tAbonati(CodAbonat) not null,
CONSTRAINT ucCodCD UNIQUE (CodCD, CodCV),
CONSTRAINT ucCodCV UNIQUE (CodCD, CodAb),
CONSTRAINT ucCodAb UNIQUE (CodCV, CodAb)
)
I want that tRestituiri to contain columns like:
tRestituiri
(
ID_Restituire int identity primary key,
DataRestituire // it should be equal with DataScadenta from tImprumuturi
CodCD char(10) foreign key references tCD(CodCD),
CodCV char(10) foreign key references tCaseteVido(CodCaseta),
CodAb char(10) foreign key references tAbonati(CodAbonat)
)
BUT tRestituiri have to contain only values from tImprumuturi where DataScadenta is not older then current time {getdate()}-60 days** *I mean values from tImprumuturi where DataScadenta is not less than current time -2 months *
Insert into tRestituiri (DataRestituire,CodCD , CodCV,CodAB)
SELECT DataScadenta,CodCD , CodCV,CodAB FROM tImprumuturi WHERE DataScadenta>GETDATE()-60