Why am I getting invalid identifier error? - sql

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.

Related

ORA-00907 (missing right parenthesis)

I'm trying to run this in my database but for some reason, I keep getting missing the right parenthesis. Thoughts?
CREATE TABLE PET_OWNER
(
OwnerID Int NOT NULL IDENTITY(1, 1),
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);
EShirvana's answer directly answers the question you asked. However, I would suggest:
CREATE TABLE PET_OWNER (
OwnerID Int generated always as identity primary key,
OwnerLastName varchar2(25) NOT NULL,
OwnerFirstName varchar2(25) NOT NULL,
OwnerPhone varchar2(12),
OwnerEmail varchar2(100)
);
The differences:
The primary key constraint can be inlined. To be honest, I don't generally see much use for naming the primary key as a separate constraint (no harm).
Declaring a primary key column as NOT NULL is redundant. That is part of being a primary key.
Do you know what the char() data type does? It pads strings with spaces so they match the length. Use variable length strings -- and Oracle recommends varchar2() for this purpose.
By default, columns are NULLable. I actually find that explicitly declaring NULL is harder to read because I have to distinguish between NOT NULL and NULL which requires more work than just seeing NOT NULL.
2 issues:
identity and missing comma before constraint:
CREATE TABLE PET_OWNER(
OwnerID Int GENERATED ALWAYS AS IDENTITY NOT NULL,
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL,
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);

beginner sql missing keyword and invalid identifier

CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
book_id char (7) primary key not null ,
constraint writer__id_fk foreign key (writer_id),
constraint publisher__id_fk foreign key (publisher_id)
);
I'm getting
[ORA-00905: missing keyword]
in publisher table
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null,
);
I'm getting
[ORA-00904: : invalid identifier]
The following SQL creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Refer this link for more details
https://www.w3schools.com/sql/sql_foreignkey.asp
Hope this helps.
Welcome to the wonderful world of SQL! :-)
General remark:
Please tell us what kind of DBMS you're using. MySQL? SQL Server? Oracle? SQlite? Different systems use different kinds of syntaxes.
First statement:
The problem seems to be in the FOREIGN KEY-portion.
Usually, you'll state something like:
CONSTRAINT [constraint_name] FOREIGN KEY([column_in_this_table]) REFERENCES OTHER_TABLE([column_in_other_table])
edit (added):
The [column_in_this_table] has to exist in your DDL (CREATE TABLE-statement), like so:
CREATE TABLE Book ( book_title ... etc., publisher_id INT, CONSTRAINT FK_publ_id FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id));
Here, you'll have a 'original' column called 'publisher_id', in the 'publisher'-table. You refer to it from within the 'Book'-table, by first having a 'publisher_id' column in the 'Book'-table (which should have the same DDL as the original column by the way). Next, you'll add a FOREIGN KEY to the 'Book'-table, that is imposed on the Book(publisher_id) column. Note, that you could also name the column in your 'Book'-table differently -- like, say, 'Spongebob' or 'Patrick'. But for future use, you'd like naming conventions that tell what you might expect to find in a column. So you'd name columns for what they contain.
Second statement:
The problem is with the last portion of your statement, where there's a comma after the NOT NULL portion for column publisher_address.
(Part of) your statement:
publisher_address varchar2(60) not null, );
Try replacing that with:
publisher_address VARCHAR2(60) NOT NULL);
edit (note to self):
VARCHAR2 turns out to be a valid datatype in Oracle databases (see:
Oracle documentation)
For your first table, the Foreign Keys do not reference any table. For your second table, I would imagine that comma after your last column isn't helping anything.
So this is the answer.
CREATE table Book
(
book_title varchar (100) not null ,
book_genre char(60) not null,
Date_of_publish date not null,
user_code char(7) not null ,
publisher_id char (7) not null,
writer_id char(7) not null,
book_id char (7) primary key not null ,
CONSTRAINT book_writer_id_fk FOREIGN KEY(writer_id) REFERENCES writer(writer_id),
CONSTRAINT book_publisher_id_fk FOREIGN KEY(publisher_id) REFERENCES publisher(publisher_id)
);
CREATE table publisher
(
publisher_id char (7) primary key not null,
publisher_name char(20) not null,
publisher_number char(10) not null,
publisher_email varchar2(60) not null,
publisher_address varchar2(60) not null
);

create table syntax error in Microsoft Access?

Here is my code, i'm trying to make multiple tables:
Create Table Order_t
(
Id AutoIncrement Not Null,
OrderDate DateTime Not Null,
CustId Int Not Null,
Primary Key(Id),
Foreign Key(CustId) References Customer_t(Id)
(;
Create Table PersonRole_t
(
PersonRoleID Autoincrement Not Null,
Person_ID int Not Null,
Primary Key(PersonRoleID, Person_ID),
Foreign Key(Person_ID) References Person_T(Person_ID)
(;
Create Table Product_t
(
Id Text(10) Not Null,
Name Text(30) Not Null,
Description Text(30),
Finish Text(30),
UnitPrice Currency Not Null,
Primary Key(Id)
) ;
Whenever I run it in Microsoft Access, I get an error in the CREATE TABLE statement (it highlights the PersonRole_T table definition). Not sure what to do, rather new to SQL.
Use CLOSE parenthesis at the end of CREATE Table statement instead of OPEN parenthesis
Create Table Order_t
(
Id AutoIncrement Not Null,
OrderDate DateTime Not Null,
CustId Int Not Null,
Primary Key(Id),
Foreign Key(CustId) References Customer_t(Id)
); -- Not (;
Create Table PersonRole_t
(
PersonRoleID Autoincrement Not Null,
Person_ID int Not Null,
Primary Key(PersonRoleID, Person_ID),
Foreign Key(Person_ID) References Person_T(Person_ID)
); -- Not (;
The table Person_T with a key of Person_ID needs to exist before the References Person_T(Person_ID) statement can execute. Based on your naming convention, I would guess the statement should rather be References Person_T(Id).
Consider changing your naming convention so that a data element does not change its name depending its location realtive to tables/views. Also consider whether the _t suffix is worth the bother.
#Prdp's point about the close parens is valid too.

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.

Error creating table in oracle

I am trying to create a table in oracle but I am getting this error: unknown command ")" - rest of line ignored. I can't figure out what is causing this error. Below is my SQL for the table:
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Your code is correct. Make sure you are referencing primary keys (all 4).
Check this: http://sqlfiddle.com/#!2/7be70/1/0
If you do not follow that, you may get this error: There are no primary or candidate keys in the referenced table 'BANKACC' that match the referencing column list in the foreign key 'fk_BANKACC_PAYMENT'.
Code in the fiddle above:
CREATE TABLE BANKACC
(BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PRIMARY KEY(BankName, AccNum));
CREATE TABLE CRCARD
(CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
PRIMARY KEY(CType, CCNum));
CREATE TABLE PAYMENT
(PayNum INT NOT NULL PRIMARY KEY,
CType VARCHAR(1) NOT NULL,
CCNum VARCHAR(16) NOT NULL,
BankName VARCHAR(75) NOT NULL,
AccNum INT NOT NULL,
PDate DATE NOT NULL,
Amt DECIMAL(11,2) NOT NULL,
CONSTRAINT fk_BANKACC_PAYMENT FOREIGN KEY (BankName, AccNum)
REFERENCES BANKACC(BankName, AccNum),
CONSTRAINT fk_CRCARD_PAYMENT FOREIGN KEY (CType, CCNum)
REFERENCES CRCARD(CType, CCNum)
);
Also you should read this for better understanding on how to implement foreign key constraint: http://docs.oracle.com/cd/E17952_01/refman-5.5-en/create-table-foreign-keys.html
If you're running this in SQL*Plus, get rid of the blank line between REFERENCES and ):
REFERENCES CRCARD(CType, CCNum)
);
Or set sqlblanklines on to change the behaviour.
By default it interprets a blank line as the end of the statement, but doesn't run it. So your entire CREATE TABLE command is essentially ignored, and the ); is treated as a stanalone command. Hence the error message you get, since it doesn't mean anything on its own.
Please use NUMBER for numeric columns.
http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT313
Also, in Oracle, use VARCHAR2 for strings.
but of it your syntax should be correct.