Big data development-mysql

introduce

DB: database (database): a "warehouse" that stores data. It holds a series of organized data.
DBMS: Database Management System (Database Management System). A database is a container created and operated by a DBMS
SQL: Structured Query Language (Structure Query Language): A language designed to communicate with a database.

Common database management systems: MySQL, Oracle, DB2, SqlServer, etc.

SQL language

SQL (Structural query language) statements are divided into the following three types: +
DML: Data Manipulation Language Data Manipulation Language
DDL: Data Definition Language Data Definition Language
DCL: Data Control Language Data Control Language

DML: DML is used to query and modify data records

  • INSERT: add data to the database
  • UPDATE: Modify data in the database
  • DELETE: delete data in the database
  • SELECT: select (query) data

DDL: used to define the structure of the database, such as creating, modifying or deleting database objects

  • CREATE TABLE: Create a database table
  • ALTER TABLE: change table structure, add, delete, modify column length
  • DROP TABLE: delete the table
  • CREATE INDEX: Create an index on the table
  • DROP INDEX: delete the index

DCL: used to control database access

  • GRANT: grant access
  • REVOKE: revoke access
  • COMMIT: Commit transaction processing
  • ROLLBACK: transaction processing rollback
  • SAVEPOINT: set savepoint
  • LOCK: Lock a specific part of the database

Data Processing Inquiry

Display table structure: describe table name

Escaping conforms to: escape

function

  • ROUND: Round
    ROUND(45.926, 2) 45.93
  • TRUNCATE: 截断truncate
    TRUNCATE(45.926,0) 45
  • MOD: Remainder
    MOD(1600, 300) 100

any operator, all operator

Create and manage tables

Create database create database database name;
view all current databases show databases;
use database: use employees;

Create table with subquery:
create table emp1 as select * from employees;

Append a new column: ALTER TABLE dept80 ADD job_id varchar(15);

Modify a column: ALTER TABLE dept80 MODIFY (last_name VARCHAR(30));
Table altered.
Modifications to the default value only affect future modifications to the table

Drop a column:
ALTER TABLE dept80 DROP COLUMN job_id;
Table altered.

Drop table:
DROP TABLE dept80;
Table dropped.

Empty the table:
TRUNCATE TABLE detail_dept;
Table truncated.
The TRUNCATE statement cannot be rolled back, and the DELETE statement deletes data and can be rolled back

Addition, deletion and modification of data processing

更新:
UPDATE table
SET column = value [, column = value, …]
[WHERE condition];

删除:
DELETE FROM table
[WHERE condition];

database transaction

Transaction: A group of logical operation units that transform data from one state to another.
A database transaction consists of the following parts:

  • one or more DML statements
  • A DDL (Data Definition Language - Data Definition Language) statement
  • A DCL (Data Control Language - Data Control Language) statement

Constraints and pagination

constraint

In order to ensure the consistency and integrity of the data, the SQL specification imposes additional conditions on the table data in a constrained manner.
Constraints are enforced at the table level.
Constraints can be specified when the table is created (via the CREATE TABLE statement), or after the table is created (via the ALTER TABLE statement

There are the following six constraints:
NOT NULL non-null constraint, which stipulates that a field cannot be empty
UNIQUE unique constraint, which stipulates that a field is unique in the entire table
PRIMARY KEY primary key (not empty and unique)
FOREIGN KEY foreign key
CHECK check constraint
DEFAULT default value

According to the restriction of constrained data columns, constraints can be divided into:
Single-column constraints: each constraint only constrains one column
Multi-column constraints: each constraint can constrain multiple columns of data

According to the scope of constraints, constraints can be divided into:
column-level constraints can only act on one column, following the definition of the column;
table-level constraints can act on multiple columns, not together with the column, but defined separately

paging

Use limit to implement pagination. The first 10 records
in the current paragraph (how many items are in each page, the current page) : SELECT * FROM table LIMIT 0,10; the 11th to 20th records: SELECT * FROM table LIMIT 10, 10;

Formula:
(current page number - 1) * number of entries per page, number of entries per page
SELECT * FROM table LIMIT(PageNo - 1)*PageSize,PageSize;

Guess you like

Origin blog.csdn.net/Sun123234/article/details/129661551