schema.sql 908 B

1234567891011121314151617181920212223242526272829303132
  1. CREATE TABLE authors (
  2. author_id SERIAL PRIMARY KEY,
  3. name text NOT NULL DEFAULT ''
  4. );
  5. CREATE INDEX authors_name_idx ON authors(name);
  6. CREATE TYPE book_type AS ENUM (
  7. 'FICTION',
  8. 'NONFICTION'
  9. );
  10. CREATE TABLE books (
  11. book_id SERIAL PRIMARY KEY,
  12. author_id integer NOT NULL REFERENCES authors(author_id),
  13. isbn text NOT NULL DEFAULT '' UNIQUE,
  14. book_type book_type NOT NULL DEFAULT 'FICTION',
  15. title text NOT NULL DEFAULT '',
  16. year integer NOT NULL DEFAULT 2000,
  17. available timestamp with time zone NOT NULL DEFAULT 'NOW()',
  18. tags varchar[] NOT NULL DEFAULT '{}'
  19. );
  20. CREATE INDEX books_title_idx ON books(title, year);
  21. CREATE FUNCTION say_hello(s text) RETURNS text AS $$
  22. BEGIN
  23. RETURN CONCAT('hello ', s);
  24. END;
  25. $$ LANGUAGE plpgsql;
  26. CREATE INDEX books_title_lower_idx ON books(title);