MySQL Basics - MySQL database client connection, data model, SQL knowledge

  • About the author: A cloud computing network operation and maintenance personnel, sharing the technology and dry goods of network and operation and maintenance every day. 

  •  Motto: Keep your head down and hurry on your way, be respectful

  • Personal homepage: Homepage of Netdou

Table of contents

foreword

1. The client connects to MySQL

2. Data model

1. Relational database (RDBMS)

2. Data model

Three.SQL

1. SQL general syntax

2. SQL classification

3. Database operation

1). Query all databases

 2). Query the current database

3). Create a database

the case

A. Create an itcast database, using the default character set of the database.

 B. Create an itheima database and specify the character set

 4). Delete the database

 5). Switch database


foreword

This series will explain the basics, introduction, operation and maintenance of the MySQL database. This chapter will provide knowledge about the client connection, data model, and SQL of the MySQL database.


1. The client connects to MySQL

1). Method 1: Use the client command line tool provided by MySQL

2). Method 2: Use the command line tool that comes with the system to execute the command

mysql [-h 127.0.0.1] [-P 3306] -u root -p
参数:
-h : MySQL服务所在的主机IP
-P : MySQL服务端口号, 默认3306
-u : MySQL数据库用户名
-p : MySQL数据库用户名对应的密码

 [ ] are optional parameters. If you need to connect to remote MySQL , you need to add these two parameters to specify the remote host IP and port. If

To connect to the local MySQL , there is no need to specify these two parameters.

 Note: When using this method to connect, you need to configure the PATH environment variable after the installation is complete.


2. Data model

1. Relational database (RDBMS )

Concept: Based on the relational model, it is a database composed of multiple interconnected two-dimensional tables.
The so-called two-dimensional table refers to a table composed of rows and columns, as shown in the figure below (similar to Excel table data, with headers, columns, and rows, and one column can also be associated with a column of data in another table ).

The MySQL , Oracle , DB2 , and SQLServer we have known before are all relational databases, which store data based on two-dimensional tables.
Simply put, a database that stores data based on two-dimensional tables becomes a relational database, and a database that is not based on two-dimensional tables stores data is a non-relational database.
Features:
  • A. Use tables to store data, the format is uniform, and it is easy to maintain.
  • B. Use the SQL language to operate, the standard is unified, and it is easy to use.

2. Data model

MySQL is a relational database, which stores data based on two-dimensional tables. The specific structure diagram is as follows :

 

We can connect to the database management system DBMS through the MySQL client , and then operate the database through the DBMS .

Database Management System (Database Management System) is a large-scale software for manipulating and managing databases, used to establish, use and maintain databases, referred to as DBMS.

You can use SQL statements to operate the database through the database management system, and manipulate the table structure and data in the database.

Multiple databases can be created in one database server, one database can also contain multiple tables, and one table can contain multiple rows of records.


Three.SQL

The full name is Structured Query Language , Structured Query Language. A programming language for manipulating relational databases, which defines
A set of unified standards for operating relational databases .

1. SQL general syntax

Before learning specific SQL statements, let's first understand the syntax of the SQL language.
  • 1). SQL statements can be written in one line or multiple lines, ending with a semicolon.
  • 2). SQL statements can use spaces / indentation to enhance the readability of the statement.
  • 3). The SQL statement of the MySQL database is not case-sensitive, and it is recommended to use capital letters for keywords.
  • 4). Comments:
  • Single-line comment: -- comment content or # comment content
  • Multi-line comment: /* comment content */

2. SQL classification

SQL statements are mainly divided into four categories according to their functions: DDL , DML , DQL , and DCL .
point
kind
full name
illustrate
DDL
Data Definition
Language
Data Definition Language, used to define database objects ( databases, tables,
field )
DML
Data Manipulation
Language
Data manipulation language, used to add, delete, and modify data in database tables
DQL
Data Query Language
Data query language, used to query the records of tables in the database
DCL
Data Control Language
Data control language, used to create database users, control database
access permission

3. Database operation

1) .Query all databases

show databases ;

 2). Query the current database

select database() ;

3). Create a database

create database [ if not exists ] 数据库名 [ default charset 字符集 ] [ collate 排序
规则 ] ; 

the case

A. Create an itcast database , using the default character set of the database.

create database itcast;

In the same database server, two databases with the same name cannot be created, otherwise an error will be reported. 

 This problem can be solved by using the if not exists parameter. If the database does not exist , the database will be created. If it exists, the database will not be created.

create.
create database if not extists itcast;

 B. Create an itheima database and specify the character set

create database itheima default charset utf8mb4;

 4). Delete the database

drop database [ if exists ] 数据库名 ;
If you delete a database that does not exist, an error will be reported. At this point, you can add the parameter if exists , if the database exists, then
Delete is performed, otherwise no delete is performed.

 5). Switch database

use 数据库名 ; 
When we want to operate a table under a certain database, we need to switch to the corresponding database through this command, otherwise it cannot be operated.
For example, to switch to itcast data, execute the following SQL :
use itcast;

Creation is not easy, please pay attention, like, collect, thank you~   

Guess you like

Origin blog.csdn.net/yj11290301/article/details/130551268