How to operate MySQL database data

Table of contents

1. MySQL database concept

data

table (data table)

database

database management system

Database creation and maintenance functions

data definition function

Data manipulation capabilities

Database operation and management functions

Communication function

data flow

2. Mainstream database classification

1.SQL Server database (Microsoft branch product)

2.Oracle database (Oracle product)

3.DB2 database (IBM product)

4.MySQL database (acquired by Oracle)

3. Relational databases and non-relational databases

4. SQL operating language

Commonly used data types

 View database structure

 Create and delete databases and tables

Manage data records in tables

Modify table name and table structure

Modify the data content in the table


1. MySQL database concept

data

The fields contain attributes, which are "columns". Symbol records describing things, including numbers, text, graphics, images, sounds, file records, etc., are stored in a unified format in the form of "records" (records are "rows")

table (data table)

A table is composed of records and fields (rows and columns), which organizes different records together to store specific data.

database

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

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

database management system

It is system software that realizes effective organization, management and access of database resources.

Database creation and maintenance functions

Including functions such as establishing the structure of the database, data entry and conversion, database dump and recovery, database reorganization and performance monitoring.

data definition function

Including functions such as defining global data structures, local logical data structures, storage structures, confidentiality modes, and information formats to ensure that the data stored in the database is correct, valid, and compatible, and to prevent incorrect data that does not conform to semantics from being input or output.

Data manipulation capabilities

Including data query statistics and data updates

Database operation and management functions

At this time, the core part of the database management system includes functions such as concurrency control, access control, and internal database maintenance.

Communication function

Communication between DBMS and other software systems, such as Access, which can interact with other office components for data

data flow

The application sends a data request and then transfers it to the DBMS. The DBMS converts the high-level instructions into complex machine code (low-level instructions) and then transfers it to the DB database to search for the specified database and identify the language in the database. You can use select to query the specified database. , the DB database returns the data query results, then transfers them back to the DBMS, converts the low-level commands into high-level instructions, and finally feeds back the data processing results to the user

2. Mainstream database classification

1.SQL Server database (Microsoft branch product)

For Windows operating system, it is simple and easy to use, with a graphical interface and you can operate it with just one click.

2.Oracle database (Oracle product)

For all mainstream platforms, with complete security and complex operations (now mainstream databases)

3.DB2 database (IBM product)

For all mainstream platforms, large databases, complete security (IBM Db2)

4.MySQL database (acquired by Oracle)

Free open source database, small size (most used database)

3. Relational databases and non-relational databases

A relational database system is a database system based on the 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, used to describe information about an object.
Each row is called a field, used to describe an attribute of the object.

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

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

The correspondence between relational entity sets is called a link, also called a relationship.
For example, there is a "savings" relationship between bank customers and bank accounts.

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, phone number, etc.

Non-relational databases are also called NoSQL (Not Only SQL)

The advantages of storing data are not based on the relational model and do not require a fixed table format. Non-relational databases

The database can be read and written with high concurrency. It can efficiently store and access massive data. The database has high scalability and high availability.

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

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

4. SQL operating language

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

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 data in tables
DQL: Data query language, used to find qualified data from data tables Data Logging
DCL: Data Control Language, used to set or change database user or role permissions

Commonly used data types

type 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 picture
decimal(5,2) 5 valid length digits, 2 digits after the decimal point

 View database structure

查看当前服务器中的数据库
SHOW DATABASES;	

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

查看表的结构(字段)
USE 数据库名;
DESCRIBE [数据库名.]表名;
可缩写成:DESC 表名;

 Create and delete databases and tables

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

Common constraints
not null #non-null constraint (value cannot be empty)
primary key #primary key constraint (set as primary key, including the effect of not null)
unique key #unique key constraint (value is unique in the field)
default specified value #default Value constraint (set the default value to the specified value, and the string should be enclosed in ' ')
auto_increment #Auto-increment constraint (starts at 1, increases by 1 as the number of record entries increases)
foreign key #Foreign key constraint (change the word field Set as a foreign key, associated with fields in the subtable)
zerofill #If the number of digits is not satisfied, fill it with 0

创建新的数据库
CREATE DATABASE 数据库名;

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

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

删除指定的数据库
DROP DATABASE 数据库名;

Manage data records in tables

向数据表中插入新的数据记录
INSERT INTO 表名(字段1,字段2[,...]) VALUES(字段1的值,字段2的值,...);

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

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

在数据表中删除指定的数据记录
DELETE FROM 表名 [WHERE 条件表达式];

查看表中字段
DESC [表名];

Insert new data into the data table

Please note that when inserting corresponding data, the string needs to be enclosed in quotation marks, otherwise an error will be reported and it will not be recognized as a string.

Modify 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 表名 字段名;

Before modifying the table name, you must first use  use [database name]; to select a database, and also ensure that the selected database has a corresponding table name.

Modify the data content in the table

修改表内数据
update [表名] set [字段]=‘值’  where [字段]=‘值’;

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/131681890