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:


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.