Skip to content

Instantly share code, notes, and snippets.

@IgorHalfeld
Last active July 5, 2017 03:23
Show Gist options
  • Save IgorHalfeld/e6008f3c10b097d06a8296f86a83f3c7 to your computer and use it in GitHub Desktop.
Save IgorHalfeld/e6008f3c10b097d06a8296f86a83f3c7 to your computer and use it in GitHub Desktop.

DDL = Data Definition Language

  • create database
  • create table
  • alter table
  • drop table

DML = Data Manipulation Language

  • insert into
  • update
  • delete
  • truncate

DQL = Data Query language

  • select

Create DB

create database dbname
default character set utf8
default collate utf8_general_ci;

Create Table

create table pessoas (
	id int not null auto_increment,
	nome varchar(30) not null,
  nascimento date,
  sexo enum('M', 'F'),
  peso decimal(5,2),
  altura decimal(3,2),
  nacionalidade varchar(20) default 'Brasil',
  primary key (id)
) default charset = utf8;
create table if not exists cursos (
  nome varchar(30) not null unique,
  descricao text,
  carga int unsigned,
  totaulas int unsigned,
  ano year default '2016'
  ) default charset=utf8;

Insert into

insert into tablename values(id, name, age, date)

Alter table

Add column

alter table pessoas
add column profissao varchar(10);
alter table pessoas
add column profissao varchar(10) after nome;
alter table pessoas
add column profissao varchar(10) first;

Remove column

alter table pessoas
drop column profissao;

Modify column

alter table pessoas
modify column profissao varchar(20) not null default '';

Change column name

alter table pessoas
change column profissao prof varchar(20) not null default '';

Rename table

alter table pessoas
rename to gafanhotos;

Add primary key

alter table cursos
add primary key(idcurso);

Update

update cursos
set nome = 'Java', carga = '40'
where id = '1';

Delete

delete from cursos
where id = '8';

Truncate

truncate table cursos;

Select

select nome, ano from cursos where ano between 2014 and 2016
select nome, ano from cursos where ano in (2014, 2016) order by ano;
select nome, carga, totaulas from cursos where carga > 35 and totaulas < 30;
select * from cursos where nome like 'p%';
select * from cursos where nome like 'ph%p%';
select * from cursos where nome like 'ph%p_';
select distinct nacionalidade from gafanhotos;
select count(nome) from cursos;
select max(carga) from cursos;
select max(totaulas) from cursos where ano = '2016';
select min(totaulas) from cursos where ano = 2016;
select sum(totaulas) from cursos;
select avg(totaulas) from cursos where ano = '2016';
select carga, count(nome) from cursos group by carga;
select ano, count(*) from cursos group by ano having count(ano) >= 5 order by count(*) desc;
select carga, count(*) from cursos where ano > '2015' group by carga having carga > (select avg(carga) from cursos);

Foreign key

alter table pessoas
add foreign key (cursopreferido)
references cursos(idcurso);
select gafanhotos.nome, gafanhotos.cursopreferido, cursos.nome, cursos.ano
from gafanhotos join cursos
on cursos.idcurso = gafanhotos.cursopreferido;
select g.nome, g.cursopreferido, c.nome, c.ano
from gafanhotos as inner join cursos as c
on c.idcurso = g.cursopreferido;
select g.nome, g.cursopreferido, c.nome, c.ano
from gafanhotos as g left outer join cursos as c
on c.idcurso = g.cursopreferido;
select g.nome, g.cursopreferido, c.nome, c.ano
from gafanhotos as g right outer join cursos as c
on c.idcurso = g.cursopreferido;
create table gafanhoto_assiste_curso (
	id int not null auto_increment,
    data date,
    idgafanhoto int,
    idcurso int,
    primary key (id),
    foreign key (idgafanhoto) references gafanhotos(id),
    foreign key (idcurso) references cursos(idcurso)
) default charset = utf8;
select g.nome, c.nome from gafanhotos g
join gafanhoto_assiste_curso a
on g.id = a.idgafanhoto
join cursos c
on c.idcurso = a.idcurso
order by g.nome;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment