CREATE TABLE example_table (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) UNIQUE,
age INTEGER CHECK (age >= 18),
department_id INTEGER REFERENCES department(id)
);
In this example, the example_table has the following constraints:
- Primary key: The id column is defined as a primary key, which ensures that each row in the table has a unique identifier.
- Not null: The name column is defined as NOT NULL, which ensures that a value is entered for each row in the table.
- Unique: The email column is defined as UNIQUE, which ensures that each email address in the column is unique.
- Check: The age column has a CHECK constraint that ensures that the age entered in the column is greater than or equal to 18.
- Foreign key: The department_id column has a REFERENCES constraint that ensures that the department_id entered in the column matches a department table's id column.
I am trying to use postgreSQL to create a blog posting site database. It will have three tables authors, author_details and posts.
In authors table has name:string column only. While the author_details will bio:string, email:string, website:sring, and profile_picture: binary. And posts will have title:string, tag: an arry of string, nad body:text. Author_details will have a referenc key into authors table, one-to-one. The authors will habe one-to-many relationship into posts. That means a author sould have many post.
Please help me to create these table in postgresql.
Sure, here's an example schema for the database:
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE author_details (
id SERIAL PRIMARY KEY,
bio TEXT,
email VARCHAR(255),
website VARCHAR(255),
profile_picture BYTEA,
author_id INTEGER UNIQUE REFERENCES authors(id) ON DELETE CASCADE
/* UNIQUE to enforce the one-to-one relationship between authors and author_details */
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR(255) NOT NULL,
tag VARCHAR(255)[],
body TEXT NOT NULL,
author_id INTEGER REFERENCES authors(id) ON DELETE CASCADE
/* one-to-many relationship between authors and posts */
);
In this schema, the authors table has two columns: id and name. The id column is an auto-incrementing primary key, while the name column stores the name of the author.
The author_details table has five columns: id, bio, email, website, and profile_picture, as well as a foreign key author_id that references the id column in the authors table. The author_id column is marked as UNIQUE to enforce the one-to-one relationship between authors and author_details. The ON DELETE CASCADE option ensures that if an author is deleted, their author_details will also be deleted.
The posts table has four columns: id, title, tag, body, and a foreign key author_id that references the id column in the authors table. The author_id column is marked as a foreign key to enforce the one-to-many relationship between authors and posts. The ON DELETE CASCADE option ensures that if an author is deleted, their posts will also be deleted.