Database mysql management

Database overview

Data (Data)
describes the symbolic records of things,
including numbers, words, graphics, images, sounds, file records, etc.
Stored in a unified format in the form of "records"

Table
organizes unused records together
to store specific data


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

Database Management System (DBMS)
is a system software that can effectively block, manage and access database resources

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

Insert picture description here
Introduction to MySQL database

A popular open source relational database
product
under Oracle. It complies with the GPL agreement and can be used and modified for free.
Features
Excellent performance, stable service,
open source, no copyright restrictions, low cost
, multi-threaded, multi-user
based on C/S (client/ Server) Architecture
Safe and reliable

MySQL Business Edition and Community Edition

The MySQL commercial version is developed and maintained by MySQL AB. You need to pay to use the
MySQL community version. It is developed and maintained by MySQL developers and enthusiasts scattered all over the world. It can be used for free.

SQL statement overview

SQL language

The abbreviation of Structured Query Language, that is,
the standard language of relational database. It is
used to maintain and manage the database,
including data query, data update, access control, object management and other functions.

Classification of commonly used statements in mysql database

DDL (Date Definition Language, data definition language): used to create databases, database objects and definition fields, such as CREATE, ALTER, DROP.
DML (Date Manipulation Language, data manipulation language): used to insert, delete and modify the data in the data, such as INSERT, UPADTE, DELETE
DQL (Date Query Language, data query language): used to query the data in the data, such as SELECT
DCL (Data Control Language, data control language): used to control the access permissions, access rights, etc. of database components, such as COMMIT, ROLLBACK, GRANT, REVOKE

1. Manage databases and tables

1.1, create a database and table

Format:
create table table name (field 01 name field 01 type field 01 constraint, field 02 name field 02 type field 02 constraint,...) storage engine, character set

Field name, type, and constraint explanation:
Field 01 name: Attribute name, custom
Field 01 type: int (4) integer represents 0000-9999
double floating-point
decimal (5, 2) The valid digits are 5 digits, and the decimal point is reserved 2-digit 100.00; 099.50
float single-precision floating point 4-byte
char character
char (10) fixed-length character string, the character string should be enclosed in single quotes
varchar (50) variable-length character string
Field 01 constraint:
non-empty constraint: content It is not allowed to be empty not null
Primary key constraint: non-empty and uniquely identifies the primary key (primary key)
Default constraint: If no data is filled, the default pre-set value is filled in the default'unknown'
auto-increment feature: id 1 2 3 4 auto_increment (automatic Growth)
storage engine: myisam innodb
character set: UTF-8

Create database

create database students;  

Use database

use students;

Create table

create table results(id int(5) auto_increment primary key, age int(3) not null, name varchar(128) not null, score decimal(5));

Insert picture description here
char和varchar

1. The length of char is immutable, while the length of varchar is variable.
Field b: type char(10), value: abc, stored as: abc (abc+7 spaces)
field d: type varchar(10 ), the value is: abc, stored as: abc (automatically changes to the length of 3)
2, automatically intercepted when the length is exceeded
Field c: type char(3), the value is: abcdefg, stored as: abc (defg is automatically deleted)
field e: type varchar(3), value: abcdefg, stored as: abc (defg is automatically deleted)
3. var(10) and char(10), both indicate that 10 characters can be stored, regardless of whether they are stored in numbers, letters or UTF8 Chinese characters (
3 bytes per Chinese character), can store 10
4, char can store up to 255 characters
, the maximum length of varchar is 65535 bytes, the number of characters that can be stored in varchar and the encoding-related
character type is gbk, Each character occupies a maximum of 2 bytes, and the maximum length cannot exceed 32766 characters.
If the character type is utf8, each character occupies a maximum of 3 bytes, and the maximum length cannot exceed 21845 characters.

1.2, insert a column in the data table

alter table results add column addr varchar(64);

Insert picture description here

1.3, delete the database and table

Delete table

format:drop table 库名.表名;

For example: delete the chengji2 table in the students library to
Insert picture description here
delete the database

Insert picture description here

Second, view the database structure

2.1. View existing database information

show databases;

Insert picture description here
mysql default 4 databases

information_schema: Define the way to access database metadata. Database name and table name, column data type, access authority, etc.
mysql: The core database is responsible for storing database users, permissions, keywords and other control and management information that users need to use.
performance_schema:Database performance parameters, storage engine, etc.
sys: The sys system library contains many views, which perform aggregate calculations on the preformance_schema table in various ways.

2.2, view database table information

show tables;

Insert picture description here

2.3. View the structure of the data table

View the structure of the results table

desc results;

Insert picture description here

Three, manage the data in the data table

DML statements are used to manage the data in the table. The
operations included
insert: insert new data
update: update the original data
delete: delete unnecessary data.
Note: use DELETE for data operations, and drop for libraries and tables.

3.1, insert new data content into the data table

insert into results(age,name,score,addr) values(17,'zhangsan',80,501),(18,'lisi',85,502),(18,'wangwu',86,502),(19,'zhaoliu',87,503);

View inserted information

select * from results;

Insert picture description here

3.2. Modify and update the data records in the data table

update results set age=19,score=82 where name='zhangsan';

Insert picture description here

3.3, delete the specified data record in the database

Format: delete from table name where conditional expression (without where means to delete all records in the table)

delete from students.results where name='zhaoliu';

Insert picture description here

Fourth, the advanced operation of the database

4.1, clear the table

delete from info;
truncate table info;
truncate clears the table, the table is still there; drop is to delete all records in the table.
Truncate and delete are the new values ​​of the two and the initial id is different.

4.2, temporary table

Temporarily created table, used to save some temporary data, will not exist for a long time

create temporary table temp_info(id int(4) not null auto_increment, name varchar(32) character set utf8 collate utf8_bin not null, level int(10) not null, primary key(id))engine=innodb default charset=utf8;

adding data

insert into temp_info(id,name,level) values(22,'zhangsan',10),(23,'lisi',20);

You can view the data added in the table; but when you view the table, there is no such table
Insert picture description here

4.3, clone table

method one:

like method: Generate a chengji table from the complete copy structure of the results table

create table chengji like results;

Insert picture description here
Method Two:

show create table results\G;

Copy the following content, modify the name of the table and
Insert picture description here
paste
Insert picture description here

4.4. Import data

insert into chengji select * from results;

Insert picture description here

4.5, database user authorization

1. DCL statement sets user permissions (when the user does not exist, create a new user)

Set the login password to the lisi user abc123, which can log in from any terminal and has full permissions to all libraries and all tables

create user 'lisi'@'%' identified by 'abc123';
grant all on *.* to 'lisi'@'%' identified by 'abc123';
select user,host from mysql.user;

Insert picture description here

Set the login password to user wangwu with abc123, you can log in from the local terminal, and have select permission on the user table in the mysql library

create user 'wangwu'@'loaclhost' identified by 'abc123';
grant select on mysql.user to 'wangwu'@'localhost' identified by 'abc123';

2. View user permissions

View the permissions of the current user

show grants;

Insert picture description here
View the permissions of the wangwu user logged in locally

show grants for 'wangwu'@'localhost';

3. Revoke the user's permissions

revoke select on mysql.user from 'wangwu'@'loaclhost';

4. View users in the current system

select user from mysql.user;

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50345511/article/details/111567466