www.samueldiasneto.com: Tutorial MySQL

<<< Voltar Avançar >>>

8. Criando tabelas

O comando para criar tabelas é:

create table TABELA(COLUNAS);

As colunas devem ser listadas separadas por vígula. Ao listar as colunas deve-se seguir a sintaxe:

NOME TIPO OPÇÕES

Exemplo:

create table TABELA(CAMPO1 TIPO OPÇÃO1 OPÇÃO2 OPÇÃO3,
                    CAMPO2 TIPO OPÇÃO1,
		    ......
		    CAMPOn TIPO OPÇÃO1); 

As opções são algumas palavras chaves que determinam algumas características do campo:

Exemplo da criação de tabelas:

aranha:/home/samuel# mysql -u sam -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.11-Debian_4-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases
    -> ;
+----------+
| Database |
+----------+
| CC       |
+----------+
1 row in set (0.07 sec)

mysql> use CC;
Database changed

mysql> create table cc(codigo tinyint unsigned not null primary key,
descricao char(100));
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+--------------+
| Tables_in_CC |
+--------------+
| cc           |
+--------------+
1 row in set (0.00 sec)

mysql> create table unidades(codigo tinyint auto_increment primary key
 not null, unidade char(3));
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+--------------+
| Tables_in_CC |
+--------------+
| cc           |
| unidades     |
+--------------+
2 rows in set (0.00 sec)

mysql> create table dependencias(codigo tinyint auto_increment primary key
 not null, dependencia char(15));
Query OK, 0 rows affected (0.00 sec)

mysql> create table material(codigo char(6) not null primary key,
    -> conta tinyint unsigned not null,
    -> unidade tinyint unsigned not null,
    -> descricao char(50) not null,
    -> qtde bigint unsigned not null,
    -> valor float(5,2) not null);
Query OK, 0 rows affected (0.00 sec)

mysql> show tables
    -> ;
+--------------+
| Tables_in_CC |
+--------------+
| cc           |
| dependencias |
| material     |
| unidades     |
+--------------+
4 rows in set (0.00 sec)

mysql>  

Observe os comandos show databases; e show tables; usados para exibir os bancos de dados disponíveis e as tabelas do banco de dados atual.

Para exibir as configurações de uma tabela use o comando describe. Exemplo:

mysql> describe cc;
+-----------+---------------------+------+-----+---------+-------+
| Field     | Type                | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| codigo    | tinyint(3) unsigned |      | PRI | 0       |       |
| descricao | char(100)           | YES  |     | NULL    |       |
+-----------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> describe material;
+-----------+---------------------+------+-----+---------+-------+
| Field     | Type                | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+-------+
| codigo    | char(6)             |      | PRI |         |       |
| conta     | tinyint(3) unsigned |      |     | 0       |       |
| unidade   | tinyint(3) unsigned |      |     | 0       |       |
| descricao | char(50)            |      |     |         |       |
| qtde      | bigint(20) unsigned |      |     | 0       |       |
| valor     | float(5,2)          |      |     | 0.00    |       |
+-----------+---------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql>  
<<< Voltar Avançar >>>