How to create a MySQL database with SQL commands?

1

I would like to know how to create a database in MySQL using SQL commands, which I can run in MySQL Workbench, phpMyAdmin or any other software.

What SQL commands do I need to create a database and its tables?

    
asked by anonymous 17.05.2016 / 21:27

2 answers

10

MySQL Workbench and phpMyAdmin are just visual tools that offer practicality / ease in manipulating a database, SQL commands are independent of it.

That way, assuming you're using some version of MySQL, to create a database the command is CREATE DATABASE

CREATE DATABASE meu_banco_de_dados

For the creation of tables, the command CREATE TABLE :

CREATE TABLE minha_tabela (
    id INT PRIMARY KEY AUTO_INCREMENT,
    campo1 VARCHAR(50),
    campo2 INT,
    campo3 FLOAT
)
    
17.05.2016 / 22:30
4

It's easy:

  • Creating the Database

    CREATE DATABASE NOME_DA_BASE_DE_DADOS
    
  • You use the Database

    USE NOME_DA_BASE_DE_DADOS
    
  • You create the table

    CREATE TABLE NOME_DA_TABELA (
      NOME_DO_CAMPO_ID INT PRIMARY KEY AUTO_INCREMENT,
      NOME_DE_OUTRO_CAMPO INT,
      NOME_DE_OUTRO_CAMPO VARCHAR(255)
    )
    
  • Note that there are several types, I just used INT and VARCHAR here.

        
    18.05.2016 / 13:27