[mysql] library operation + table operation


If you want to use mysql normally, you must ensure that there are these three files

  • mysql is the client of the database
  • mysqld is the server side of the database
    (mysql is the mode of server and client)
    insert image description here

start mysql

(Start the mysql server, that is, mysqld)

systemctl start mysql

insert image description here
mysql is a network layer service and an application layer process
insert image description here

log in to mysql

View version
insert image description here
When you first log in, mysql has only one root account

1. MySQL environment installation and basic understanding

Modify the port number

The default port number is 3306, you can modify the port number

vim /etc/my.cnf

insert image description here

Then restart mysql

systemctl restart mysqld

connect to the server

mysql -h 127.0.0.1 -P 3306 -u root -p

server, database, table relationship

  • create database

Entering the database

does not specify the ip by default, and if the port is not specified, it is local by default. The essence of these steps just now when 3306
insert image description here
insert image description here
creates the database
insert image description here
insert image description here
insert image description here
is: I build a sql on my client, and the sql statement is handed over to us through the network or inter-process communication server process. The server process received my sql command, and then there must be a corresponding system call inside his process to help us create this directory, which is called a database

build table

If you want to create a table under the database, you must first enter the database
to use the XXX database

use xxx

insert image description here
Check which database you are in now

select database();

Create a table below.
insert image description here
What is the essence of building a database on Linux? In mysq, creating a database create database XXX essentially creates a directory under lnux! What is the essence of
building a table on Linux? ? In mysgl In, the essence of creating a table is to create the corresponding file on Linux!
insert image description here
Insert information into the table

insert image description here

query the data in the table
insert image description here
insert image description here

Lecture 2_Library Operation

Basic functions of the database:

  • More attribute operations on database and table structures
  • For database and table structure, more data content operations

insert image description here

1. Create a database

create database 数据库名;

2. Create a database case

1. Create a database
insert image description here
insert image description here
insert image description here

3. Specify the character set and checksum

insert image description here

The impact of validation rules on the database

Setting the encoding and verification rules of the database, who will essentially affect who? In fact, it affects the corresponding internal tables in the database, and the corresponding encoding and verification rules

(1) Create a database gc_db, use utf8_ general_ ci [case-insensitive] for verification rules
(2) create a database bin_db, use utf8_ bin [case-sensitive] for verification rules

Case-insensitive query and results:

(3) Choose to use the gc_db database
insert image description here
(4) Create a table structure in the database

create table person(name varchar(20))

Check the attribute column of a table:

desc person;

insert image description here
Insert some properties:

insert image description here

insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');

Case-insensitive query and result:
name is a

select * from person
select * from person where name='a';

insert image description here

Case-sensitive queries and results

Use the bin_db database, go through the above steps again, create tables, insert attributes
insert image description here
insert image description here

Sort by name attribute

1. Case sensitive

default ascending sort

select * from person order by name;

insert image description here
2. Case insensitive

default ascending sort
insert image description here

delete database

The corresponding database folder is deleted, cascaded deletion, and all the data tables in it are deleted

drop database 数据库名;

Backup and restore – last

backup

mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径

Example: backup mytest library to file (exit connection)

 mysqldump -P3306 -u root -p123456 -B mytest > D:/mytest.sql

insert image description here

find database

show database;

modify database

  • The modification of the database mainly refers to modifying the character set of the database, and the verification rules

Example: Change the character set of mytest database to gbk

alter database gc_db charset=gbk;

insert image description here

alter database gc_db charset set utf8mb4;

insert image description here

insert image description here
Currently does not support the modification of the database name, you can modify the table name

Lecture 3_Table operation

create table

create table XXX
create table users (
id int,
name varchar(20) comment '用户名',
password char(32) comment '密码是32位的md5值',
birthday date comment '生日'
) character set utf8 engine MyISAM;

Create table case:
insert image description here
insert image description here

view table

  • View table structure
desc XXX;

insert image description here

  • view table
select * from 表名;

modify table

In the actual development of the project, the structure of a certain table is often modified, such as field name, field size, field type, character set type of the table,
storage engine of the table, and so on. We also have requirements, adding fields, removing fields, and so on. Then we need to modify the table
to add/modify/delete the table

ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename DROP (column);
  • Add a field in the users table to save the image path
mysql> alter table users add assets varchar(100) comment '图片路径' after birthday;

insert image description here

  • Add two records in the users table
mysql> insert into users values(1,'a','b','1982-01-04'),(2,'b','c','1984-01-
04');

insert image description here

  • Modify the name and change its length to 60
mysql> alter table users modify name varchar(60)

insert image description here

  • delete the password column

Note: Be careful when deleting a field, as the deleted field and its corresponding column data are gone

mysql> alter table users drop password;

insert image description here

  • Modify the table name to employee
mysql> alter table users rename to employee;

insert image description here

  • Change the name column to xingming
mysql> alter table employee change name xingming varchar(60); --新字段需要完整定义

insert image description here

delete table

drop table t1;

Mysql user management

create user

grammar:

create user '用户名'@'登陆主机/ip' identified by '密码';

case:

mysql> create user 'whb'@'localhost' identified by '12345678';

insert image description here

delete users

grammar:

drop user '用户名'@'主机名'

Example:
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_47952981/article/details/130081658