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
);
Related
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),
);
SQL Server guy, new to PostgreSQL. Created the below table, performed the below insert, then ran a SELECT to see the data, yet the row shows the integer formatted with columns to break up the integer. Is this just a formatting style in the HeidiSQL utility I'm using, or is the data actually being stored as x,xxx,xxx,xxx rather than xxxxxxxxxx.
Table:
CREATE TABLE customer (
business_partner_id INTEGER PRIMARY KEY,
first_name VARCHAR(100),
last_name VARCHAR(100),
organisation_name VARCHAR(200),
date_of_bith DATE,
gender VARCHAR(50),
customer_type VARCHAR(50),
store_joined VARCHAR(10),
store_joined_date DATE,
created_date_time TIMESTAMP,
updated_date_time TIMESTAMP);
Insert:
-- Insert a customer
INSERT INTO customer VALUES
(1029884766,'James','Matson','Unknown','1980-02-17','M','Standard','303',CURRENT_DATE,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)
Query results:
I would like to prevent a mismatch between course_code and course_namewhen inserting values to the table below.
CREATE TABLE course (
course_id INT4 NOT NULL PRIMARY KEY,
course_code CHAR(4) NOT NULL,
course_name VARCHAR(30) NOT NULL
);
For both I created an enumeration (see below), now I want to link 'C101' to 'Computer Science' etc.
CREATE TYPE e_course_code AS ENUM (
'C101',
'B102',
'E103',
'V104',
'A105',
'E104'
);
CREATE TYPE e_course_name AS ENUM (
'Computer Science',
'Business Information Management',
'Electronics',
'Visual Programming',
'Audio Technology',
'Engineering'
);
Is it possible to link specified (enumerated) values for two (or even more) columns? Something that returns an error message when inserting a course_code and course_name that do not match?
The fast and reliable tool to implement what you ask in the title would be a foreign key constraint with MATCH FULL:
CREATE TABLE course (
course_code char(4) PRIMARY KEY
, course_name text NOT NULL
);
CREATE TABLE some_other_table (
some_other_id serial PRIMARY KEY
, course_code char(4)
, course_name text
, -- more columns
, CONSTRAINT course_fk FOREIGN KEY (course_code, course_name)
REFERENCES course(course_code, course_name) MATCH FULL
);
Related:
MATCH FULL vs MATCH SIMPLE in foreign key constraints
However, some_other_table.course_name would be completely redundant, and the clean implementation would be the normalized form instead:
CREATE TABLE some_other_table (
some_other_id serial PRIMARY KEY
, course_code char(4)
, -- more columns
, CONSTRAINT course_fk FOREIGN KEY (course_code) REFERENCES course(course_code)
);
Or you add course_id as PK to the course table and use that as FK column.
You can always add a VIEW to display course_name additionally.
The simplest way to solve this (as i see it) would make two separate tables- one with id and code, another with code and name. See this question - Difference between 3NF and BCNF in simple terms (must be able to explain to an 8-year old) - the example in the answer is similar to your problem.
I have three tables, upvotes, comments, and posts. Since both comments and posts can be upvoted here is what I'm currently using:
CREATE TABLE upvotes(
id PRIMARY KEY,
post_id INTEGER REFERENCES posts(id),
comment_id INTEGER REFERENCES comments(id),
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
CREATE TABLE comments(
id PRIMARY KEY,
body TEXT,
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
CREATE TABLE posts(
id PRIMARY KEY,
title TEXT,
body TEXT,
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
Is there some way to represent both posts and comments together in some sort of view or table where I can more elegantly reference it from upvotes e.g.
CREATE TABLE upvotes(
id PRIMARY KEY,
item_id INTEGER REFERENCES items(id), /*this could be a post or a comment*/
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
Is there a way to create a table or a view items that would make this possible? Is there a better way to group the set of items that can be upvoted such that they all have their own unique primary key id (the reason I am stuck here is because a post and a comment might have the same primary key). Sorry if this is an obvious question, I was not sure how to phrase this when I was searching so it could be a duplicate.
Something like this:
CREATE TABLE comments(
id PRIMARY KEY,
body TEXT,
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
CREATE TABLE posts(
id PRIMARY KEY,
title TEXT,
body TEXT,
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
instead use:
CREATE TABLE user_input(
id PRIMARY KEY,
inputType smallint,
title TEXT,
body TEXT,
user_id INTEGER REFERENCES users(id),
time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
)
And add indices for id, inputType.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
create table Coordinator(
cor_id char(6) primary key,
cor_name varchar(10) not null,
contact_no char(12),
address varchar(50));
create table Student(
student_id char(6) primary key,
name varchar(10) not null,
department char(20),
grade char(6),
percentage smallint,
contact_no char(12),
address varchar(50));
create table Criteria(
crt_id char(6) primary key,
min_cutoff smallint);
create table Company(
cmp_id char(6) primary key,
crt_id char(6),
cmp_name varchar(25) not null,
grade char(6),
package int,
contact_no char(12),
address varchar(50),
foreign key (crt_id) references Criteria(crt_id));
**create table Coordinate_with(
cor_id char(6),
cmp_id char(6),
date char(10),
shift varchar(10),
primary key (cor_id,cmp_id),
foreign key (cor_id) references Coordinator(cor_id),
foreign key (cmp_id) references Company(cmp_id));**
create table Placed_in(
cmp_id char(6),
student_id char(6),
primary key (cmp_id,student_id),
foreign key (comp_id) references Company,
foreign key (student_id) references Student);
create table Criteria_Branch(
crt_id char(6),
branch_allowed varchar(20),
primary key (crt_id, branch_allowed),
foreign key (crt_id) references Criteria(crt_id);
shift is a reserved word. You need to name that column differently.
You should never, ever store dates as char or varchar columns!
Additionally the name date is also not a very good choice. Apart from being a reserved word as well, it doesn't document what the column is about. A "start date", an "end date", a "join date", a "due date", ...
Using char datatype is almost always not going to do what you want due to its values being padded to the defined length. I'm pretty sure you want varchar (or varchar2) instead.
If your IDs (cor_id, ...) are really numbers, you should define those columns as number (or integer).
So the table in question should at least look like this:
create table Coordinate_with
(
cor_id char(6),
cmp_id char(6),
start_date date, -- or whatever name describes better what kind of date that is.
shifted varchar(10), -- the name is just a suggestion, use what best describes this column
primary key (cor_id,cmp_id),
foreign key (cor_id) references Coordinator(cor_id),
foreign key (cmp_id) references Company(cmp_id)
);