Para os exemplos mostrados aqui precisaremos dessas tabelas.
CLIENTE (cod_cli, nome_cli, endereco, cidade, cep, uf)
VENDEDOR (cod_vend, nome_vend, sal_fixo, faixa_comiss)
PEDIDO ( num_ped, prazo_entr, cd_cli, cd_vend)
ITEM_PEDIDO (no_ped, cd_prod, qtd_ped)
PRODUTO (cod_prod, unid_prod, desc_prod, val_unit)
*Todos os exemplos serão rodados no banco Postgres.
Criação de Tabelas
A sintaxe para a criação de tabelas é:
CREATE TABLE <nome_tabela>
(<descrição das colunas>,
<descrição das chaves>);
CREATE TABLE cliente
(
cod_cli smallint NOT NULL,
nome_cli character varying(40) NOT NULL,
endereco character varying(80),
cidade character varying(20),
uf character varying(2),
PRIMARY KEY (cod_cli)
);
CREATE TABLE vendedor
(
cod_vend smallint NOT NULL,
nome_vend character varying(20) NOT NULL,
salario_fixo integer NOT NULL,
faixa_comissao character(1),
PRIMARY KEY (cod_vend)
);
CREATE TABLE produto
(
cod_prod smallint NOT NULL,
unid_prod character(3) NOT NULL,
desc_prod character varying(20),
val_unit integer NOT NULL,
PRIMARY KEY (cod_prod)
);
CREATE TABLE pedido
(
num_ped smallint NOT NULL,
prazo_entr smallint NOT NULL,
cd_cli smallint NOT NULL,
cd_vend smallint NOT NULL,
CONSTRAINT pedido_pkey PRIMARY KEY (num_ped),
CONSTRAINT pedido_cd_cli_fkey FOREIGN KEY (cd_cli)
REFERENCES cliente (cod_cli) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT pedido_cd_vend_fkey FOREIGN KEY (cd_vend)
REFERENCES vendedor (cod_vend) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
CREATE TABLE item_pedido
(
no_ped smallint NOT NULL,
cd_prod smallint NOT NULL,
qtd_ped double precision NOT NULL,
CONSTRAINT item_pedido_cd_prod_fkey FOREIGN KEY (cd_prod)
REFERENCES produto (cod_prod) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT item_pedido_no_ped_fkey FOREIGN KEY (no_ped)
REFERENCES pedido (num_ped) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
Exclusão de Tabelas
Para excluirmos uma tabela existente devemos usar o comando DROP TABLE. A sua forma geral é:
DROP TABLE <nome_tabela>;
*obs: nao exclua as tabelas que você acabou de criar.
exemplo:
drop table item_pedido;