DEV Community

Ganga Sri V
Ganga Sri V

Posted on

CREATE TABLES

For all the question I just give the basic syntax to create the table but for especially question 7 it asks for combination of two columns to be unique that was give some difficulty for me.

1.CREATE TABLE students(id INT PRIMARY KEY,name VARCHAR(50),age INT);
CREATE TABLE

2.CREATE TABLE employee(name VARCHAR(50) NOT NULL,email VARCHAR(50) NOT NULL,phone_number INT);
CREATE TABLE

3.CREATE TABLE users(user_name VARCHAR(100) UNIQUE,email VARCHAR(100) UNIQUE);
CREATE TABLE

4.CREATE TABLE products(price INT check(price>0), stock INT check(stock >= 0));
CREATE TABLE

5.CREATE TABLE orders(status VARCHAR(50) default 'pending', created_at timestamp default NOW() );
CREATE TABLE

  1. CREATE TABLE accounts(account_number INT UNIQUE NOT NULL,balance INT check(balance >= 0));
    CREATE TABLE

  2. CREATE TABLE enrollment(student_id INT,course_id INT, UNIQUE(course_id,student_id));
    CREATE TABLE

  3. CREATE TABLE departments(id INT PRIMARY KEY, name VARCHAR(100));
    CREATE TABLE
    postgres=# CREATE TABLE employees(name VARCHAR(100),department_id INT,FOREIGN KEY(department_id) REFERENCES departments(id));
    CREATE TABLE

9.ALTER TABLE employees
DROP CONSTRAINT employees_department_id_fkey;
ALTER TABLE
postgres=# ALTER TABLE employees ADD FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE

Top comments (0)