What is breaking my sql program? Incorrect syntax near ( - sql

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.

Related

Error during foreign key creation: Invalid references

I have 2 tables and I want to create a foreign key constraint in the second table. This is what I tried:
Table 1:
CREATE TABLE REMINDER_RULE_M
(
REMINDER_RULE_M_D int IDENTITY(1,1) NOT NULL,
COMMUNICATION_MODE nvarchar(255) NOT NULL,
REMINDER_TO nvarchar(255) NOT NULL,
REMINDER_VALUE varchar(255) NOT NULL,
REMINDER_CONDITION varchar(255) NOT NULL,
REMINDER_TO_CUSTOM varchar(255)
)
Table 2:
CREATE TABLE REMINDER_AUDIT
(
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
CONSTRAINT FK_b892318b20e5bbe162722ea5946
FOREIGN KEY (REMINDER_RULE_M_D)
REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)
I get an error running the second SQL query:
Reason:
SQL Error [1769] [S0001]: Foreign key 'FK_b892318b20e5bbe162722ea5946' references invalid column 'REMINDER_RULE_M_D' in referencing table 'REMINDER_AUDIT'.
As the error clearly tells you - you don't have a column in your second table.
You must have a column in order to create a FK constraint - the FK constraint does NOT create a column in your table - it just establishes a constraint between existing tables and columns.
So try this for your second table:
CREATE TABLE REMINDER_AUDIT
(
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
-- define the column!
REMINDER_RULE_M_D int NOT NULL,
-- I'd strongly recommend trying to come up with a more
-- intuitive and useful naming convention for your FK constraints!
CONSTRAINT FK_b892318b20e5bbe162722ea5946
FOREIGN KEY (REMINDER_RULE_M_D)
REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)
I just guessed that REMINDER_RULE_M_D is NOT NULL - you might need to adapt this (if it's an optional key).
You do not need to write Foreign Key
CREATE TABLE REMINDER_AUDIT (
REMINDER_AUDIT_D int IDENTITY(1,1) NOT NULL,
ACTION varchar(255) NOT NULL,
CONSTRAINT FK_b892318b20e5bbe162722ea5946 REFERENCES REMINDER_RULE_M(REMINDER_RULE_M_D),
OLD_VALUE nvarchar(1024) NOT NULL,
NEW_VALUE nvarchar(1024) NOT NULL,
)

"Syntax error in constraint clause" MS Access creating a table

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

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.

Creation of new table fails with error message "Foreign key constraint is incorrectly formed"

I have 2 SQL statement as follows:
CREATE TABLE IF NOT EXISTS countries
(
country_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
country varchar(45) NOT NULL
);
CREATE TABLE IF NOT EXISTS patients
(
p_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
p_fname varchar(50) NOT NULL,
p_mname varchar(10) NULL,
p_lname varchar(50) NOT NULL,
age INT NOT NULL,
sex SET ('Male','Female'),
phone_num_mobile varchar(10) NULL,
phone_num_res varchar(7) NULL,
phone_num_office varchar(7) NULL,
email varchar(75) NULL,
addr_house varchar(10) NULL,
addr_street1 varchar(45) NOT NULL,
addr_street2 varchar(45) NULL,
addr_street3 varchar(45) NULL,
addr_city varchar(20) NOT NULL,
addr_country varchar(45) NOT NULL,
occupation varchar(20) NOT NULL,
married BOOLEAN NOT NULL,
FOREIGN KEY(addr_country) REFERENCES countries(country)
);
The first one executes successfully and the second one, which assigns a foreign key to the previous table, fails to execute with the message 'Foreign key constraint incorrectly formed'. I have also tried altering the second query's foreign key field(addr_country) to have the same name as that of the 'countries' table(country) but it were top no avail.
Can anyone please explain me what's going on and suggest me with a solution to this problem?
Thanks in advance.
A foreign key in MariaDB can only reference either a primary key in another table or a unique key. In this case, the countries table already has a primary key, so you can create a unique key on the country column.
Try putting a unique constraint on the country column in the countries table:
CREATE TABLE IF NOT EXISTS countries
(
country_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
country varchar(45) NOT NULL
UNIQUE KEY(country)
);
Late edit: As the commentors mentioned, you might want to just reference the country_id primary key instead of the country name. Since no two countries in the world have the same name, either might work in practice.

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.