How to debug the error in this SQL command? - sql

I am trying to execute this SQL command to create a table.
CRETE TABLE NEWS (
ID INT(128) NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(128),NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL,
PRIMARY (ID), );
)

You need:
Create table news (
ID INT(128) NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(128) NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL,
PRIMARY KEY (ID) );
Missing KEY at the end, bogus comma on the line creating TITLE, another bogus comma on the PRIMARY line. First problem was the full stop in the table name.

Create table news
(
ID INT identity (1,1) primary key ,
TITLE VARCHAR(128) NOT NULL,
SLUG VARCHAR(128) NOT NULL,
TEXTS_S TEXT NOT NULL
)

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

Create table does not work

I have a problem with creating a table in Maria DB Server.
I could successfully make the table on my other device, but for MYSQL (with probably different code).
Query:
CREATE TABLE admin (
'id' double(9999) NOT NULL auto_increment,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null,
PRIMARY KEY(id)
);
Query Error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "id' double(9999) NOT NULL auto_increment 'username' VARCHAR(50) NOT NULL, 'pa' at line 2
If any other information is required ask in comments.
Thanks
You were putting single quotes around the table names, which MariaDB was interpreting as string literals. Instead, just use the unquoted names directly:
CREATE TABLE admin (
id double(9999) NOT NULL auto_increment,
username VARCHAR(50) NOT NULL,
passcode VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
state VARCHAR(50) NOT NULL,
points double(9999) not null,
PRIMARY KEY(id)
);
If you do need to escape a column name in MariaDB/MySQL, e.g. because the name has whitespace or is a keyword, you can do so using backticks:
CREATE TABLE admin (
`id` double(9999) NOT NULL auto_increment,
`username` VARCHAR(50) NOT NULL,
...
)
But note that putting whitespace into column or table names, or using reserved keywords, is considered bad practice.
You could also put the PRIMARY KEY right after AUTO_INCREMENT
CREATE TABLE admin (
'id' double(9999) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null
);

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.

Problems getting foreign keys working in MySQL

I've been trying to get a delete to cascade and it just doesn't seem to work. I'm sure I am missing something obvious, can anyone help me find it?
I would expect a delete on the 'articles' table to trigger a delete on the corresponding rows in the 'article_section_lt' table.
CREATE TABLE articles (
id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
url_stub VARCHAR(255) NOT NULL UNIQUE,
h1 VARCHAR(60) NOT NULL UNIQUE,
title VARCHAR(60) NOT NULL,
description VARCHAR(150) NOT NULL,
summary VARCHAR(150) NOT NULL DEFAULT "",
html_content TEXT,
date DATE NOT NULL,
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)ENGINE=INNODB;
CREATE TABLE article_sections (
/* blog, news etc */
id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
url_stub VARCHAR(255) NOT NULL UNIQUE,
h1 VARCHAR(60) NOT NULL,
title VARCHAR(60) NOT NULL,
description VARCHAR(150) NOT NULL,
summary VARCHAR(150) NOT NULL DEFAULT "",
html_content TEXT NOT NULL DEFAULT ""
)ENGINE=INNODB;
CREATE TABLE article_section_lt (
fk_article_id INTEGER UNSIGNED NOT NULL REFERENCES articles(id) ON DELETE CASCADE,
fk_article_section_id INTEGER UNSIGNED NOT NULL
)ENGINE=INNODB;
You need to explicitly declare the foreign key constraint. The REFERENCES clause on a column definition doesn't work.
Try this:
CREATE TABLE article_section_lt (
fk_article_id INTEGER UNSIGNED NOT NULL,
fk_article_section_id INTEGER UNSIGNED NOT NULL,
KEY fk_article_id (fk_article_id),
CONSTRAINT fk_article_section_lt_to_article FOREIGN KEY (fk_article_id) REFERENCES articles(id) ON DELETE CASCADE
)ENGINE=INNODB;