DB & SQL Flashcards
Архитектура СУБД
- Ядро - процессы, сеть, память, файловая система
- Диспетчер данных - транзакции, кэш
- Диспетчер запросов - парсер запросов, оптимизатор, исполнитель
- Инструменты для служебных операций - резервное копирование, восстановление, мониторинг
run mysql in terminal
mysql -u root -p
create a db
CREATE DATABASE database_name;
delete a db
DROP DATABASE database_name;
switching to a db
USE database_name;
create a table
CREATE TABLE tablename ( column_name data_type, column_name data_type );
show a table
desc table;
show tables;
delete a table
drop table;
inserting data
INSERT INTO table_name(column_name) VALUES (data);
a table with primary key constraint
CREATE TABLE unique_cats ( cat_id INT NOT NULL, name VARCHAR(100), age INT, PRIMARY KEY (cat_id) );
aliases
SELECT cat_id AS id, name FROM cats;
updating data
UPDATE table SET column WHERE condition;
run SQL files from mysql cli
source folder/script.sql
combine data from several columns
SELECT concat(column1, ‘ ‘, column2) AS [name] FROM table;
combine data from several columns using separator
select concat_ws(separator, col1, col2, …) as [name] from table;
how to use substring
select substring(text|column, start, end) as [name] from table; select substring(text|column, start) as [name] from table; select substring(text|column, -start) as [name] from table;
how to replace symbols in a string
select replace(text|column, old, new) as [name] from table;
reverse strings
select reverse(string|column)
count characters in a string
select char_length(string|column)
change case of a string
select upper|lower(string|column)
get unique values of a column
select distinct column from table;
sort results of selections
select column from table order by condition;
select column from table order by condition desc;
select col1, col2, col3 from table order by 2;
how to limit number of results
select column from table limit x;
select column from table limit start, end;
non strict search
select * from table where column like ‘condition’