How to create a Mysql database with SQL commands?

Asked

Viewed 12,794 times

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 are needed to create a database and its tables?

2 answers

10

The Mysql Workbench and the phpMyAdmin are only visual tools that offer convenience/ease in handling a database, SQL commands are independent of it.

That way, assuming you are using some version of Mysql, to create a database the command is CREATE DATABASE

CREATE DATABASE meu_banco_de_dados

To create tables, use the command CREATE TABLE:

CREATE TABLE minha_tabela (
    id INT PRIMARY KEY AUTO_INCREMENT,
    campo1 VARCHAR(50),
    campo2 INT,
    campo3 FLOAT
)
  • 3

    Very good answer, showed the commands questioned, explained and exemplified succinctly and objectively.+ 1

4

It’s easy:

  1. You create the database

    CREATE DATABASE NOME_DA_BASE_DE_DADOS
    
  2. You use the database

    USE NOME_DA_BASE_DE_DADOS
    
  3. 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’m the one who only used here the INT and the VARCHAR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.