why won't my SQL run in vscode? What can I do to fix this? - visual-studio-code

I am following this tutorial for SQL: https://www.youtube.com/watch?v=Cz3WcZLRaWc
I followed all the instructions, but when I click run I get a message saying Code language not supported or defined. How do I fix this?
--#block
CREATE TABLE User(
id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
bio TEXT,
country VARCHAR(2),
);

Related

Creating my first table- Syntax error at or near ();

First day of postgreSQL- sorry if this is too basic, but couldn't find the answer on here.
So basically I'm following a video tutorial, and have written exactly the same lines as tutorial, yet I get above mentioned syntax error when trying to clear:
CREATE TABLE Actors(
actor_id SERIAL PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30)NOT NULL,
gender CHAR(1),
date_of_birth DATE,
);
I know there is probably a super simple solution but I cant seem to find out what after nearly 2 hours- help
No need to apologize!
For clean queries, always recommended to put a [SPACE] after datatypes (for example: VARCHAR(30)[SPACE]NOT NULL ) and always be sure to remove your COMMAS right before your closing parenthesis.
CREATE TABLE Actors(
actor_id SERIAL PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30) NOT NULL,
gender CHAR(1),
date_of_birth DATE
)
Remove the , after date_of_birth and perhaps add a space before NOT NULL
CREATE TABLE Actors (
actor_id SERIAL PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30) NOT NULL,
gender CHAR(1),
date_of_birth DATE
);

Problems running Postgresql query

Probably a very easy question for most of you, but I'm having some problems running this code:
CREATE TABLE Users(
userId INT SERIAL PRIMARY KEY,
userName VARCHAR(50),
userPassword VARCHAR(50),
);
I usually use MySQL but encountered PostgreSQL in a school work. Why is this code not working for me? The database just gives me a syntax error, nothing else is provided for me. The only thing I changed compared to the MySQL code is auto_increment to SERIAL.
Thanks in advice!
If you want auto generated sequential IDs , use bigserial, and the query looks something like this :-
CREATE TABLE Users(
userId bigserial,
userName VARCHAR(50),
userPassword VARCHAR(50),
Primary Key(userId)
);

Why is the format of the sql files affecting if they can run or not in PG?

I have placed a file in my docker-entrypoint-initdb.d/ directory. Here is what is in the file:
CREATE TABLE user_test (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
The error I am getting is psql:/docker-entrypoint-initdb.d/0001-initial-database-design.sql:8: ERROR: syntax error at or near "CREATE".
What am I missing in being able to run a file? How do I change this file to work?
USER is a reserved keyword in Postgres, see the documentation. In general, you should avoid naming your tables and columns using reserved SQL keywords. If you really wanted to proceed as is, then place user into double quotes:
CREATE TABLE "user" (
user_id INTEGER,
name VARCHAR(100),
email VARCHAR(128),
active_flg BOOLEAN,
type VARCHAR(20),
CONSTRAINT pk_user PRIMARY KEY (user_id)
);
But, keep in mind that if you choose to name your table user, then you will forever have to escape it with double quotes.

I can't view the primary key on my table

I'm fairly new to SQL Server (I'm learning with SQL Server 2008), I have this question:
I have a database "two trees test" with 2 tables in it (Employee & Product), this is the design of the Employee table:
EmployeeID INT Not Null (Primary key)
FirstName nvarchar(50) Not Null
LastName nvarchar(50) Not Null
Title nvarchar(50) Not Null
HireDate date Null
BirthDate date Null
Phone nvarchar(20) Null
Status bit Not Null
You can see I have a primary key (EmployeeID), but if I go to the edit view (so I can start to insert data), I can't see the primary key.
Thanks in advance for your help & apologies if I misspelled something (English isn't my first language), I tried to attach screenshots but I can't because I don't have 10 points yet.
Maybe the primary key is Auto-Increment which might not allow you to enter a value for it.
See this to give custom values to the primary column.
Expand your table go to KEYS Option to see the List of Key's
select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='Your Table Name'

What's wrong with my CREATE TABLE command?

I'm following the instructions from this document. My exact version is 8.4.4.
This is what I try to do
CREATE TABLE testInfo (
testNo integer PRIMARY KEY,
product varchar(15),
firmware varchar(15),
startDate date,
eta date
);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "testinfo_pkey" for table "testinfo"
It totally ignores my PRIMARY KEY constraint. I don't see whay this isn't essentially the same as the example in the docs.
CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
)
I'm sure the obvious is staring me right in the face. Nevertheless I would appreciate any help offered.
Update: I just tried the example from the documentation, returns the same message. So may I conclude the documentation is in error, or that 8.4.4 is buggy?
I'm no Postgresql expert, but it appears the message is simply to inform you that an INDEX is being created to assist in the implementation of the PRIMARY KEY that you defined.
It's not ignoring your primary key, it's telling you the mechanism it will use to enforce it. This message can be disabled with client_min_messages (warning).