Search code examples
sqloracle-databasesyntax-errorddlcreate-table

Invalid identifier error but i can't see why


Started learning SQL and am having a go at creating a script. The code looks perfectly fine to me but I keep getting the invalid identifier error. I've checked the code over and over again but everything seems ok. I'm going mad here. I am using oracle by the way.

create table Products ( ID int not null, Item varchar(30) not null, Size 
varchar(1) not null);
insert into Products values ( 321, 'T-shirt', 'M');
insert into Products values ( 211, 'Jeans', 'L');

Solution

  • size is a reserved word in Oracle's SQL (not sure if it is according to the ANSI standard, but some databases, like MySQL, definitely allow it).

    You could escape it by using double quotes ("):

    CREATE TABLE Products (
        ID INT NOT NULL,
        Item VARCHAR(30) NOT NULL,
        "Size" VARCHAR(1) NOT NULL
    );
    

    But it would be much easier to just choose a name that isn't a reserved word:

    CREATE TABLE Products (
        ID INT NOT NULL,
        Item VARCHAR(30) NOT NULL,
        ProductSize VARCHAR(1) NOT NULL
    );