Basic concepts and instructions of MySQL database

One, the basic concept of the database

Database -> Data Table
Row (record): used to describe the information of an object
Column (field): used to describe an attribute of the object

(1) The composition of the database

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

Table: Organize different records together to store specific data

Database: A collection of tables, a warehouse
for storing data, a collection of related data stored in a certain organizational manner

(2) Database management system (DBMS)

System software that realizes effective organization, management and access to database resources

Function: database establishment and maintenance function, data definition function, data manipulation function, database operation management function, communication function

(3) Database system (DBS)

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

2. Today's mainstream databases

SQL Server (product of Microsoft Corporation)

  • Simple and easy to use for Windows operating system

Oracle (A product of Oracle Corporation)

  • Safe, complete, and complex operation for all mainstream platforms

DB2 (product of IBM)

  • Large, safe and complete for all mainstream platforms

MySQL (acquired by Oracle)

  • Free, open source, small size

3. Introduction to relational database

Relational database system is a database system based on relational model

(1) The data structure of the relational model uses a simple and easy to understand two-dimensional data table

Each row is called a record, which is used to describe the information of an object.
Each row is called a field, which is used to describe an attribute of the object.

(2) The relational model can be represented by a simple "entity-relation-attribute"

  • Entities are also called instances,
    corresponding to "events" or "things" that can be distinguished from other objects in the real world, such as bank customers, bank accounts, etc.

  • The corresponding relationship between the set of relational entities is called a connection or a relationship. For
    example, there is a "savings" relationship between a bank customer and a bank account.

  • A certain characteristic of
    an attribute entity. An entity can have multiple attributes. For example, each entity in the "bank customer" entity set has attributes such as name, address, and phone number.

Fourth, the introduction of non-relational databases

Non-relational databases are also known as 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. It has high scalability and high availability for the efficient storage and access of massive data.

Five, MySQL database introduction

A popular open source relational database
Oracle's product complies with the GPL agreement and can be used and modified for free

Features

  • Excellent performance and stable service
  • Open source, no copyright restrictions, low cost
  • Multi-threaded, multi-user
  • Based on C/S (client/server) architecture
  • Safe and reliable

Six, SQL operating language

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

SQL language classification:

  • DDL: Data Definition Language, used to create database objects, such as libraries, tables, indexes, etc.
  • DML: Data manipulation language, used to manage the data in the table
  • 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
(1) Commonly used data types
Types of significance
int Integer
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 effective length numbers with 2 digits after the decimal point

Case insensitive, semicolon ";" means end

(2) View the database structure
查看当前服务器中的数据库
SHOW DATABASES;	

查看数据库中包含的表
USE 数据库名;
SHOW TABLES;

查看表的结构(字段)
USE 数据库名;
DESCRIBE [数据库名.]表名;
可缩写成:DESC 表名;
(3) Create and delete databases and tables
创建新的数据库
CREATE DATABASE 数据库名;

创建新的表
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]);
#主键一般选择能代表唯一性的字段不允许取空值(NULL),一个表只能有一个主键。

删除指定的数据表
DROP TABLE [数据库名.]表名;				#如不用USE进入库中,则需加上数据库名

4.删除指定的数据库
DROP DATABASE 数据库名;
(4) Data records in the management table
向数据表中插入新的数据记录
INSERT INTO 表名(字段1,字段2[,...]) VALUES(字段1的值,字段2的值,...);

查询数据记录
SELECT 字段名1,字段名2[,...] FROM 表名 [WHERE 条件表达式];

修改、更新数据表中的数据记录
UPDATE 表名 SET 字段名1=字段值1[,字段名2=字段值2] [WHERE 条件表达式];

在数据表中删除指定的数据记录
DELETE FROM 表名 [WHERE 条件表达式];
(5) Modify the table name and table structure
修改表名
ALTER TABLE 旧表名 RENAME 新表名;

扩展表结构(增加字段)
ALTER TABLE 表名 ADD address varchar(50) default 'xxxx';
#default 'xxxx':表示此字段设置默认值 地址不详;可与 NOT NULL 配合使用

修改字段(列)名,添加唯一键
ALTER TABLE 表名 CHANGE 旧列名 新列名 数据类型 [unique key];

删除字段
ALTER TABLE 表名 字段名;

Guess you like

Origin blog.csdn.net/weixin_51468875/article/details/113246494