postgreSQL

·

1 min read

psql -h localhost -d postgres

CREATE TABLE subjects(
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name varchar(20) NOT NULL
);

Alter table

ALTER TABLE subjects
ADD COLUMN credits varchar(10) NOT NULL;

ALTER TABLE subjects
ADD COLUMN credits INT;

Drop table

DROP TABLE users;

DISPLAY CONTENTS

SELECT * FROM subjects;

Insert values into table

INSERT INTO (name)
VALUES(‘web_tech’);

Update

UPDATE subjects
SET name = ‘probability’
WHERE id =1;

DELETE FROM subjects
WHERE id=1;

relationship:

defining a column that references another column, which forces every value in the column to exist in the column we’re referencing to.

CREATE TABLE users(
user_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
id int REFERENCES subjects(id),
user_name text NOT NULL,
reg_no varchar(10) NOT NULL
);

to create a user we need a subject first.

Foreign-key constraints: "users_id_fkey" FOREIGN KEY (id) REFERENCES subjects(id)