Why cant i use the field user in SQL Server 8? - sql

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.

Related

Using like for numeric in a constraint

I am creating a table which is like this:
CREATE TABLE Peeps
(
Name VARCHAR(255) NOT NULL,
PhoneNum BIGINT NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
)
Every phone number has to start with 08. However when I tried insert there's an error because LIKE can't be used for numeric (or that's what my friend said). The alternative would be using VARCHAR for PhoneNum, but this is an assignment and we have to use numeric for the phone number.
If a phone number can start with a 0 then you need to use a string:
CREATE TABLE Peeps (
Name VARCHAR(255) NOT NULL,
PhoneNum VARCHAR(255) NOT NULL,
CONSTRAINT PhoneNum_Check CHECK (PhoneNum LIKE '08%')
);
Although you can use LIKE on a number, it is highly not recommended. What happens is that the number is converted to a string. However, that string will never start with a 0 -- well, at least never when the value is greater than 1.

Feature from workbench maybe?

I'm trying to create a table in workbench, and I don't know why theses names can be set up. It ask me to change it. Maybe it concern features that I would like to disable.
create table net_shows(
title VARCHAR(100)
,rating VARCHAR(100)
,ratingLevel VARCHAR(100)
,ratingDescription INT(10)
,release year INT(4)
,user rating score FLOAT(4)
,user rating size FLOAT(4)
);
release , user appear in blue like a special command.
A column name is an identifier, and those can't include unless it's a delimited identifier (sometimes called quoted identifier.) Also reserved words need to be delimited. (en.wikipedia.org/wiki/SQL_reserved_words.) In MySQL you use back-ticks to delimit an identifier.
https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

How do I declare a table with a string column that has a non null default value in Postgres?

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.

Why is one identifier in a CREATE TABLE double-quoted, but not the others?

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;

sqlite3 Error Executing SQL From File

I am trying to create tables in an SQLite database with sqlite3.
The command $ sqlite3 mydb < mytables.sql produce the following error: Incomplete SQL: ??C.
mytables.sql is:
CREATE TABLE SizeCulture (
SizeCultureID INTEGER PRIMARY KEY ASC,
SizeID INTEGER NULL,
CultureID TEXT NULL,
Name TEXT NULL,
Description TEXT NULL,
Abbreviation TEXT NULL,
);
CREATE TABLE Size(
SizeID INTEGER PRIMARY KEY ASC ,
Creation TEXT NOT NULL,
Modification TEXT NOT NULL,
Deleted INTEGER NOT NULL,
);
/****** Object: Table [Ordering].[BarCode] Script Date: 11/09/2011 14:58:19 ******/
CREATE TABLE BarCode(
BarCodeID INTEGER PRIMARY KEY ASC NOT NULL,
BarCodeValue TEXT NOT NULL,
);
This was modified from a script generated by SQL Server, where some tables need to be replicated on an Android device.
The above is just a set of repeating create table statements. From what I understand, SQLite follows standard SQL (like MySQL or postgres).
Though I can't test it at the moment, I think it's the trailing commas that are confusing it (for example, the comma at the end of Abbreviation TEXT NULL,). Try removing all those trailing commas.
Edit: To be clear, I'm talking about all of these commas:
Abbreviation TEXT NULL,
...
Deleted INTEGER NOT NULL,
...
BarCodeValue TEXT NOT NULL,
I had the same problem, but for a different reason (so I'm commenting because Google led me here). Turns out you can also encounter this error if your file has a weird encoding (like UCS-2 instead of UTF8).