I'm currently learning some basic stuff about sql in school. We received an exercise in which consists in creating a script that creates tables. We have a schema and we need to recreate it.
I'm having some issues with this one:
When I run the script, it shows this error:
CREATE TABLE "EMPLOYEE" ( "BIRTH_DATE" DATE , "FIRST_NAME" VARCHAR(14) , "LAST_NAME" VARCHAR(16) , "GENDER" ENUM('M','F') , "HIRE_DATE" DATE ) IN "TS_EMPLOYEE"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "(" was found following "16) , "GENDER" ENUM".
Expected tokens may include: "DEFAULT". SQLSTATE=42601
I looked for the error on the internet and thought I should specify the DEFAULT. For this reason I modified the script adding this part:
"GENDER" ENUM('M','F')DEFAULT 'M' ,
Unfortunately it didn't help me much, since it indicates me the same mistake as before.
Does anyone know where I am wrong? Or what I could change?
Any kind of help is appreciated! ^^
I don't think DB2 supports enums. If you are using DB2, then use a check constraint:
CREATE TABLE EMPLOYEE (
BIRTH_DATE DATE,
FIRST_NAME VARCHAR(14),
LAST_NAME VARCHAR(16),
GENDER CHAR(1),
HIRE_DATE DATE,
CHECK (GENDER IN ('M', 'F'))
);
Notes:
I removed the double quotes. Just don't use them for identifiers. They only clutter queries and introduce the possibilities for strange errors.
I think such a table should have a primary key, although I have not added one.
The lengths of the strings for the names seems unnecessarily short.
Related
liquibase version being used - org.liquibase:liquibase-core:3.8.2. (not pro version)
Liquibase documents (1 & 2) says below about splitStatements (defaults to true)
Set to false to not have Liquibase split statements on ;'s and GO's.
Defaults to true if not set
and
Removes Liquibase split statements on ;'s and GO's when it is set to
false. Default value is: true.
Another useful sof post i found - In Liquibase is it OK to have an empty line on splitstatements?
I understand - when splitStatements is true, liquibase splits the statements on ; and GO
It not entirely clear what benefit splitStatements adds - i.e if SQL statement are split on ; (end delimiter) or not, what difference will it make - i.e if the statements are executed in a single query or multiple queries - won't the db handle the ";" based stuff anyway. This seems to be essential to understand. --could some one quote an example.
My current project has splitStatements:false. what advantages are we getting by disabling splitStatements. -any example would be greatly appreciated.
------------------------question expanded after answer from #user13579
below is an extract from a liquibase changelog file. This is what brought me to this question. It has splitStatements:false and a ;s in script and it works. With splitStatements:false i would expect a error in this case and the answer I suppose also suggests an error in this case. The below is from production code so I am NOT sure how it works and the backend is POSTGREs. Can someone explain.
--liquibase formatted sql
--changeset adam:001-users-001 failOnError:true splitStatements:false logicalFilePath:001-users.sql
CREATE TABLE sys_users
(
user_id SERIAL,
first_name character varying(64) NOT NULL,
last_name character varying(64) NOT NULL,
email character varying(255) NOT NULL
)
WITH (
OIDS=FALSE
);
CREATE TABLE user_role
(
role_id SERIAL,
role_name character varying(255) NOT NULL,
description character varying(255) NOT NULL,
created_on timestamp(6) with time zone NOT NULL,
created_by character varying(64) NOT NULL,
)
WITH (
OIDS=FALSE
);
I'm doing a group project to learn SQL, I'm using jdoodle as an online IDE for now and w3schools. I feel so weird for asking this because this is literally my first attempt but I get an error.
CREATE DATABASE turing;
CREATE TABLE Suppliers (
SupplierNumber int,
SupplierName varchar(255),
SupplierAddress varchar(255),
);
Error: near line 1: in prepare, near "DATABASE": syntax error (1)
Error: near line 2: in prepare, near ")": syntax error (1)
I'm just like copying exactly what w3schools taught me?
I don't see any errors in here, But these are some best practises,
RUN the SQL queries one by one in case if that's causing the error. First create the Database and then Create the Table. Do both seperately.
CREATE TABLE Suppliers (
supplierNumber int PRIMARY KEY NOT NULL,
supplierName varchar(255),
supplierAddress varchar(255)
);
Best practise is to have the column names in lowerCamelCase.
Normally we don't use comma for the last line. It is Unnecessary
Having a Primary key for every table is good. Make it PRIMARY KEY and NOT NULL at the same time to prevent some error in the future.
I'm currently learning SQL and I've installed oracle 11g express on my system. I'm trying to create a table however when I try to run the below command I get the following Error Message:
ERROR at line 3:
ORA-00904 : invalid identifier
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
Desc CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
Can anybody please tell me what I'm doing wrong?
Thanks for all the replies, I ran this command succesfully:
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
description CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
Really Appreciate the fast replies!
Chris
You have DESC in as a column name. While you can use it you will have to encompass it in quotes:
CREATE TABLE PROJECTS (
proID NUMBER(4) NOT NULL,
"Desc" CHAR(20),
sDate DATE,
eDate DATE,
Budget NUMBER(7,2),
maxStaff NUMBER(2)
);
You will also have to use quotes every time you call it in a query. I recommend just changing that column to something else (maybe DESCRIPTION?)
Since DESC is a reserved word, you would have to enclose it in double quotes.
However, I would not recommend using reserved words for fields names, perhaps change to description or something similar
As already said several times, the error is caused here by the use of a reserved keyword unquoted as an identifier. For sake of completeness:
Oracle has an impressive list of reserved keywords.
Unquoted identifiers are internally converted upper-case by Oracle.
Quoted identifiers are case-sensitive
So:
CREATE TABLE T (DESC INT);
ORA-00904: : invalid identifier as DESC is a keyword
CREATE TABLE T (Desc INT);
ORA-00904: : invalid identifier same reason as unquoted identifiers are converted all upper-case
CREATE TABLE T ("DESC" INT);
Table created by using quotes, "DESC" is no longer recognized as a reserved keyword
INSERT INTO T("Desc") VALUES (1);
ORA-00904: "Desc": invalid identifier Quoted identifiers are case-sensitive. "DESC" is not the same columns as "Desc"
INSERT INTO T("DESC") VALUES (1);
1 row(s) inserted
That being said, you should avoid using a keyword as an identifier...
I've checked other similar questions. about repeating commas, error in commands but cant find any in my error. I've also searched examples of create tables to compare with mine, but fail to find any difference :(.
Below is the CREATE table statement:
CREATE TABLE DRIVER(L# VARCHAR(15) NOT NULL
, DNAME VARCHAR(75) NOT NULL
, STATUS VARCHAR(50) NOT NULL
, NRIC VARCHAR (15) NOT NULL
, PRIMARY KEY(L#)
, CANDIDATE KEY(NRIC)
);
Anyone can help me point out that i can't see or missing something,thanks (:
You can't specify CANDIDATE KEY like that in Oracle. The right way is to create a UNIQUE CONSTRAINT on your Candidate Key(s).
Like so.
Here's a working SQLFiddle: http://sqlfiddle.com/#!4/b392d/1
CREATE TABLE DRIVER(
L# VARCHAR(15) NOT NULL,
DNAME VARCHAR(75) NOT NULL,
STATUS VARCHAR(50) NOT NULL,
NRIC VARCHAR (15) NOT NULL,
PRIMARY KEY(L#),
CONSTRAINT UK_NRIC UNIQUE (NRIC)
);
ORA-00907 pretty much always indicates a syntax error. It happens when the parser finds a word which is not a key word in a position where it is expecting either a keyword or a right-hand bracket to close the current statement (hence Missing Right Parenthesis). Although sometime it can just be that we have an unmatched left-hand bracket: a decent text editor can help trace that.
In your case the intruding word is CANDIDATE. Syntax errors can be tricky for beginners. I recommend you familarise yourself with the Oracle documentation:it is online, complete and free, and has quite a lot of examples. Check it out . The list of reserved words could have helped you, because CANDIDATE is not on it, which is a big clue.
If you are going to use odd characters in a name, wrap it in square brackets. []
I am trying to create the following table.
CREATE TABLE customer
(
cust# CHAR(3)NOT NULL ,
cname VARCHAR(30) NOT NULL ,
city VARCHAR(20) NOT NULL,
PRIMARY KEY (cust#)
)engine=InnoDB;
I am getting the following 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 'cname VARCHAR(30) NOT NULL , city VARCHAR(20) NOT NULL, PRIMARY KEY (cust#' at line 4
I have confirmed that the problem is with the '#' by replacing it with 'custNum'.
However, I must use the '#' sign. I know I can rename the field in the myPhpAdmin interface to cust# but I need to know how to escape it in the SQL statement.
*Edit to say I have already tried '\#'
Thanks
I was able to answer my own question and I'm posting here in case someone else has the same problem.
You have to use '`' around the name. So `cust#` did the trick.