Skip to content

Commit 549b2ff

Browse files
committed
Comandos básicos para MySql
1 parent fe6cc21 commit 549b2ff

13 files changed

+137
-0
lines changed

alter_table-drop_table.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Adding new column and describing tables
2+
describe pessoas;
3+
4+
alter table pessoas
5+
add column profissao varchar(10);
6+
7+
8+
-- Drop
9+
alter table pessoas
10+
drop column profissao;
11+
12+
13+
-- add column and specifying its position
14+
alter table pessoas
15+
add column profissao varchar(10) after nome;

create_cursos.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
create table if not exists cursos(
2+
idcurso int auto_increment,
3+
nome varchar(30) not null unique,
4+
descricao text,
5+
carga int unsigned,
6+
totaula int unsigned,
7+
ano year default '2016',
8+
primary key(idcurso)
9+
) default charset = utf8;

create_not-exist.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
create table cursos if not exists (
2+
nome varchar(30) not null unique,
3+
descricao text,
4+
carga int unsigned,
5+
totaula int unsigned,
6+
ano year default '2021'
7+
) default charset = utf8
8+
9+
-- o unique nesse caso nao vai deixar que o curso tenha seu nome repetido

create_pessoas.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
create table pessoas (
2+
id int not null auto_increment,
3+
nome varchar(30),
4+
nascimento date,
5+
sexo enum('M', 'F'),
6+
peso decimal(5,2),
7+
altura decimal(3,2),
8+
nacionalidade varchar(20) default 'Brasil',
9+
primary key (id)
10+
) default charset = utf8;

delete.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
delete from cursos
2+
where idcurso = '8';
3+
4+
5+
-- Limitando a somente um valor
6+
delete from cursos
7+
where ano = '2010'
8+
limit 1;

insert_data.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
insert into pessoas
2+
(id, nome, nascimento, sexo, peso, altura, nacionalidade)
3+
values
4+
(default, 'Maria', '1999-12-30', 'F', '55.5', '1.63', 'Portugal');
5+
6+
-- Para visualizar os dados inseridos
7+
select * from pessoas;
8+
9+
-- Quando os dados são na ordem correta das colunas não precisamos especificar diretamente as colunas, exemplo:
10+
11+
insert into pessoas values
12+
(default, 'RR', '1999-09-09', 'M', '77.7', '1.77', default);

like.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Retorna todos os nomes dos cursos aonde termine com a letra A
2+
select * from cursos where nome like '%a';
3+
4+
5+
-- Retorna todos os nomes dos cursos aonde comece com a letra A
6+
select * from cursos where nome like 'a%';
7+
8+
9+
-- Retorna todos os cursos que comecem ou terminem com a letra P
10+
select * from cursos where nome like '%p%';

modifying.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Modify
2+
describe pessoas;
3+
4+
alter table pessoas
5+
modify column profissao varchar(20) not null default '';
6+
7+
-- Alterando a estrutura e definindo como nao vazia, importante usar o default '' pois a sintaxe do sql nao permitira sem.
8+
9+
10+
-- Renaming column
11+
describe pessoas;
12+
13+
alter table pessoas
14+
change column profissao prof varchar(20);
15+
16+
-- Nesse caso a sequencia é: Velho > novo.
17+
18+
19+
-- Renaming tables
20+
alter table pessoas
21+
rename to pessoinhas;
22+
23+
describe pessoinhas;

select.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- ordem crescente
2+
select * from cursos
3+
order by nome;
4+
5+
6+
-- ordem decrescente
7+
select * from cursos
8+
order by nome desc;

stepbystep.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use exemplo;
2+
show tables;
3+
describe amigos;
4+
select * from amigos;

table_primary-key.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
create table pessoas (
2+
id int not null auto_increment,
3+
nome varchar(30),
4+
nascimento date,
5+
sexo enum('M', 'F'),
6+
peso decimal(5,2),
7+
altura decimal(3,2),
8+
nacionalidade varchar(20) default 'Brasil',
9+
primary key (id)
10+
) default charset = utf8;

truncate.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Para deletar todos os dados de uma tabela
2+
truncate cursos;

update.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Um unico valor
2+
update cursos
3+
set nome = 'HTML 5'
4+
where idcurso = '1';
5+
6+
7+
-- Alterando mais de um valor
8+
update cursos
9+
set nome = 'PHP', ano = '2015'
10+
where idcurso = '4';
11+
12+
13+
-- Limitando alterações a somente uma linha
14+
update cursos
15+
set nome = 'Java', carga = '40', ano = '2015'
16+
where idcurso = '5'
17+
limit 1;

0 commit comments

Comments
 (0)