Basic concepts of database and basic MySQL command operations (detailed explanations)

One, the basic concept of the database

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)

  • It is a system software function that realizes effective organization, management and access to database resources
    : database establishment and maintenance functions, data definition functions, data manipulation functions, database operation management functions, and communication functions

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 applications

2. Today's mainstream databases

SQL Server (product of Microsoft Corporation)

  • For Windows operating system
  • Simple and easy to use

Oracle (A product of Oracle Corporation)

  • For all major platforms
  • Safe, perfect, complicated operation

DB2 (product of IBM)

  • For all major platforms
  • Large, safe and complete

MySQL (acquired by Oracle)

  • Free, open source, small size

3. Introduction to relational database

1. Relational database system is a database system based on relational model

2. 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 the information of an object
  • Each row is called a field, used to describe an attribute of the object

3. The relational model can be simple"Entity-relation-attribute"To represent

1. Entity

  • Also called an instance, it corresponds to "events" or "things" that can be distinguished from other objects in the real world,
    such as bank customers, bank accounts, etc.

2. Relationship

  • The corresponding relationship between entity sets is called connection, also called relationship. For
    example, there is a "savings" relationship between bank customers and bank accounts.

3. Properties

  • A certain characteristic of an 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 concurrent
  • Efficient storage and access to massive data
  • Database has high scalability and high availability

Five, MySQL database introduction

A popular open source relational database
product owned by Oracle. It
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, compile and install MySQL

1. Software package

MySQL installation package

Drag the installation package to the /opt directory, edit a script file, copy the following content into it, and then source or execute the script. After the script is executed, you can use mysql -u root -p and press Enter to enter.

Shell script one-click deployment-source code compilation and installation of MySQL

Seven, MySQL database basic command operation

Commonly used data types

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

For char, the maximum number of characters can be stored is 255. If the actual length of the data stored in char is smaller than the specified length, spaces will be added to the specified length. If the actual length of the stored data is greater than the specified length, the lower version will be intercepted. The version will report an error

The length of char is immutable, while the length of varchar is variable, that is to say, define a char[10] and varchar[10], if it is stored in'csdn', then the length occupied by char is still 10, in addition to the character'csdn', followed by six spaces, and varchar immediately changed the length to 4

Varchar storage rules:
Below version 4.0, varchar(20) refers to 20 bytes. When storing UTF8 Chinese characters, only 6 (3 bytes per Chinese character) can be stored
above version 5.0, varchar(20) refers to It is 20 characters, no matter whether it is digits, letters or UTF8 Chinese characters (each Chinese character is 3 bytes), it can store 20, the maximum size is 65532 bytes

Insert picture description here

View database structure

1. View the database in the current server

SHOW DATABASES;						#大小写不区分,分号“;”表示结束

Insert picture description here

2. View the tables contained in the database

USE 数据库名;
SHOW TABLES;

Insert picture description here

3. View the structure of the table (fields)

USE 数据库名;
DESCRIBE [数据库名.]表名;
可缩写成:DESC 表名;

Insert picture description here

SQL statement

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 data records that meet the conditions from the data table
DCL Data control language, used to set or change database user or role permissions

1. Create a new database

CREATE DATABASE 数据库名;

2. Create new table

CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...][,PRIMARY KEY (主键名)]);

#Primary key Generally, select fields that can represent uniqueness and do not allow NULL values. A table can only have one primary key.

例:create database SCHOOL;
use SCHOOL;
create table CLASS1 (id int not null,name char(10) not null,sex char(1),primary key (id));

Insert picture description here

3. Delete the specified data table

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

4. Delete the specified database

DROP DATABASE 数据库名;

例:show databases;
drop table SCHOOL.CLASS1;
use SCHOOL;
show tables;

drop database SCHOOL;
show databases;

Insert picture description here

Data record in management table

1. Insert a new data record into the data table

INSERT INTO 表名(字段1,字段2[,...]) VALUES(字段1的值,字段2的值,...);

例:create database SCHOOL;

use SCHOOL;

create table CLASS2 (id int not null,name char(20) not null,sex char(1) not null,primary key (id));

insert into CLASS2 (id,name,sex) values(1,'zhangsan','男');

Insert picture description hereInsert picture description here

2. Query data records

SELECT 字段名1,字段名2[,...] FROM 表名 [WHERE 条件表达式];

例:select * from CLASS2;
select name,sex from CLASS2 where id=1;

Insert picture description here

3. Modify and update data records in the data table

UPDATE 表名 SET 字段名1=字段值1[,字段名2=字段值2] [WHERE 条件表达式];

例:insert into CLASS2 (id,name,sex) values(2,'lisi','女');
insert into CLASS2 (id,name,sex) values(3,'wangwu','男');
select * from CLASS2;

update CLASS2 set id=4 where name='zhangsan';
select * from CLASS2;

update CLASS2 set name='sicong',sex='男' where id=2;
select * from CLASS2;

Insert picture description here
Insert picture description here
Insert picture description here

4. Delete the specified data record in the data table

DELETE FROM 表名 [WHERE 条件表达式];

例:delete from CLASS2 where id=4;
select * from CLASS2;

Insert picture description here

Modify the table name and table structure

1. Modify the table name

ALTER TABLE 旧表名 RENAME 新表名;

例:alter table CLASS2 rename CLASS3;
show tables;
select * from CLASS3;

Insert picture description here

2. Expand the table structure (add fields)

ALTER TABLE 表名 ADD address varchar(50) default '地址不详';

#default'Address unknown': indicates that the default value of this field is set to unknown address; it can be used in conjunction with NOT NULL

例:alter table CLASS3 add address varchar(50) default '地址不详';

Insert picture description here

3. Modify the field (column) name and add a unique key

ALTER TABLE 表名 CHANGE 旧列名 新列名 数据类型 [unique key];

例:alter table CLASS3 change name student_name varchar(20) unique key;
select * from CLASS3;

insert into CLASS3 (id,student_name,sex) values (1,'zhangsan','男');
select * from CLASS3;
insert into CLASS3 (id,student_name,sex) values (4,'zhangsan','男');

Insert picture description here

4. Delete fields

ALTER TABLE 表名 DROP 字段名;

例:alter table CLASS3 drop address;

Insert picture description here

Expand

例:use SCHOOL;
create table if not exists CLASS4 (id int(4) zerofill primary key auto_increment,student_name varchar(20) not null,cardid varchar(18) not null unique key,hobby varchar(50));

Insert picture description here

if not exists Indicates whether the table to be created already exists, and if it does not exist, continue to create
int(4) zerofill Indicates that if the value is less than 4 digits, it will be filled with "0" in front, for example 0001
auto_increment Indicates that this field is a self-growing field, that is, each record is automatically incremented by 1, and the default starts from 1; the self-growing field data cannot be repeated; the self-growing field must be a primary key; if the added record data does not specify the value of this field and add Failure will automatically increment once
unique key Indicates the unique key constraint of this field, the data of this field cannot be repeated; there can be only one primary key in a table, but there can be multiple unique keys in a table
not null Indicates that this field is not allowed to be NULL

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51432770/article/details/113118488