Syntax Error in Query Field Definition - ms-access

I'm trying to create a table in ms-access at runtime when using VB6. I keep getting an error saying that there is a syntax error in the field definition, but I can't see one anywhere.
"CREATE TABLE PickedByPicco(OrderID INT, StockCode TEXT(30), Qty CURRENCY, ScannedBy TEXT, " & _
"ProcessedBy TEXT, processed BOOLEAN, Constraint compKey PRIMARY KEY(OrderID, StockCode))"
Where is the error in this query? Is it something to do with the primary key perhaps?

The name for a boolean yes/no field in Access is BIT, not BOOLEAN.
Use the following:
"CREATE TABLE PickedByPicco(OrderID INT, StockCode TEXT(30), Qty CURRENCY, ScannedBy TEXT, " & _
"ProcessedBy TEXT, processed BIT, Constraint compKey PRIMARY KEY(OrderID, StockCode))"

Related

Hi there, Im trying to create a table using SQL developer and it's providing me with an error message saying "invalid data type"

It doesn't allow me to successful create the table because it has an invalid datatype.
w3Schools
CREATE TABLE Day_Care_Resident (
PatientNO INT,
Arrival_Time time,
Collection_Time time,
Address VARCHAR2(50),
PhoneNumber number,
SocialWorker VARCHAR2(25),
WeekdaysAtHome VARCHAR2(10),
PRIMARY KEY (PatientNO),
FOREIGN KEY (PatientNO) REFERENCES PATIENT (PatientNO)
);
You mentionned SQL Developer in the title of your question, so I suspect that you are running Oracle, not mysql as tagged in your question.
Arrival_Time time, Collection_Time time
There is no time datatype in Oracle. You probably want to use date instead, so you can correctly represent these points in time.
On the other hand, if you are running mysql, then datatype issues are:
the varchar2 datatype, does not exist in mysql (this is an Oracle specific datatype). You can just replace that with varchar
the number datatype does not exist either; you can use numeric instead

Issue of creating table in MySql

I am trying to execute this query:
create table order_details(
Order_id int PRIMARY KEY AUTO_INCREMENT,
Book_Id int,
Cust_Name varchar(50),
Phone_No int,
Address varchar(100),
Order_Date DATE,
Quantity int,
FOREIGN KEY(Book_Id) REFERENCES book(Book_Id));
But the result is a MySQL Error 1005:
ERROR 1005 (HY000): Can't create table 'bookstore.order_details' (errno: 150)
create table order_details(
Order_id int PRIMARY KEY AUTO_INCREMENT,
Book_Id int,
Cust_Name varchar(50),
Phone_No int,
Address varchar(100),
Order_Date DATE,
Quantity int,
FOREIGN KEY(Book_Id) REFERENCES book(Book_Id));
Error (150) is being thrown because mysql is unable to reference the specified foreign key. This can be caused for a number of reasons however, I recommend taking the following steps to troubleshoot this.
A common issue that causes this error to become evasive is when you have not performed USE my_database_name; before executing the query. It fails because the context is either wrong or absent.
1.) Revise your query by adding in the name of the database in the reference
Example: FOREIGN KEY(`Book_Id`) REFERENCES `my_database_name`.`book`(`Book_Id`));
2.) Take a look at the book table and make sure `Book_Id` is the correct type (int) and is named exactly as you reference it. Perform the following queries and you may find your answer:
SELECT `b`.`Book_Id` FROM `book` AS `b`;
EXPLAIN `book`;
Selecting Book_Id from the referenced table will rule out typos in field naming. Explain will reveal the value type & key information that should help ensure consistency between foreign relations when investigating issues like this.
You might not be having a 'book' table in your database or if you have one then there might not be a column named 'Book_id'

Error as I try to create a primary key on a field with type Long

While trying to create a table using the following SQL :
CREATE TABLE person(id INTEGER,r_id LONG,p_name TEXT,p_age INTEGER,
p_address TEXT,p_mobile LONG,PRIMARY KEY(r_id));
I am getting an error :
Error Code: 1170
BLOB/TEXT column 'r_id' used in key specification without a key length
What error is it ?
You probably wanted bigint instead of LONG which is a long text data type.
CREATE TABLE person
(
id INTEGER,
r_id BIGINT,
p_name TEXT,
p_age INTEGER,
p_address TEXT,
p_mobile BIGINT,
PRIMARY KEY(r_id)
);
See MySQL data types

Creating a table with a couple as primary key in MySQL Server [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I have 2 tables:
Authors(id, name, add, DOB) where the fields are INTEGER, CHAR(20), CHAR(20), DATE respectively. Primary key = id.
Books(bid, title, publisher) where the fields are INTEGER, CHAR(20), CHAR(20) respectively. Primary key = bid.
I want to create another table Write with fields aid, bid, datePublished, where:
aid = INTEGER ... this is actually the id from Authors.
bid = INTEGER ... this is actually the bid from Books
Primary Key = (aid, bid).
How do I do this?
I tried the following lines of code, and all of them gave syntax errors:
CREATE TABLE Write (
aid INTEGER,
bid INTEGER, datePublished DATE,
PRIMARY KEY NONCLUSTERED (aid,bid),
FOREIGN KEY (aid) REFERENCES Authors(id),
FOREIGN KEY (bid) REFERENCES Books(bid)
);
CREATE TABLE Write (
aid INTEGER,
bid INTEGER,
datePublished DATE,
PRIMARY KEY (aid,bid)
);
Funny problem you're having. The reason is that Write is a reserved keyword in mysql and you can only use it as an identifier if you suround it by backticks in your queries. I recomend you picking another name or, if you really want to use Write, replace your create query with
CREATE TABLE `Write` (
aid INTEGER,
bid INTEGER,
datePublished DATE,
PRIMARY KEY (aid,bid)
);
Just remember that any other query on the table Write would require the backticks as well, which might lead to annoying errors if someone forgets them.
Write is a reserved word, no table can be named that (it can using back tics but to make your life easy stay away from names that requires back tics).
Here is the complete schema
http://sqlfiddle.com/#!2/dfe22/1
Try this :
CREATE TABLE Write (aid INTEGER, bid INTEGER, datePublished DATE PRIMARY KEY (aid,bid));

error creating table on my DB

I am trying to create a table that references another table in my db however I am receiving this error.
#1064 - 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 'UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20' at line 1
I have had a look at error #1064 on the mySQL refrence page but its abit of a vuage error just giving the outline of its structure.
Error: 1064 SQLSTATE: 42000 (ER_PARSE_ERROR)
Message: %s near '%s' at line %d
this is the code I have tried to execute
CREATE TABLE User (userID UInt32 PRIMARY KEY, name VARCHAR(20), handle VARCHAR(20), countryName VARCHAR(20) references Countries(SID))engine=innodb;
Countries table has already been created, so I am not entirely sure what is causing the problem
any help would be greatly appreciated.
added not this is how I have defined countries
CREATE TABLE Countries (countryName VARCHAR(20) PRIMARY KEY)engine=innodb;
UPDATED response:
Moving the constraint definition out of the table definition helped isolate the problem:
CREATE TABLE User(
userID UInt32 PRIMARY KEY
,name VARCHAR(20)
,handle VARCHAR(20)
,countryName VARCHAR(20)
)engine=innodb;
ALTER TABLE user ADD CONSTRAINT user_country_name_fk FOREIGN KEY(countryName) REFERENCES Countries(SID);
Table creation failure was caused by the use of the UInt32 data type. Changed to int, table creation succeeded.
Foreign key constraint was referring to the wrong column in Countries. It was referring to SID but needed to refer to countryName.