I am trying to run a migration script against a sqlite database. Below is the error I am getting when attempting the migration queries:
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
[Error: SQLITE_ERROR: near "categoryId": syntax error] {
errno: 1,
code: 'SQLITE_ERROR'
}
sql query I am running to create tables
CREATE TABLE IF NOT EXISTS "Category" (
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL );
CREATE TABLE IF NOT EXISTS "Location"(
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL );
CREATE TABLE IF NOT EXISTS "Job" (
id INTEGER PRIMARY KEY NOT NULL,
createdAt datetime NOT NULL default current_time,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
locationId INTEGER NOT NULL,
FOREIGN KEY(locationId) REFERENCES Location(id),
categoryId INTEGER NOT NULL,
FOREIGN KEY(categoryId) REFERENCES Category(id) );
You need to define all columns before defining the constraints, so swap the 2nd and 3rd last lines in your script:
CREATE TABLE IF NOT EXISTS "Job" (
id INTEGER PRIMARY KEY NOT NULL,
createdAt datetime NOT NULL default current_time,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
locationId INTEGER NOT NULL,
categoryId INTEGER NOT NULL,
FOREIGN KEY(locationId) REFERENCES Location(id),
FOREIGN KEY(categoryId) REFERENCES Category(id) );
Related
I have two tables:
CREATE TABLE Trasy_srodki_transportu(
ID_Trasy Integer NOT NULL,
ID_pojazdu Integer NOT NULL
)
/
CREATE TABLE Trasy(
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Now i want to add foreign key to Trasy_srodki_transportu referencing to Trasy table:
ALTER TABLE Trasy_srodki_transportu ADD CONSTRAINT Trasa_jest_wykorzystywana FOREIGN KEY (ID_Trasy) REFERENCES Trasy (ID_Trasy)
/
and this throws Oracle (ORA-02270) : no matching unique or primary key for this column-list error. Any suggestions how to fix this?Data modeler view
A foreign key needs to reference a key on the related table, but it's not the case in your example. Change the definition of the second table by adding a PRIMARY KEY constraint in it, as in:
CREATE TABLE Trasy (
ID_Trasy Integer PRIMARY KEY NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL
)
Alternatively, you can create a unique constraint on it, that can also serve as a key. For example:
CREATE TABLE Trasy (
ID_Trasy Integer NOT NULL,
Linia Varchar2(4 ) NOT NULL,
Data_rozpoczecia_kursowania Date NOT NULL,
Data_zakonczenia_kursowania Date,
ID_Pracownika Integer NOT NULL,
CONSTRAINT uq_idtrasy UNIQUE (ID_Trasy)
)
I keep getting the syntax error in access when trying to add a foreign key to a table. The foreign key is LendorID and it is referenced in another table. What am I doing wrong?
Here is my code for creating the table:
CREATE TABLE BOOK_INVENTORY
(
CRN int NOT NULL PRIMARY KEY,
AuthorLastName varchar(20) NOT NULL,
AuthorFirstName varchar(20) NOT NULL,
BookTitle varchar (100) NOT NULL,
LendorID varchar(5) FOREIGN KEY REFERENCES LENDOR_INFO(LendorID),
);
Here is my code for the table that contains LendorID:
CREATE TABLE LENDOR_INFO
(
LendorID varchar(5) NOT NULL,
LendorLastName varchar(20) NOT NULL,
LendorFirstName varchar(20) NOT NULL,
LendorEmail varchar(100) NOT NULL,
PRIMARY KEY (LendorID)
);
I am new to sql so I have a problem where I am asked to create a database for a city. The database I created with DB Browser.
The database code is below. When I try to use the database to add anything to it or even see if it is created successfully I get an error:
SQL logic error near "DATABASE".
I am not sure what is wrong with the code I wrote.
CREATE DATABASE City;
CREATE TABLE IF NOT EXISTS "OfficialEmployee" (
"EID" INTEGER NOT NULL,
"FirstName" TEXT NOT NULL,
"LastName" TEXT NOT NULL,
"BirthDate" TEXT NOT NULL,
"City" TEXT NOT NULL,
"StreetName" TEXT NOT NULL,
"Number" INTEGER NOT NULL,
"Door" INTEGER NOT NULL,
"StartWorkingDate" TEXT NOT NULL,
"Degree" TEXT NOT NULL,
"DID" TEXT NOT NULL,
PRIMARY KEY("EID"),
FOREIGN KEY("DID") REFERENCES "Department"("DID") On delete cascade on update cascade
);
CREATE TABLE IF NOT EXISTS "ConstructorEmployee" (
"EID" INTEGER NOT NULL,
"FirstName" TEXT NOT NULL,
"LastName" TEXT NOT NULL,
"BirthDate" TEXT NOT NULL,
"City" TEXT NOT NULL,
"StreetName" TEXT NOT NULL,
"Number" INTEGER NOT NULL,
"Door" INTEGER NOT NULL,
"CompanyName" TEXT NOT NULL,
"SalaryPerDay" INTEGER NOT NULL,
PRIMARY KEY("EID")
);
CREATE TABLE IF NOT EXISTS "Employee_Phones" (
"EID" INTEGER NOT NULL,
"CellPhoneNumber" INTEGER NOT NULL,
PRIMARY KEY("EID","CellPhoneNumber")
);
CREATE TABLE IF NOT EXISTS "Department" (
"DID" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
"Description" TEXT NOT NULL,
"ManagerID" INTEGER,
PRIMARY KEY("DID"),
FOREIGN KEY("ManagerID") REFERENCES "OfficialEmployee"("EID") On Update cascade on delete no action
);
CREATE TABLE IF NOT EXISTS "Neighborhood" (
"NID" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
PRIMARY KEY("NID")
);
CREATE TABLE IF NOT EXISTS "Apartment" (
"StreetName" TEXT NOT NULL,
"Number" INTEGER NOT NULL,
"Door" INTEGER NOT NULL,
"Type" TEXT NOT NULL,
"SizeSquareMeter" INTEGER NOT NULL,
"NID" INTEGER NOT NULL,
FOREIGN KEY("NID") REFERENCES "Neighborhood"("NID") on delete no action on update no action,
PRIMARY KEY("Door","StreetName","Number")
);
CREATE TABLE IF NOT EXISTS "Project" (
"PID" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
"Description" TEXT NOT NULL,
"Budget" INTEGER NOT NULL,
"NID" INTEGER NOT NULL,
PRIMARY KEY("PID"),
FOREIGN KEY("NID") REFERENCES "Neighborhood"("NID") On Update no action on delete no action
);
CREATE TABLE IF NOT EXISTS "ProjectConstructorEmployee" (
"EID" INTEGER NOT NULL,
"PID" INTEGER NOT NULL,
"StartWorkingDate" TEXT NOT NULL,
"EndWorkingDate" TEXT,
"JobDescription" TEXT NOT NULL,
FOREIGN KEY("PID") REFERENCES "Project"("PID") On Update Cascade on Delete No Action,
FOREIGN KEY("EID") REFERENCES "ConstructorEmployee"("EID") On Update Cascade On Delete No Action
);
CREATE TABLE IF NOT EXISTS "Resident" (
"RID" INTEGER NOT NULL,
"FirstName" TEXT NOT NULL,
"LastName" TEXT NOT NULL,
"BirthDate" TEXT NOT NULL,
"StreetName" TEXT NOT NULL,
"Number" INTEGER NOT NULL,
"Door" INTEGER NOT NULL,
"CatalogID" INTEGER NOT NULL,
PRIMARY KEY("RID"),
FOREIGN KEY("Door") REFERENCES "Apartment"("Door") On update cascade on delete no action,
FOREIGN KEY("StreetName") REFERENCES "Apartment"("StreetName") On Update cascade on delete no action,
FOREIGN KEY("Number") REFERENCES "Apartment"("Number") On Update cascade on delete no action
);
CREATE TABLE IF NOT EXISTS "TrashCan" (
"CatalogID" INTEGER NOT NULL,
"CreationDate" TEXT NOT NULL,
"ExpirationDate" TEXT NOT NULL CHECK(ExpirationDate>CreationDate),
PRIMARY KEY("CatalogID")
);
CREATE TABLE IF NOT EXISTS "ParkingArea" (
"AID" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
"PricePerHour" INTEGER NOT NULL,
"MaxPricePerDay" INTEGER NOT NULL,
"NID" INTEGER NOT NULL,
PRIMARY KEY("AID"),
FOREIGN KEY("NID") REFERENCES "Neighborhood"("NID") On Update cascade on delete cascade
);
CREATE TABLE IF NOT EXISTS "CarParking" (
"CID" INTEGER NOT NULL,
"StartTime" TEXT NOT NULL,
"EndTime" TEXT NOT NULL CHECK(StartTime<EndTime),
"Cost" INTEGER NOT NULL CHECK(MaxPricePerDay>Cost),
"AID" INTEGER NOT NULL,
"MaxPricePerDay" INTEGER NOT NULL,
PRIMARY KEY("CID","StartTime"),
FOREIGN KEY("CID") REFERENCES "Cars"("CID") On Update Cascade on delete cascade,
FOREIGN KEY("AID") REFERENCES "ParkingArea"("AID") On Delete set null on update cascade
);
I don't believe you can create a database from within a database in SQLite
Just do this, on the command line:
UNIX: $ touch my.db
Windows: c:> type nul > my.db
Then you can open the database using DB Browser
I Got a table BILL which is associated to another table BANK_CARD as follows:
create table BILL (
id_bill BIGSERIAL not null,
id_bank_card BIGSERIAL null,
id_registred_user BIGSERIAL not null,
reference_number INT4 null,
purchase_date DATE null,
bill_status VARCHAR(50) null,
payment_method VARCHAR(50) null,
constraint PK_BILL primary key (id_bill)
);
create table BANK_CARD (
id_bank_card BIGSERIAL not null,
id_registred_user BIGSERIAL not null,
card_type VARCHAR(50) null,
card_number INT4 null,
expiring_date DATE null,
cipher INT4 null,
constraint PK_BANK_CARD primary key (id_bank_card)
);
The table BILL has a 0..1 association with the table BANK_CARD, and BANK_CARD has a 1..n association with table BILL.
But when i execute my sql script i get the following error:
conflicting NULL/NOT NULL declarations for column "id_bank_card" of table "bill"
Because the relationship BILL and BANK_CARD is 0..1 the foreign key id_bank_card can be null in the table bill, so i don't understand why i get this error.
Any help please?
Thanks.
You are confusing data type definitions for primary and foreign keys. A bigserial is a generator of values of the bigint type. The foreign keys should use that data type. See table definitions below. Also, use of the NULL clause is redundant as that is the default behaviour. Primary keys can not be NULL so NOT NULL there is also redundant.
create table bank_card (
id_bank_card bigserial,
id_registred_user bigint references <user table>,
card_type VARCHAR(50),
card_number INT4,
expiring_date DATE,
cipher INT4,
constraint PK_BANK_CARD primary key (id_bank_card)
);
create table bill (
id_bill BIGSERIAL,
id_bank_card bigint references bank_card,
id_registred_user bigint references <user table>,
reference_number INT4,
purchase_date DATE,
bill_status VARCHAR(50),
payment_method VARCHAR(50),
constraint pk_bill primary key (id_bill)
);
If you are using Java+PostgreSQL:
this can happen if you accidentially insert null strings into your statement.
String N = System.getProperty("line.seperator");
Returned null, screwing up my sql statement.
FIXES: (Choose One)
String N = System.lineSeparator();
String N = "\n";
Exact string as it appeared in my code:
String CT =(""
//:+"---------10--------20--------30-------39
//:+"0123456789012345678901234567890123456789
+"CREATE TABLE t_1( "+N//:0
+"id SERIAL PRIMARY KEY "+N//:30
+" ,c_1 INT "+N//:60
+" ,c_2 VARCHAR (255) "+N//:90
+" ,c_3 CHARACTER( 8 ) "+N//:120
+" ,c_4 BOOLEAN "+N//:150
+" ,c_5 BYTEA "+N//:180
+" ,c_6 DATE "+N//:210
+"); " //:240
);;
Exact Error Message when N==null instead of "\n":
org.postgresql.util.PSQLException:
ERROR: conflicting NULL/NOT NULL declarations
for column "nullid" of table "t_1"
Stack: Java+Heroku+Tomcat9+PostGreSQL 9.5.13
I have the following SQL I am running in my local HSQLDB 2.3.2 database:
CREATE TABLE IF NOT EXISTS countries (
country_id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL PRIMARY KEY,
country_version INTEGER NOT NULL,
country_name NVARCHAR(100) NOT NULL,
country_label NVARCHAR(100) NOT NULL,
country_description NVARCHAR(500) NOT NULL
country_code NVARCHAR(10) NOT NULL,
CONSTRAINT uc_countries UNIQUE (country_id, country_version, country_label, country_description, country_code)
);
CREATE TABLE IF NOT EXISTS states (
state_id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL PRIMARY KEY,
state_version INTEGER NOT NULL
state_name NVARCHAR(100) NOT NULL,
state_label NVARCHAR(100) NOT NULL,
state_description NVARCHAR(500) NOT NULL,
country_id INT NOT NULL,
FOREIGN KEY (country_id) REFERENCES countries(country_id),
CONSTRAINT uc_states UNIQUE (state_id, state_version, state_label, state_description, country_id)
);
When I run this I get:
Error: unexpected token: FOREIGN
SQLState: 42581
ErrorCode: -5581
What is going on here, and what can I do to fix it?
Is there a difference between INT NOT NULL and INTEGER NOT NULL? Because you have defined the country_id fields by both.
Also, if you insert SET REFERENTIAL_INTEGRITY FALSE; at the top, do you still get this error?