-- 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)
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.
PostgreSQL is a open source relational database system and is also knows as Postgres.
CREATE command is used to create a table, schema or an index.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
ALTER command is used to add, modify or delete columns or constraints from the database table.
ALTER TABLE Table_name ADD column_name datatype;
TRUNCATE command is used to delete the data present in the table but this will not delete the table.
TRUNCATE table table_name;
DROP command is used to delete the table along with its data.
DROP TABLE table_name;
RENAME command is used to rename the table name.
ALTER TABLE table_name1 RENAME to new_table_name1;
INSERT Statement is used to insert new records into the database table.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Select statement is used to select data from database tables.
SELECT column1, column2, ...
FROM table_name;
UPDATE statement is used to modify the existing values of records present in the database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE statement is used to delete the existing records present in the database table.
DELETE FROM table_name where condition;