My question is about running CREATE TABLE with a SERIAL PRIMARY KEY, but keep getting errors. Even after I correct the code based on the errors. the errors persist. Any help is appreciated.
DROP TABLE IF EXISTS nm_land_grants;
CREATE TABLE nm_land_grants (
ID SERIAL, land_grant INT,
area REAL, perimeter REAL,
grant_name VARCHAR(255),
land_grant INT, land_gra_1 INT,
survey_app DATE, grant_conf DATE
CONSTRAINT pk_nm_land_grants
PRIMARY KEY (ID)
);
The error I'm getting is as follows:
Done.
(psycopg2.ProgrammingError) syntax error at or near "("
LINE 6: PRIMARY KEY (ID)
^
[SQL: 'CREATE TABLE nm_land_grants (\n ID SERIAL, land_grant INT, area
REAL, perimeter REAL,\n grant_name VARCHAR(255), land_grant INT,
land_gra_1 INT,\n survey_app DATE, grant_conf DATE\n CONSTRAINT
pk_nm_land_grants\n PRIMARY KEY (ID)\n);']
Related
Hey I have a Postgres database that has a Schema with
CREATE TABLE Mentor (
mentor_ID serial unique,
person_ID serial not null unique,
career_history varchar(255) not null,
preferred_communication varchar(50) not null,
mentoring_preference varchar(50) not null,
linked_in varchar(100) not null,
capacity int not null,
feedback_rating int,
feeback_comment varchar(255),
PRIMARY KEY (mentor_ID),
CONSTRAINT fk_person FOREIGN KEY (person_ID) REFERENCES Person(person_ID)
);
CREATE TABLE Mentee(
mentee_ID integer not null unique,
mentor_ID serial references Mentor(mentor_ID),
person_ID serial not null unique,
study_year int,
motivation varchar(50),
interests varchar(255),
random_match boolean default false,
PRIMARY KEY (mentee_ID),
CONSTRAINT fk_person FOREIGN KEY (person_ID) REFERENCES Person(person_ID)
);
With this, i expect to be able to enter null values for mentor_ID in my database but when I enter the query
insert into mentee(mentee_ID, mentor_ID, person_ID) VALUES (12313, null, 1)
I get the violation
ERROR: null value in column "mentor_id" of relation "mentee" violates not-null constraint
I was wondering how I could make it so I can insert null values for mentor_ID? I dont have it as not null in the table but it still says violating not null constraint.
Thank you
Because serial is not null.
serial is...
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Note the integer not null. This is because serial is to be used for primary keys, not foreign keys. Foreign keys are always assigned, they don't need to auto increment.
Use a plain integer.
mentor_ID integer references Mentor(mentor_ID)
Same for your other foreign keys.
Notes:
identity is the SQL standard way to do auto incremented primary keys.
You don't need to declare primary keys as unique, primary keys are already unique.
Unless there's a specific reason to constrain the size of a text field, use text. varchar and text only use the necessary amount of space for each row. "foo" will take the same amount of space in varchar(10) as in varchar(255). For example, there's no particular reason to limit the size of their linked in nor motivation.
for some reason i am getting there is no unique constraint matching given keys for referenced table "accident_location". I am using postgresql to create the table. what is the error here? as i have set the primary key of ID and address_ID on my accident_location table
CREATE TABLE IF NOT EXISTS Accident(
ID varchar(10) NOT NULL,
Severity INT,
Start_Time varchar(100) NOT NULL,
End_Time varchar(100) NOT NULL,
Description varchar(100),
PRIMARY KEY(ID)
);
CREATE TABLE IF NOT EXISTS Accident_Location(
ID varchar(10),
Address_ID INT,
Start_lat float,
Start_Lng float,
End_Lat float,
End_Lng float,
"Distance(mi)" float,
PRIMARY KEY (ID,Address_ID),
FOREIGN KEY (ID) REFERENCES Accident(ID)
);
CREATE TABLE IF NOT EXISTS Address(
Address_ID INT,
Number INT,
Street varchar(100),
Side varchar(5) ,
City varchar(50) NOT NULL,
County varchar(50) ,
State varchar(10) NOT NULL,
Zipcode varchar(15) NOT NULL,
Country varchar(5) ,
Timezone varchar(30) ,
Airport_code varchar(10),
Location_ID INT NOT NULL,
Weather_ID INT NOT NULL,
PRIMARY KEY (Address_ID),
FOREIGN KEY (Address_ID) REFERENCES Accident_Location(Address_ID)
);
The referenced Address_ID on the Address table should be unique on table Accident_Location.
Change Accident_Location table to
CREATE TABLE IF NOT EXISTS Accident_Location(
ID varchar(10),
Address_ID INT constraint unx_addres_id_location unique,
Start_lat float,
Start_Lng float,
End_Lat float,
End_Lng float,
"Distance(mi)" float,
PRIMARY KEY (ID,Address_ID),
FOREIGN KEY (ID) REFERENCES Accident(ID)
);
Here, I've added a unique constraint on Address_ID
Address_ID INT constraint unx_addres_id_location unique
UPDATED
Since you are referring to Address_ID of Accident_Location table as a foreign key on the Address table, this will force you to make it a unique record. That means any two rows or more should not have the same value of Address_ID. Otherwise, the foreign key Address_ID on the Address table wouldn't know which row you are referring to.
The reason you are not seeing this same error for the Accident table is that by default primary keys are unique.
Create a function to Get Ticket (for a Reservation).Ticket a booked reservation when the passenger pays the total amount.
Create a trigger, called line disruption, that adjusts all the tickets to the immediate next line when a line is closed due to an accident or maintenance, unless the customer specified no substitutions/adjustments in which case the ticket is canceled.
I created the tables, but I have some mistakes on my function: Please show me the mistakes and explain your solutions.
CREATE OR REPLACE FUNCTION get_ticket (reservation_id int, id int, price int)
RETURNS SETOF ticket
AS $$
BEGIN
INSERT INTO Ticket (ticket_id, reservation_id, price);
END;
$$ LANGUAGE plpgsql;
CREATE TABLE Customer (
id int PRIMARY KEY,
fname varchar,
lname varchar,
street varchar,
town varchar,
postal_code varchar
);
CREATE TABLE Station (
id int PRIMARY KEY,
name varchar,
open_time varchar,
close_time varchar,
stop_delay int,
street varchar,
town varchar,
postal_code varchar
);
CREATE TABLE Train (
id int PRIMARY KEY,
name varchar,
description varchar,
seat_avail int,
top_speed int,
cost_per_km int
);
CREATE TABLE Rail_Line (
line_id int PRIMARY KEY,
speed_limit int
);
CREATE TABLE Passes_Thru_SRL (
line_id int PRIMARY KEY,
station_id int PRIMARY KEY,
distance int,
CONSTRAINT FK1_SRL FOREIGN KEY (line_id) REFERENCES Rail_Line (line_id) ON DELETE CASCADE,
CONSTRAINT FK2_SRL FOREIGN KEY (station_id) REFERENCES Station (id) ON DELETE CASCADE
);
CREATE TABLE Route (
route_id int PRIMARY KEY
);
CREATE TABLE Passes_Thru_SR (
route_id int PRIMARY KEY,
station_id int PRIMARY KEY,
stops bool,
CONSTRAINT FK1_SR FOREIGN KEY (route_id) REFERENCES Route (route_id) ON DELETE CASCADE,
CONSTRAINT FK2_SR FOREIGN KEY (station_id) REFERENCES Station (id) ON DELETE CASCADE
);
CREATE TABLE Route_Schedule (
route_id int PRIMARY KEY,
day varchar,
time varchar,
train_id int PRIMARY KEY,
CONSTRAINT FK1_RS FOREIGN KEY (route_id) REFERENCES Route (route_id) ON DELETE CASCADE,
CONSTRAINT FK2_RS FOREIGN KEY (train_id) REFERENCES Train (id) ON DELETE CASCADE
);
CREATE TABLE Reservation (
reservation_id int PRIMARY KEY,
price int PRIMARY KEY,
id int,
CONSTRAINT FK_RS FOREIGN KEY (id) REFERENCES Customer (id) ON DELETE CASCADE
);
CREATE TABLE Ticket (
ticket_id int PRIMARY KEY,
reservation_id int,
id int, price int,
CONSTRAINT FK_RS1 FOREIGN KEY (reservation_id) REFERENCES Reservation (reservation_id) ON DELETE CASCADE,
CONSTRAINT FK_RS2 FOREIGN KEY (id) REFERENCES Customer (id) ON DELETE CASCADE,
CONSTRAINT FK_RS3 FOREIGN KEY (price) REFERENCES Reservation (price) ON DELETE CASCADE
);
This question already has answers here:
Adding a column as a foreign key gives ERROR column referenced in foreign key constraint does not exist
(5 answers)
Closed 1 year ago.
my code :
create table courses(
id SERIAL PRIMARY KEY
)
----------------------
create table teachers(
id SERIAL PRIMARY KEY
)
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id),
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100)
);
error:
column "course_id" referenced in foreign key constraint does not exist
SQL state: 42703
i tried running the third create table code after inserting data in the courses and teachers table's id yet no change.
You can not reference a column like that. First you have add the column you want to create foreign key then reference it.
create table courses(
id SERIAL PRIMARY KEY
);
----------------------
create table teachers(
id SERIAL PRIMARY KEY
);
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
course_id INT,
teacher_id INT,
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100),
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id)
);
I run my code and get the error in PostgreSQL. I have created multiple tables and the first seems to work while the others don't.
create table people (
id serial,
family_names text,
given_names text,
displayed_names text,
email_address text,
primary key (id)
);
create table users (
userid integer,
website text,
date_registered date,
gender GenderValue,
birthday date,
password text,
portrait integer,
primary key (userid),
foreign key (userid) references people(id),
foreign key (portrait) references photos(photoid)
);
create table photos (
photoid serial,
title TitleValue,
date_uploaded date,
date_taken date,
description text,
technical_details text,
safety_level safetyLevel,
visibility visibilityLevel,
owns integer,
primary key (photoid),
foreign key (owns) references users(userid)
);