In certain SQL dialects, identifiers in square brackets have special semantics. What is the effect of enclosing an identifier in square brackets in SQLite?
As an example, SQLite (at least as of 3.40.1) accepts the following table definition.
CREATE TABLE [Banana] (
[Weight] REAL NOT NULL,
[Color] TEXT DEFAULT 'yellow' NOT NULL,
[Ripe] BOOLEAN NOT NULL DEFAULT 1
);
You can then insert and select bananas:
INSERT INTO [Banana] ([Weight]) VALUES (1.0), (2.0), (3.0);
SELECT * FROM [Banana];
The square brackets escape the database identifier, such as a table name. For example, if you wanted to name your table using a reserved SQLite keyword, such as TABLE, you could escape the name in square brackets:
CREATE TABLE [Table] (
[Weight] REAL NOT NULL,
[Color] TEXT DEFAULT 'yellow' NOT NULL,
[Ripe] BOOLEAN NOT NULL DEFAULT 1
);
In earlier versions of SQLite, you could have used double quotes to achieve the same thing:
CREATE TABLE "Table" (
Note that in general you should avoid such naming schemes, as you would forever have to escape the table name.
Related
I added a column to my table :
ALTER TABLE venues ADD decent BOOLEAN NOT NULL DEFAULT 0;
.schema venues returns :
CREATE TABLE [venues] (
[id] INTEGER PRIMARY KEY,
[foursquare_id] TEXT NOT NULL,
[pick] INTEGER NOT NULL DEFAULT 0,
[foursquare_data] TEXT NOT NULL,
[lat] FLOAT NOT NULL,
[lng] FLOAT NOT NULL,
[name] TEXT NOT NULL,
[closed] INTEGER NOT NULL DEFAULT 0,
[google_data] TEXT NOT NULL,
[google_place_id] TEXT NOT NULL,
[google_name] TEXT NOT NULL
, decent BOOLEAN NOT NULL DEFAULT 0);
Column I added does not have square brackets while all others do. What does this mean? Do I need to do this some other way?
The dot-command .schema venues returns the sql column of the Schema Table for the row that corresponds to the table venues.
The equivalent SQL statement would be:
SELECT sql FROM sqlite_schema WHERE name = 'venues';
As it is explained in Interpretation Of The Schema Table:
The text in the sqlite_schema.sql column is a copy of the original
CREATE statement text that created the object, except...
which means that if you used square brackets in all or some column names in the CREATE TABLE statement then this is what you get when you execute .schema venues.
If you add a new column, then its definition is added in the original CREATE TABLE statement inside the sql column as it is written in the ALTER TABLE statement.
If you want square brackets for all the column names then use them when you add the column:
ALTER TABLE venues ADD [decent] BOOLEAN NOT NULL DEFAULT 0;
See the demo.
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');
Size is a reserved word in Oracle, try changing the column name to an unreserved word.
See here for the full list of reserved words
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
);
What do I need to add to make sure this table is created with the 'name' field as non null but with a value of ""?
CREATE TABLE stuff (id serial primary key, name varchar(64) <-- what goes here??
Standard SQL applies:
CREATE TABLE stuff (
id serial primary key,
name varchar(64) not null default ''
);
It's possible you attempted to use double quotes to specify the text literal like this "", which will explode. Postgres uses single quotes to delimit text literals, like this ''.
See SQLFiddle.
I set the table
CREATE TABLE author (
id serial NOT NULL,
name character varying(255) NOT NULL,
orcid character varying(128) NOT NULL,
"position" integer NOT NULL,
CONSTRAINT author_pkey PRIMARY KEY (id )
);
Why does the "position" name contain ""?
How can I remove "" from position?
According to the manual, position is
non-reserved (cannot be function or type)
It's a reserved word in standard SQL. What you see is probably the output of pgAdmin or some other client that double-quoting all reserved words in SQL standard when used as identifiers.
This statement is syntactically correct:
SELECT position FROM author LIMIT 1;
You can always double-quote identifiers (thereby preserving mixed-case spelling). This works, too:
SELECT "position" FROM author LIMIT 1;
But double quotes are required here:
SELECT "where" FROM author LIMIT 1;
SELECT "CaMeL" FROM author LIMIT 1;
SELECT "a-b-c" FROM author LIMIT 1;
Maybe not literally but the query below gets an error near user. If i change it to userZ it works. WHY can i not use that name? Is there a way to specific its a field instead of a keyword? (or whatever it is)
create table Post2 (
id INTEGER PRIMARY KEY NOT NULL,
title nvarchar(max)
NOT NULL,
body nvarchar(max)
NOT NULL,
user integer REFERENCES Post1(id));
Reserved words, like user should be enclosed in brackets.
Take a look at Using Identifiers for a more in depth explanation.
CREATE TABLE Post2 (
id INTEGER PRIMARY KEY NOT NULL
, title NVARCHAR(MAX)NOT NULL
, body NVARCHAR(MAX) NOT NULL
, [User] INTEGER REFERENCES Post1(id)
);
USER is a reserved word.
Try delimiting it with "" (e.g. "user" or [user])
Read more on using delimited identifiers here.