I am creating a table in pgweb Heroku, and get this error "ERROR: pq: syntax error at or near "("" - sql

Here is my exact query
CREATE Table Publisher
(Publisher_Id Int primary key not null,
Name varchar(20) not null,
Address varchar(50) not null,
Phone Int(10),
Isbn varchar(13) references books (Isbn) not null
)
Any help would be greatly appreciated.

datatype int does not take a length. So:
create table publisher (
publisher_id int primary key,
name varchar(20) not null,
address varchar(50) not null,
phone int,
isbn varchar(13) references books (isbn) not null
);
Notes:
not null is redondant on a primary key column
int does not seem like a good pick for a phone number; you would typically need to store leading 0s, or allow special characters such as + or () - int cannot do that. A string datatype would probably be a better pick

Related

Error in create table T-SQL "Incorrect syntax near ','."

When create table have error "Incorrect syntax near ','."
I cant see this error in code. Please point out this error.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc),
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR CONSTRAINT,
first_name VARCHAR CONSTRAINT,
middle_name VARCHAR,
phone_number INT(11),
address VARCHAR,
ticket_number INT CONSTRAINT,
date_registration DATETIME,
date_reregistratiom DATETIME,
issued_books_id FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
You cannot add multiple primary keys to the table, either composite ey (combination of two fields) or Unique constraint can be added instead.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
this is an error, you cannot ass multiple primary keys. Instead you can add Unique constraint
Correction would be,
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null Unique, --this is used to create the second table
quantity INT NOT null,
inventory_numbers INT NOT NULL Unique )
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc), --Referenced column needs to be unique
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR (255),
first_name VARCHAR (255),
middle_name VARCHAR(255),
phone_number INT,
[address] VARCHAR(255),
ticket_number INT ,
date_registration DATETIME,
date_reregistratiom DATETIME)
You cannot use a foreign key of another table as a foreign key for some other table. You need to read more on table, Primary key constraints and foreign key referencing.
This cannot be done. you are trying to refer to the foreign key of issued_books table as a foreign key in readers table, which is wrong.
issued_books_id INT FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
I hope this explanation will give you a better understanding of errors. There were synatx errors as well as wrong use of the SQL table creation steps. I have added the corrected code for you. Feel free to contact any time.

Why am I getting invalid identifier error?

I have already created two tables without issue. They are:
CREATE TABLE region
(
regionid CHAR NOT NULL,
regionname VARCHAR(25) NOT NULL,
PRIMARY KEY (regionid)
);
CREATE TABLE store
(
storeid VARCHAR(3) NOT NULL,
storezip CHAR(5) NOT NULL,
regionid CHAR NOT NULL,
PRIMARY KEY (storeid),
FOREIGN KEY (regionid) REFERENCES region(regionid)
);
However, when I try to enter the following table I get an error:
CREATE TABLE employee
(
employeeid VARCHAR(3) NOT NULL,
firstN CHAR(25) NOT NULL,
lastN CHAR(25) NOT NULL,
PRIMARY KEY (employeeid),
FOREIGN KEY (storeid) REFERENCES store(storeid)
);
The error I am getting is:
ORA-00904: "storeid": invalid identifier
Why am I getting this error? Thank you
You have:
FOREIGN KEY (storeid) REFERENCES store(storeid)
But you have not declared storeid. You need to declare it before you can specify that it is a foreign key.
Other notes:
Don't use char without a length. If it is one character, declare it as such.
I think id columns should be numeric rather than strings.
Don't use char() when you should be using varchar2(). char() pads the value out with spaces, and that is usually undesirable.

What is breaking my sql program? Incorrect syntax near (

This code section
CREATE TABLE AIRPORT (
Airport_Code int NOT NULL,
City varchar(20) NOT NULL,
State varchar(20) NOT NULL,
Name varchar(25) NOT NULL,
CONSTRAINT PK_AIRPORT PRIMARY KEY (Airport_Code)
)
Is almost the same as this one but this one is giving me an error and I can't figure out how to fix it
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL,
Max_seats int NOT NULL,
CONSTRAINT PK_AIRPLANE_TYPE (Typename) //error is here (Typename)
)
I am getting the error Incorrect syntax near '('.
Adding the primary key inline with the column will give you a crap auto-generated name for your primary key. I also don't understand how "encouraging primary keys to be on one column" is an added benefit.
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL,
Max_seats int NOT NULL,
CONSTRAINT PK_AIRPLANE_TYPE PRIMARY KEY (Typename)
)
You are missing the PRIMARY KEY. I have a preference for putting this directly in the column definition:
CREATE TABLE AIRPLANE_TYPE (
Company varchar(20) NOT NULL,
Typename varchar(20) NOT NULL PRIMARY KEY,
Max_seats int NOT NULL
);
In addition to a shorter definition, this encourages all primary keys to be only one column -- an added benefit.

Syntax Error- SQL Microsoft Access

I will appreciate nay help I am new to SQL and I am using Microsoft Access. I wrote this myself so if something looks wrong, please let me know!
Also if you could answer this question, I have to write and run a SQL query to list all occurrences of a certain course.How do I begin going about doing this? I cannot locate anything on it in my book.
CREATE TABLE CUSTOMER (
CustomerNumber Int PRIMARY KEY,
CustomerLastName Char (25) NOT NULL,
CustomerFirstName Char (25) NOT NULL,
Phone Char (12) NULL
);
CREATE TABLE COURSE (
CourseNumber Int PRIMARY KEY,
Course Char ( 15) NOT NULL,
CourseDate DateTime NOT NULL,
Fee Currency NOT NULL
);
CREATE TABLE ENROLLMENT (
CustomerNumber Int PRIMARY KEY,
CourseNumber Int NOT NULL,
AmountPaid Currency NOT NULL
);
I test your queries on MS Acces 2007 and MS Access 2010 and works fine,
You should run each query at a time, not all at once, when I try to execute the entire SQL I get the same error message.
SQL1:
CREATE TABLE CUSTOMER (
CustomerNumber Int PRIMARY KEY,
CustomerLastName Char (25) NOT NULL,
CustomerFirstName Char (25) NOT NULL,
Phone Char (12) NULL
);
SQL2:
CREATE TABLE COURSE (
CourseNumber Int PRIMARY KEY,
Course Char ( 15) NOT NULL,
CourseDate DateTime NOT NULL,
Fee Currency NOT NULL
);
SQL3:
CREATE TABLE ENROLLMENT (
CustomerNumber Int PRIMARY KEY,
CourseNumber Int NOT NULL,
AmountPaid Currency NOT NULL
);
Few comments that may help you, first check the valid data types in MS-Access in your current version, for example: MSDN-MS-Access DataTypes. You will see that Char and Int are not valid, also Numeric does not take arguments (you probably wanted to use Dobule). Also you are missing a comma (,) after the PRIMARY KEY in the first CREATE TABLE CUSTOMER statement. It may be a good practice using the GUI interface to create tables then move into DDL later.

Error 1064(42000) Creating a Table in msql5

CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int unsigned(7) NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);
I keep trying to run this simple create table blurb on a Mysql5 database and I keep getting the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '(7) NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
)' at line 3
I've googled the proper syntax from several different sites and can't make anything run! The maddening part is that when I use my teacher's code, which is very similar to this apart from some data types, it runs perfectly:
CREATE TABLE EMPL (
Eid int unsigned not null auto_increment primary key,
Name varchar(20),
title varchar(30),
salary int,
emailsuffix varchar(60)
);
It's because your syntax is indeed wrong: there's no int unsigned(7)type; try this:
CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int(7) unsigned NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);
or:
CREATE TABLE Message(
MessageID int unsigned not null auto_increment primary key,
naiveUserID int unsigned NOT NULL,
Title varchar(20),
bodyOfText TEXT(2000)
);