-- create
CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

-- insert
INSERT INTO EMPLOYEE VALUES (0001, 'Clark', 'Sales');
INSERT INTO EMPLOYEE VALUES (0002, 'Dave', 'Accounting');
INSERT INTO EMPLOYEE VALUES (0003, 'Ava', 'Sales');

-- fetch 
SELECT * FROM EMPLOYEE WHERE dept = 'Sales';


--create database Jardineria

CREATE TABLE oficina (
  codigoOficina VARCHAR(10) PRIMARY KEY NOT NULL,
  ciudad VARCHAR(30) NOT NULL,
  pais VARCHAR(50) NOT NULL,
  region VARCHAR(50) DEFAULT NULL,
  codigoPostal VARCHAR(10) NOT NULL,
  telefono VARCHAR(20) NOT NULL,
  direccionUno VARCHAR(50) NOT NULL,
  direccionDos VARCHAR(50) DEFAULT NULL
  );

CREATE TABLE empleado (
  codigoEmpleado INTEGER PRIMARY KEY NOT NULL,
  nombre VARCHAR(50) NOT NULL,
  apellido1 VARCHAR(50) NOT NULL,
  apellido2 VARCHAR(50) DEFAULT NULL,
  extension VARCHAR(10) NOT NULL,
  email VARCHAR(100) NOT NULL,
  codigoOficina VARCHAR(10) NOT NULL,
  codigoJefe INTEGER DEFAULT NULL,
  puesto VARCHAR(50) DEFAULT NULL
  );
alter table empleado
add
--constraint FK_Officina foreign key (codigoOficina) references oficina(codigoOficina),
constraint FK_mJefe foreign key (codigoJefe) references empleado(codigoEmpleado)

CREATE TABLE gamaProducto (
  gama VARCHAR(50) PRIMARY KEY NOT NULL,
  descripcionTexto TEXT,
  descripcionHTML TEXT,
  imagen VARCHAR(256)
 
);

CREATE TABLE cliente (
  codigoCliente INTEGER PRIMARY KEY NOT NULL,
  nombreCliente VARCHAR(50) NOT NULL,
  nombre_contacto VARCHAR(30) DEFAULT NULL,
  apellidoContacto VARCHAR(30) DEFAULT NULL,
  telefono VARCHAR(15) NOT NULL,
  fax VARCHAR(15) NOT NULL,
  direccionUno VARCHAR(50) NOT NULL,
  direccionDos VARCHAR(50) DEFAULT NULL,
  ciudad VARCHAR(50) NOT NULL,
  region VARCHAR(50) DEFAULT NULL,
  pais VARCHAR(50) DEFAULT NULL,
  codigoPostal VARCHAR(10) DEFAULT NULL,
  codigoEmpleado_repVentas INTEGER DEFAULT NULL,
  limiteCredito NUMERIC(15,2) DEFAULT NULL
  );
alter table cliente
add
constraint FK_EmpleadoRV FOREIGN KEY (codigoEmpleado_repVentas) REFERENCES empleado(codigoEmpleado)

CREATE TABLE pedido (
  codigoPedido INTEGER PRIMARY KEY NOT NULL,
  fechaPedido date NOT NULL,
  fechaEsperada date NOT NULL,
  fechaEntrega date DEFAULT NULL,
  estado VARCHAR(15) NOT NULL,
  comentarios TEXT,
  codigoCliente INTEGER NOT NULL
  -- FALTAN LAS CLAVES FORANEAS
);
alter table pedido
add
constraint FK_cliente FOREIGN KEY (codigoCliente) REFERENCES cliente(codigoCliente)

CREATE TABLE producto (
  codigoProducto VARCHAR(15) PRIMARY KEY NOT NULL,
  nombre VARCHAR(70) NOT NULL,
  gama VARCHAR(50) NOT NULL,
  dimensiones VARCHAR(25) NULL,
  proveedor VARCHAR(50) DEFAULT NULL,
  descripcion text NULL,
  cantidadStock SMALLINT NOT NULL,
  precioVenta NUMERIC(15,2) NOT NULL,
  precioProveedor NUMERIC(15,2) DEFAULT NULL
);
alter table producto
add
constraint FK_gama FOREIGN KEY (gama) REFERENCES gamaProducto(gama)

CREATE TABLE detallePedido (
  codigoPedido INTEGER NOT NULL,
  codigoProducto VARCHAR(15) NOT NULL,
  cantidad INTEGER NOT NULL,
  precioUnidad NUMERIC(15,2) NOT NULL,
  numeroLinea SMALLINT NOT NULL

);
 alter table detallePedido
add
constraint FK_producto FOREIGN KEY (codigoProducto) REFERENCES producto(codigoProducto),
constraint FK_PedidoDetalle FOREIGN KEY (codigoPedido) REFERENCES pedido(codigoPedido)


CREATE TABLE pago (
  codigoCliente INTEGER NOT NULL,
  formaPago VARCHAR(40) NOT NULL,
  codigoTransaccion VARCHAR(50) PRIMARY KEY NOT NULL,
  fechaPago date NOT NULL,
  total NUMERIC(15,2) NOT NULL
  
);

 alter table pago
add
constraint FK_ClienteP FOREIGN KEY (codigoCliente) REFERENCES cliente(codigoCliente) 

PostgreSQL online editor

Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About PostgreSQL

PostgreSQL is a open source relational database system and is also knows as Postgres.

Key Features:

  • Postgres is not only free and open-source but also it is highly extensible.
  • Custom Data types and funtions from various programming languaues can be introduced and the good part is compiling entire database is not required.
  • ACID(Atomicity, Consistency, Isolation, Durability) compliant.
  • First DBMS which implemented Multi-version concurrency control (MVCC) feature.
  • It's the default database server for MacOS.
  • It supports all major operating systems like Linux, Windows, OpenBSD,FreeBSD etc.

Syntax help

1. CREATE

CREATE command is used to create a table, schema or an index.

Syntax:

         CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

2. ALTER

ALTER command is used to add, modify or delete columns or constraints from the database table.

Syntax

ALTER TABLE Table_name ADD column_name datatype;

3. TRUNCATE:

TRUNCATE command is used to delete the data present in the table but this will not delete the table.

Syntax

TRUNCATE table table_name;

4. DROP

DROP command is used to delete the table along with its data.

Syntax

DROP TABLE table_name;

5. RENAME

RENAME command is used to rename the table name.

Syntax

ALTER TABLE table_name1 RENAME to new_table_name1; 

6. INSERT

INSERT Statement is used to insert new records into the database table.

Syntax

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

7. SELECT

Select statement is used to select data from database tables.

Syntax:

SELECT column1, column2, ...
FROM table_name; 

8. UPDATE

UPDATE statement is used to modify the existing values of records present in the database table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

9. DELETE

DELETE statement is used to delete the existing records present in the database table.

Syntax

DELETE FROM table_name where condition;