Add a foreign key when creating a new table [closed] - sql

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

Related

Number of referencing columns must match referenced columns

I am relatively new to SQL and I keep getting the following error "The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list."
create table client
(name varchar(30),
phone int,
city varchar(20),
state char(2) CHECK(state='MN' OR state='ND' OR state='SD' OR state='WI' or state='IA'),
primary key(name,phone));
create table owns_vehicle
(name varchar(30),
phone int,
vin varchar(10),
primary key(name, phone, vin),
foreign key(name,phone) references client);
create table service_appointment
(mydate date,
vin varchar(10),
mechanic varchar(15),
description varchar(30) NOT NULL,
cost int CHECK (cost>=0),
primary key(mydate,vin),
foreign key(vin) references owns_vehicle);
This is the line that is causing the issue:
foreign key(vin) references owns_vehicle);
Does anyone know why I am getting this error?
You try to reference owns_vehicle, thus you need to reference all of the tables PRIMARY KEY columns, which would be name, phone, vin. But your foreign key has only vin, thus there are two columns (name, phone) missing.
The primary key has three parts. You need to reference all of them. If you are going to use this model, then you need the other two components:
create table service_appointment (
mydate date,
name varchar(30),
phone int,
vin varchar(10),
mechanic varchar(15),
description varchar(30) NOT NULL,
cost int CHECK (cost>=0),
primary key(mydate,vin),
foreign key(name, phone, vin) references owns_vehicle (name, phone, vin)
);
However, I think you data model should change. VINs can change owners. People can change phone numbers. These don't seem like good components of a primary key.
If you assume that a vehicle can only have a single owner at a given time, you can make your current data model work by changing owns_vehicle to have a uniqueness constraint on VIN:
CREATE TABLE OWNS_VEHICLE
(NAME VARCHAR2(30),
PHONE INT,
VIN VARCHAR2(10),
CONSTRAINT PK_OWNS_VEHICLE
PRIMARY KEY(NAME, PHONE, VIN)
USING INDEX,
CONSTRAINT OWNS_VEHICLE_UQ1
UNIQUE(VIN)
USING INDEX,
CONSTRAINT OWNS_VEHICLE_FK1
FOREIGN KEY(NAME,PHONE) REFERENCES CLIENT(NAME, PHONE));

Microsoft SQL Server Management Studio - Trying to add FK constraint, getting error: "Invalid column..."

I'm trying to add a foreign key constraint to link my tables, however I keep getting this error:
Msg 1769, Level 16, State 1, Line 1
Foreign key 'fk_customers_titles' references invalid column 'title_id' in referencing table 'customers'.
I've created my tables, along with it's columns and also stated my primary keys using the ALTER TABLE statement. But whenever I try to add a foreign key constraint for any table, I keep getting this error.
This is how I created my tables:
CREATE TABLE customers
(
customer_id char_idtype,
name varchar(50) NOT NULL,
contact_name varchar(30),
address varchar(50),
city varchar(20),
region varchar(15),
country_code varchar(10),
country varchar(15),
phone varchar(20),
fax varchar(20)
);
GO
CREATE TABLE titles
(
title_id char(3) NOT NULL,
description varchar(35) NOT NULL
);
GO
And this is how I added my primary keys:
ALTER TABLE customers
ADD PRIMARY KEY (customer_id);
GO
ALTER TABLE titles
ADD PRIMARY KEY (title_id);
GO
This is how I am adding my foreign key constraint:
ALTER TABLE customers
ADD CONSTRAINT fk_customers_titles
FOREIGN KEY (title_id)
REFERENCES titles(title_id);
I am using Microsoft SQL Server Management Studio 2012.
Your help is appreciated,
Thanks
I think the error is pretty clear: customers does not have a title_id. You need to add it . . .
ALTER TABLE customers ADD title_id char(3);
Or put it in the CREATE TABLE statement.
Your script should be like below mentioned.
CREATE TABLE titles
(
title_id char(3) Primary key NOT NULL,
description varchar(35) NOT NULL
);
GO
CREATE TABLE customers
(
customer_id char(3) primary key,
title_id char(3) foreign key references titles,
name varchar(50) NOT NULL,
contact_name varchar(30),
address varchar(50),
city varchar(20),
region varchar(15),
country_code varchar(10),
country varchar(15),
phone varchar(20),
fax varchar(20)
);
GO

#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).

CHECK constraint on a table [duplicate]

This question already has answers here:
adding CHECK to impose constraints on more than one table
(4 answers)
Closed 8 years ago.
I want to modify the following DDL to add CHECK constraints so that the manager of a store(FK employee_number is store table) works at the same store (FK store_code in employee table table) and a store supplies all the products if its type is 'local'.
Can anyone help?
CREATE TABLE employee(
employee_number CHAR(5) NOT NULL,
name VARCHAR(30),
store_code CHAR(5)
PRIMARY KEY(employee_number),
FOREIGN KEY(store_code) REFERENCES store
)
CREATE TABLE store(
store_code CHAR(5) NOT NULL,
type VARCHAR(15),
employee_number CHAR(5),
PRIMARY KEY(store_code),
FOREIGN KEY(employee_number) REFERENCES employee
)
CREATE TABLE product(
product_code CHAR(5) NOT NULL,
description VARCHAR(150),
cost DEC(10,2),
PRIMARY KEY(product_code)
)
CREATE TABLE stocks(
store_code CHAR(5) NOT NULL,
product_code CHAR(5) NOT NULL,
PRIMARY KEY(product_code, store_code),
FOREIGN KEY(product_key) REFERENCES product,
FOREIGN KEY(store_code) REFERENCES store
)
you could use a trigger to realize this
you can look at this question for an example Throw an error in a MySQL trigger