Snippets

Sergio Luiz Araújo Silva gestao-de-agua

Created by Sergio Araújo last modified
#!/bin/bash

# Use DBML to define your database structure
# Docs: https://dbml.dbdiagram.io/docs
# https://sempreupdate.com.br/como-criar-um-novo-usuario-e-conceder-permissoes-no-mariadb-e-mysql/

# Here we are using bash "here documment" to access the database and create the tables
mysql=$(cat <<-EOF

# Connect to the MySQL database (change the use accordingly)
mysql -u sergio -p -D gestao_de_agua

# CREATE DATABASE gestao_de_agua;
use gestao_de_agua

CREATE TABLE unidades_consumidoras (
  id INT AUTO_INCREMENT PRIMARY KEY,
  codigo_hidrometro VARCHAR(10) NOT NULL UNIQUE,
  endereco VARCHAR(255) NOT NULL,
  numero VARCHAR(10) NOT NULL,
  responsavel VARCHAR(255) NOT NULL,
  cpf VARCHAR(11) NOT NULL,
  telefone VARCHAR(15) NOT NULL,
  email VARCHAR(255) NOT NULL
);

CREATE TABLE consumos (
  id INT AUTO_INCREMENT PRIMARY KEY,
  id_unidade_consumidora INT NOT NULL,
  leitura_anterior INT NOT NULL,
  leitura_atual INT NOT NULL,
  data_leitura DATE NOT NULL,
  valor_total DECIMAL(10,2) NOT NULL
);

ALTER TABLE consumos
ADD COLUMN id_unidade_consumidora INT NOT NULL,
ADD FOREIGN KEY (id_unidade_consumidora) REFERENCES unidades_consumidoras(id);

EOF
)



Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.