Mysql database basics and addition, deletion, modification and query operations

Table of contents

1. Basic concepts of database

2. Database types and commonly used databases

1. Relational database

2. Non-relational database

3. The data type of the database

Four, SQL statement

1 Introduction

2. Classification

Five, the use of SQL statements

1. Database operation

(1) Create a database

​edit

(2) Check the database

(3) Use (enter) the database

​edit

(4) Delete the database

​edit

2. Database table operation

(1) View the tables in the database

(2) Create a database table

(3) View the structure of the database table

(4) Delete the database table

(5) Modify the database table structure

3. Data manipulation in the table

(1) Add record

(2) Query the records in the table

(3) Delete records

(4) Modification records

6. Advanced operation of database table

1.if judge to create a table

2. Clone table

3. View table structure and details

4. Rebuild the table (formatting)

5. Create a temporary table

6. Foreign key joins


1. Basic concepts of database

Data : Symbolic records describing things, including numbers, text, graphics, images, sounds, file records, etc., are stored in a unified format in the form of "records".

Table : organizes different records together to store specific data.

Database : A collection of tables is a warehouse for storing data. A collection of interrelated data stored in a certain organizational manner is a warehouse for organizing, storing and managing data according to the data structure.

Database Management System (DBMS) : It is a system software that realizes the effective organization, management and access of database resources. Database establishment and maintenance functions, data definition functions, data manipulation functions, database operation management functions, and communication functions.

Database system : It is a man-machine system consisting of hardware, OS, database, DBMS, application software and database users. Users can operate databases through DBMS or application programs.

2. Database types and commonly used databases

1. Relational database

A relational database system is a database system based on the relational model

The data structure of the relational model uses an easy-to-understand two-dimensional data table

  • Each row is called a record , which is used to describe the information of an object
  • Each column is called a field and is used to describe a property of the object

The relational model can be represented by a simple "entity-relationship" (ER) diagram


The ER diagram contains three elements: entities (data objects), relationships and attributes

  • MySQL (acquired by Oracle)
  • MariaDB
  • Oracle
  • SQL-Server (Microsoft)
  • DB2(IBM)
  • PostgreSQL

2. Non-relational database

Non-relational database is also called NoSQL (Not Only SQL)

The stored data is not based on the relational model and does not require a fixed table format

Advantages of non-relational databases

  • The database can be read and written with high concurrency
  • Efficient storage and access to massive data
  • Database with high scalability and high availability

        Cache database         Redis, Memcache

        Indexed database         ES (Elasticsearch)

        Time series database         Prometheus

        Document database         MongoDB

3. The data type of the database

integer

type of data meaning
tinyint(m) 1 byte range (-128~127)
smallint(m) 2 byte range (-32768~32767)
mediumint(m) 3 byte range (-8388608~8388607)
int(m)  4 byte range (-2147483648~2147483647)
bigint(m) 8 byte range (±9.22*10 to the 18th power)

floating point

string 

Common Data Types

  • int : integer type
  • float : single precision floating point 4 bytes 32 bits
  • double : double precision floating point 8 bytes 64 bits
  • char : fixed-length character type
  • varchar: variable-length character type
  • text : text
  • image : image
  • decimal(5,2) : 5 valid length digits, with 2 digits after the decimal point

Four, SQL statement

1 Introduction

        SQL statements are used to maintain and manage the database, including functions such as data query, data update, access control, and object management.

2. Classification

        DDL : Data Definition Language, used to create database objects such as libraries, tables, indexes, etc.

        DML : Data Manipulation Language, used to manage data in tables.

        DQL : Data query language, used to find qualified data records from data tables.

        DCL : Data Control Language, used to set or change database user or role permissions.

Five, the use of SQL statements

1. Database operation

(1) Create a database

CREATE DATABASE `database name`;

(2) Check the database

SHOW DATABASES;

(3) Use (enter) the database

USE `database_name`;

(4) Delete the database

DROP DATABASE `database name`;

2. Database table operation

(1) View the tables in the database

SHOW TABLES; Enter the database and view all tables

(2) Create a database table

CREATE TABLE `tablename` (column1 datatype, field2 datatype, ...);

 When creating, add constraints (separated by spaces) after the data type to achieve the constraint effect

Common Constraints effect
not null not-null constraint (value cannot be null)
primary key Primary key constraint (set as primary key, including the effect of not null)
unique key Unique key constraints (values ​​are unique within the field)
default specifies the value Default value constraint (set the default value to the specified value, the string must be enclosed in ' ')
auto_increment Self-increment constraint (starts at 1, increases by 1 as the number of record entries increases)
foreign key Foreign key constraints (set the word field as a foreign key, associated with the field in the subtable)
zerofill If the number of digits is not satisfied, fill with 0

(3) View the structure of the database table

DESC `TableName`;

(4) Delete the database table

DROP TABLE `table name`;

(5) Modify the database table structure

ALTER TABLE `old table name` RENAME `new table name`; modify table name

ALTER TBALE `table name` ADD field name data type; add field

ALTER TABLE `table name` CHANGE old field name new field name data type; modify field name

ALTER TBALE `table name` DROP field name; delete field

3. Data manipulation in the table

(1) Add record

INSERT INTO `TableName` (field1, field2, ...) VALUES (number, 'string');

If there is no non-null constraint, parts of unspecified fields default to NULL.

Fill in the data in all fields without specifying the field, that is, INSERT INTO table name VALUES (number, 'string');

(2) Query the records in the table

SELECT * FROM `table name`; View all records in the table

SELECT * FROM `table name` limit number; view the specified number of records in the table

SELECT * FROM `table name` limit number 1, number 2; view the number 2 records after the specified number 1 in the table

SELECT field x, field y, ... FROM `table name`; query specified field records

SELECT field x, field y, ... FROM `table name` \G Display the queried records vertically (applicable to more fields)

SELECT * FROM `table name` WHERE condition 1 [AND condition 2] [OR condition 3]; query records according to the condition

(3) Delete records

DELETE FROM `table name`; delete all records in the table

DELETE FROM `table name` WHERE condition 1 [AND condition 2] [OR condition 3]; delete specified condition records in the table

                                                                                                             (Generally based on the primary key)

(4) Modification records

UPDATE `table name` SET field = value WHERE condition 1 [AND condition 2] [OR condition 3]; Modify the specified condition record

6. Advanced operation of database table

1.if judge to create a table

Before creating a table, determine whether the table exists, create it if it does not exist, or not create it if it exists.

CREATE TABLE IF NOT EXISTS ` tablename` (field1 datatype, field2 datatype, ...);

2. Clone table

CREATE TABLE `New Table` LIKE `Old Table`; Copy the table structure of the old table by like method
INSERT INTO `New Table` SELECT * FROM `Old Table`; Insert the data queried by the old table into the new table

3. View table structure and details

SHOW CREATE TABLE `table name`; display the table structure in SQL command format, and also display other information about the table

4. Rebuild the table (formatting)

TRUNCATE TABLE `table name`;

The difference from clearing table data
        is delete from table name; it deletes data records line by line, and the deletion efficiency is slow. After execution, the number of deleted record entries will be returned, and the data record will be inserted after deletion. The original maximum record is auto-incremented.

        The truncate table table name; is to directly rebuild the table, and the emptying speed is faster than delete. After the execution, the number of record entries will not be returned, and the data will be inserted after the table is cleared, and the auto-increment field will be incremented from 1 again.

5. Create a temporary table

CREATE TEMPORARY `TableName` (field1 datatype, field2 datatype, ...);

Temporary tables can only be valid in the current connection, and can be added, deleted, modified, and checked normally, but they cannot be seen in show tables , and the temporary table will become invalid after changing or exiting the current connection.

6. Foreign key joins

Definition of foreign key

        If the same attribute field x is the primary key in table 1 but not in table 2, the field x is called the foreign key of table 2.

Understanding of primary key tables and foreign key tables

(1) The table with the public key as the primary key is the primary key table (parent table, main table)

(2) The table with the public key as the foreign key is the foreign key table (slave table, outer table)

Note: The field of the main table associated with the foreign key must be set as the primary key. It is required that the slave table cannot be a temporary table; the fields of the master and slave tables have the same data type, character length and constraints. 

Example: Create two tables, the primary key table is the number and name of the supplier, the foreign key table is the goods table, and the supplier number is used as the foreign key.

First create the primary key table (supplier table)

Add a primary key constraint to the main table (the primary key name is recommended to start with "PK_")

Then create a foreign key table (goods table) 

Add a foreign key to the slave table, and establish a foreign key association between the pid field of the master table and the providers_id field of the slave table (the foreign key name is recommended to start with "FK_" )

When inserting data, first master and then foreign

When deleting data, first external and then primary

Guess you like

Origin blog.csdn.net/wlc1213812138/article/details/131663065