Code Monkey home page Code Monkey logo

sql-e-jdbc-em-java's Introduction

SQL e JDBC com JAVA

Iniciando os estudos em SQL e JDBC em java usando o Banco de Dados PostgreSQL.

Objetivo do repositório

CRUD simples para ajudar a reforçar os Conhecimentos tanto em Java como SQL ultilizando o padrão de Arquitetura DAO(Data Access Objects)

Introdução ao SQL

Subconjuntos da linguagem SQL

DDL (Date Definition Language)

  • CREATE TABLE;
  • ALTER TABLE;
  • DROP TABLE;

DML (Date Manipulation Language)

  • INSERT;
  • DELETE;
  • UPDATE;

DQL (Date Query Language)

  • SELECT;

DCL (Date Control Language)

  • CREATE USER;
  • ALTER USER;

DTL (Date Transaction Language)

  • BEGIN TRANSACTION;
  • COMMIT;
  • ROLLBACK;

Criando Banco de Dados

Criando nosso Banco de Dados usando PostgreSQL.

create database userposjava(
with owner = postgres
    encoding = 'UTF8'
    tablespace = pg_ddefault
    connection limit = -1;
)

Criação de uma tabela

DDL (Date Definition Language) (tabela pai)

create table userposjava(
  id bigint not null,
  nome character varying(255),
  email character varying(255),
  constraint user_pk primary key (id)
)

Manipulação de dados da tebela

DML (Date Manipulation Language)

insert into userposjava (id, nome, email)
values (1, 'José', '[email protected]');  

Trabalhando com SEQUENCE

Passando a criação de usuários de modo manual para automático no Banco.

CREATE SEQUENCE public.usersequence
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 7
  CACHE 1;
ALTER TABLE public.usersequence
  OWNER TO postgres;

transformando o id para único

Nela será aplicado o conceito de um para muitos.

alter table userposjava add unique (id);

Criando a tabela de telefones

(tabela pai)

create table telefoneuser
(
id bigint not null,
numero character varying (255) not null,
tipo character varying (255) not null,
usuariopessoa bigint not null,
constraint telefone_id primary key (id));

Transformando o usuariopessoa em uma Forign Key

Tranformando em forign key usuariopessoa referenciando a tabela Pai o tributo id.

alter table telefoneuser  add foreign key (usuariopessoa) references userposjava(id)

SEQUENCE para telefones

Gerando a criação de telefones automático no Banco.

CREATE SEQUENCE user_telefone_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;
ALTER TABLE user_telefone_seq
  OWNER TO postgres;

Transformando o user_telefone_seq em uma Forign Key

Tranformando em forign key user_telefone_seq referenciando a tabela Pai o tributo id.

ALTER TABLE telefoneuser  ALTER COLUMN id SET DEFAULT nextval('user_telefone_seq'::regclass);

INSERT na tebela Pai

INSERT INTO public.userposjava( nome, email) VALUES ('test10', '[email protected]');

INSERT na tebela telefone

INSERT INTO public.telefoneuser(numero, tipo, usuariopessoa)
    VALUES (' (25) 9 9865-7823', 'celular', '10');

INSERT INTO public.telefoneuser(numero, tipo, usuariopessoa)
        VALUES (' (25) 9 8881-7823', 'celular', '10');

INSERT INTO public.telefoneuser(numero, tipo, usuariopessoa)
        VALUES (' (18) 9 3659-4587', 'celular', '9');

INSERT INTO public.telefoneuser(numero, tipo, usuariopessoa)
        VALUES (' (17) 9 4521-8975', 'celular', '9');

INSERT INTO public.telefoneuser(numero, tipo, usuariopessoa)
        VALUES (' (19) 9 3684-7841', 'celular', '9');                

INSERT na tebela Pai

Trabalhando com relacionamento de tabela com INNER JOIN.

select * from telefoneuser as fone
inner join userposjava as userp
on fone.usuariopessoa = userp.id

DELETE com tabelas relacionadas

primeiro deletando as filhas e depois o Pai.

delete from telefoneuser where usuariopessoa = 10
delete from userposjava where id = 10  

Ferramentas e Tecnologias usadas nesse repositório 🌐


Augusto-Java Augusto-POSTGRESQL

Teste o projeto 👁‍🗨

Download do projeto para testar em sua máquina: https://github.com/AugustoMello09/SQL-e-JDBC-em-Java/archive/refs/heads/main.zip

Entre em contato comigo através dos canais abaixo e desde já, agradeço a atenção. 🤝

sql-e-jdbc-em-java's People

Contributors

augustomello09 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.