postgres
1、docker-compose文件
version: "3.8"
services:
postgres:
container_name: postgres
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: mypassword
ports:
- "5432:5432"
volumes:
- dbdata:/var/lib/postgresql/data
phppgadmin:
container_name: phppgadmin
image: neimheadh/phppgadmin:latest
ports:
- "8080:80"
environment:
- POSTGRES_HOST=postgres
- POSTGRES_PORT=5432
- POSTGRES_USER=user
- POSTGRES_PASS=mypassword
volumes:
dbdata:
2、Running the containers
docker-compose up -d
3、Enter iterative mode
docker exec -it postgres psql -U user
4、Some useful commands
# show tables from current schema
\dt
# list tables from all schemas
\dt *.*
# show table
\d TABLE_NAME
# show databases
\l
5、Create table and insert content
CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME text);
INSERT INTO company(id,name) values (1,'test');
SELECT * from company;
6、Go to phpPgAdmin
http://localhost:8080/
7、Go back to terminal. Simple SQL queries
# NOTE: when doing queries, uncheck the paginate results
# create a new db
create database employee_database;
# create table
create table employee (
last_name varchar(150),
department_id int,
primary key (last_name)
);
# insert content
insert into employee (last_name, department_id) values
('Rafferty', 31),
('Jones', 33),
('Steinberg', 33),
('Robinson', 34),
('Smith', 34);
# creat another table
create table department (
department_id int,
department_name varchar(150),
primary key ( department_name )
);
# insert content
insert into department (department_id, department_name) values
(31, 'Sales'),
(33, 'Engineering'),
(34, 'Clerical'),
(35, 'Marketing');
# visualizing tables
select *
from employee;
select *
from department;
# single relation query
select department_name
from department dept
where dept.department_id = 33;
# join, (must specify ON in postgres)
# select * from employee emp join department dept
select * from employee emp join department dept ON true
# join with filtering
select *
from employee emp join department dept
on emp.department_id=dept.department_id
# delete (rows)
delete from employee
where last_name='Jones';
# update
update employee
set department_id=37
where last_name = 'Robinson';
# alter table command, add column, drop column
alter table employee
add salary real null;
update employee
set salary = 5000
where department_id = 33;
update employee
set salary = 6000
where department_id = 31;
update employee
set salary = 7000
where department_id = 34;
# Useful arithmetic functions
select avg(salary)
from employee;
# drop column
alter table employee
drop column department_id;
8、Shutdown
docker-compose down
mysql
1、docker-compose文件
version: "3.8"
services:
db:
image: mysql:latest
container_name: db
command: --default-authentication-plugin=mysql_native_password
restart: unless-stopped
environment:
MYSQL_USER: user
MYSQL_ROOT_PASSWORD: mypassword
MYSQL_PASSWORD: mypassword
MYSQL_DATABASE: testdb
volumes:
- my-db:/var/lib/mysql
ports:
- '3306:3306'
phpmyadmin:
container_name: phpmyadmin
image: phpmyadmin/phpmyadmin:latest
ports:
- "8082:80"
environment:
MYSQL_ROOT_PASSWORD:
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: mypassword
volumes:
my-db:
2、Go to phpMyAdmin
http://localhost:8082/
3、Create database and the schema
CREATE DATABASE freedbtech_language
CREATE TABLE programming_languages
(
id INT(11) NOT NULL auto_increment ,
name VARCHAR(255) NOT NULL ,
released_year INT NULL ,
githut_rank INT NULL ,
pypl_rank INT NULL ,
tiobe_rank INT NULL ,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME on UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP ,
PRIMARY KEY (id),
UNIQUE idx_name_unique (name(255))
)
engine = innodb charset=utf8mb4 COLLATE utf8mb4_general_ci;
4、Insert some data into the table
INSERT INTO
programming_languages(id,name,released_year,githut_rank,pypl_rank,tiobe_rank)
VALUES
(1,'JavaScript',1995,1,3,7),
(2,'Python',1991,2,1,3),
(3,'Java',1995,3,2,2),
(4,'TypeScript',2012,7,10,42),
(5,'C#',2000,9,4,5),
(6,'PHP',1995,8,6,8),
(7,'C++',1985,5,5,4),
(8,'C',1972,10,5,1),
(9,'Ruby',1995,6,15,15),
(10,'R',1993,33,7,9),
(11,'Objective-C',1984,18,8,18),
(12,'Swift',2015,16,9,13),
(13,'Kotlin',2011,15,12,40),
(14,'Go',2009,4,13,14),
(15,'Rust',2010,14,16,26),
(16,'Scala',2004,11,17,34);
|