Thursday, September 29, 2011

Comandos Mysql (Command Mysql)

---------------------------------------------------------------------------------------------------------------------------------
Com uma prova de BD amanhã resolvi estudar aqui    ;-)
"With a future proof BD decided to study here"   
---------------------------------------------------------------------------------------------------------------------------------
Some of the classes command are: data selection, manipulation and definition among others, more important today is the one for my study.
  • Manipulation : insert, update, delete.
  • Definition : create,alter,drop,rename ...
  • Selection : select.
#command line ;-)


how to create a database?



>mysql create database blog;


to use your DB !

mysql> use blog;


creating a table?



mysql> create table pessoa(
    -> id_p int(10) primary key auto_increment,
    -> nome varchar(30) not null);

how to insert value in the table 'pessoa' ?


mysql> insert into pessoa (nome) values ('andre');
mysql> insert into pessoa (nome) values ('will');
mysql> insert into pessoa (nome) values ('keila');
mysql> insert into pessoa (nome) values ('safira');
mysql> insert into pessoa (nome) values ('glenda');

how to retrieve values 'pessoa'?
>mysql select * from pessoa;


+------+--------+
| id_p | nome |
+------+--------+
|    1 | andre   |
|    2 | will       |
|    3 | keila    |
|    4 | safira   |
|    5 | glenda |
+------+--------+

how to create table with the relationship between two tables,recuparar your values?



(1 'pessoa' may have "N" 'contato')



mysql> create table contato(
    -> id_c int(10) primary key auto_increment,
    -> numero int (10) not null,
    -> id_p int(10) not null,
    -> index id_p(id_p),
    -> foreign key (id_p) references pessoa(id_p));

how to insert value in the table 'contato' ?



mysql> insert into contato (numero,id_p) values(88225190,1);
mysql> insert into contato (numero,id_p) values(23123434,1);
mysql> insert into contato (numero,id_p) values(98453434,2);
mysql> insert into contato (numero,id_p) values(56353664,3);
mysql> insert into contato (numero,id_p) values(38793664,4);
mysql> insert into contato (numero,id_p) values(77793664,5);
mysql> insert into contato (numero,id_p) values(66793664,5);

how to retrieve values 'pessoa & contato'?


mysql> select * from pessoa inner join contato on pessoa.id_p = contato.id_p;

+------+--------+------+----------+------+
| id_p | nome | id_c | numero | id_p |
+------+--------+------+----------+------+
|    1 | andre    |   3 | 88225190 |  1 |
|    1 | andre    |   4 | 23123434 |  1 |
|    2 | will        |   5 | 98453434 |  2 |
|    3 | keila     |   6 | 56353664 |   3 |
|    4 | safira    |   7 | 38793664 |  4 |
|    5 | glenda |   8 | 77793664 |  5 |
|    5 | glenda |   9 | 66793664 |  5 |
+------+--------+------+----------+------+--

some kind of selection !

mysql> select * from pessoa where nome like 'a%';

+------+-------+
| id_p | nome  |
+------+-------+
|    1 | andre |
+------+-------+

mysql> select * from pessoa where nome like 'a%' or nome like 'g%';

+------+--------+
| id_p | nome   |
+------+--------+
|    1 | andre  |
|    5 | glenda |
+------+--------+

mysql> select * from pessoa where nome regexp '^[A-H]';

+------+--------+
| id_p | nome   |
+------+--------+
|    1 | andre  |
|    5 | glenda |
+------+--------+

mysql>  Select * from pessoa limit 2,2;

+------+--------+
| id_p | nome   |
+------+--------+
|    3 | keila  |
|    4 | safira |
+------+--------+

modifying table !
no implemented by me!
sintax 
*ALTER TABLE pessoa DROP column
*ALTER TABLE pessoa CHANGE nome name char(30) not null; 
last 

mysql> desc pessoa;



+-------+-------------+------+-----+---------+----------------+

| Field | Type        | Null | Key | Default | Extra          |

+-------+-------------+------+-----+---------+----------------+

| id_p  | int(10)     | NO   | PRI | NULL    | auto_increment |
| nome  | varchar(30) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

mysql> alter table pessoa add data date;
Query OK, 5 rows affected 

current

mysql> desc pessoa;



+-------+-------------+------+-----+---------+----------------+

| Field | Type        | Null |     Key | Default | Extra  |

+-------+-------------+------+-----+---------+----------------+

| id_p  | int(10)     |NO        | PRI | NULL    | auto_increment |
| nome| varchar(30) | NO  |         | NULL   |           |
| data  | date        | YES      |         | NULL   |          |
+-------+-------------+------+-----+---------+----------------+


mysql> select * from pessoa;

+------+--------+------+
| id_p | nome   | data |
+------+--------+------+
|    1 | andre  | NULL |
|    2 | will      | NULL |
|    3 | keila    | NULL |
|    4 | safira   | NULL |
|    5 | glenda | NULL |
+------+--------+------+


*checks

mysql> show tables;

+----------------+
| Tables_in_blog |
+----------------+
| contato        |
| pessoa         |
+----------------+

mysql> desc contato;

+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra     |
+--------+---------+------+-----+---------+----------------+
| id_c   | int(10) | NO   | PRI  | NULL   | auto_increment |
| numero | int(10) | NO|         | NULL   |                |
| id_p   | int(10) | NO   | MUL | NULL  |                |
+--------+---------+------+-----+---------+----------------+

mysql> desc pessoa;

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id_p  | int(10)     | NO   | PRI | NULL    | auto_increment |
| nome  | varchar(30) | NO|     | NULL    |                |
| data  | date        | YES  |        | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

union
requires tables with the same fields.

mysql> desc pessoa;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id_p    | int(10)     | NO   | PRI | NULL    | auto_increment |
| nome    | varchar(30) | NO   |     | NULL    |                |
| caddate | date        | NO   |     | NULL    |                |
| idade   | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

mysql> desc person;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id_p    | int(10)     | NO   | PRI | NULL    | auto_increment |
| nome    | varchar(30) | NO   |     | NULL    |                |
| caddate | date        | NO   |     | NULL    |                |
| idade   | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+

mysql> insert into person (nome,caddate,idade) values('andre',current_date,21);
mysql> insert into person (nome,caddate,idade) values('marcelo',current_date,17);

mysql> insert into pessoa (nome,caddate,idade) values('cleo',current_date,48);
mysql> insert into pessoa (nome,caddate,idade) values('francisco',current_date,49);

mysql> select * from person;
+------+---------+------------+-------+
| id_p | nome    | caddate    | idade |
+------+---------+------------+-------+
|    1 | andre   | 2011-09-29 |    21 |
|    2 | marcelo | 2011-09-29 |    17 |
+------+---------+------------+-------+

mysql> select * from pessoa;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+

mysql> select * from person union select * from pessoa;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | andre     | 2011-09-29 |    21 |
|    2 | marcelo   | 2011-09-29 |    17 |
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+

some kind of selection !

mysql> select * from pessoa where caddate >= 2011-09-29  ;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+
mysql> select * from pessoa where month(caddate) = 09;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+

mysql> select * from pessoa where day(caddate) = 29;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+


mysql> select * from pessoa where year(caddate) = 2011;
+------+-----------+------------+-------+
| id_p | nome      | caddate    | idade |
+------+-----------+------------+-------+
|    1 | cleo      | 2011-09-29 |    48 |
|    2 | francisco | 2011-09-29 |    49 |
+------+-----------+------------+-------+

0 comentários:

Post a Comment