Sql schema error on create table [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 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).

Related

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.

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

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.

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

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