Linux: SQL - Paiet/Tech-Journal-for-Everything GitHub Wiki
Managing Data with SQL
- Install MySQL
yum install mysql mysql-server
service mysqld start
/usr/bin/mysql_secure_installation
mysql -ppassword
- Install MariaDB
yum install mariadb mariadb-server
systemctl start mariadb
/usr/bin/mysql_secure_installation
mysql -u root -p
insert
- Add a new row to a tableupdate
- Modify an existing row in a tableselect
- Return the values in a selected range of columnsdelete
- Remove a row from a tablefrom
- Specifies the table whose data you want to manipulatewhere
- Specifies criteria that define which elements to work withgroup by
- Combines rows according to a common criteriaorder by
- Sorts returned informationjoin
- Combines data from multiple tables to create a single set of data
Example
SHOW DATABASES;
USE test;
Example
CREATE DATABASE Company;
USE Company;
SHOW TABLES;
CREATE TABLE employees (id int NOT NULL AUTO_INCREMENT PRIMARY KEY, firstname varchar(50), lastname varchar(50), department int);
INSERT INTO employees (firstname, lastname, department) VALUES ('Bob', 'Smith', '1'), ('John', 'Doe', '2'), ('Alan', 'Johnson', '1'), ('Alice', 'Smith', '2');
Example
SELECT * FROM employees;
SELECT * FROM employees WHERE lastname LIKE 'S%';
SELECT * FROM employees GROUP BY department;
SELECT * FROM employees ORDER BY lastname;
Example
CREATE TABLE departments (id int NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(50));
INSERT INTO departments (name) VALUES ('Marketing'), ('Sales');
Example
SELECT employees.firstname, employees.lastname, departments.name FROM employees JOIN departments ON employees.department=departments.id;
SELECT * FROM employees WHERE id='4';
UPDATE employees SET department='1' WHERE id='4';
SELECT * FROM employees WHERE id='4';