The first day of database learning

MySQL learning first day

Today's content

First, get to know MySQL

1. Basic concepts of MySQL

  1. Warehouse for storing data
  2. It is essentially a file system, or it is stored on the server's computer in the form of files.
  3. All relational databases can be managed using common SQL statements DBMS DataBase Management System
  4. Enterprises generally use oracle database and MySQL database. Because the oracle database is expensive, small and medium-sized enterprises generally do not use it. Generally speaking, they use powerful and free MySQL database. There is also the SQLite database, which is mainly used for embedded development, such as the development of Android mobile phone systems.

2. MySQL installation

  1. To install MySQL, first ensure that the software does not exist on the computer, then the first step is to determine whether the MySQL software has been installed locally. Open the cmd command line and enter services.msc, open the service management, check whether there is MySQL software in it, and uninstall it if there is one, otherwise you can install it directly.
  2. After viewing this machine, MySQL is not installed, so here is the record of the installation steps first

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Note here! Do not choose the last option. After you purchase a dedicated server in your work, you can provide all available resources to the database for use!

Insert picture description here
The general purpose of the mysql database is selected here, "Multifunctional Database (general multifunctional, good)", "Transactional Database Only (server type, focusing on transaction processing, general)", "Non-Transactional Database Only (non-transactional, Simpler, mainly for some monitoring and counting purposes. The support for MyISAM data type is limited to non-transactional), press "Next" to continue.
Insert picture description here
Insert picture description here
Here you can set the number of concurrent connections acceptable to the server. After I choose 20
Insert picture description here
options , I can Access our server on other machines, otherwise you can only use your own computer. The following is whether to use strict standards. After startup, no minor errors will be tolerated. Novices can turn it off, and it should be turned on after familiarization to ensure that no error data is entered. Database
Insert picture description here
Set the database silent language, generally set to utf8 mode.
Insert picture description here
Choose whether to install mysql as a windows service, you can also specify Service Name (service identification name), whether to add the mysql bin directory to the Windows PATH (after adding , You can directly use the file under bin without pointing out the directory name, such as connecting, "mysql.exe -uusername -ppassword;" is fine, without pointing out the full address of mysql.exe, it is very convenient), I will type all of them here The Service Name remains unchanged. Press “Next” to continue.
Insert picture description here
Ask if you want to modify the password of the default root user (super management). “Enable root access from remote machines (whether to allow root users to log in on other machines, if you want to be safe , Don’t tick it, if it’s convenient, tick it)”. Finally, “CreateAn Anonymous Account (create an anonymous user, anonymous users can connect to the database, but can’t manipulate data, including queries)”,
Insert picture description here
Just wait for the installation!
Insert picture description here
If this warning appears unfortunately, then you need to check whether the MySQL database has been installed. Just delete the original one and reinstall it. If it still doesn’t work, check whether the
previous password is as described in the above step. Modify, follow the above operation; if it still does not work, back up the data folder in the mysql installation directory, and then delete it. After the installation is complete, delete the data folder generated by the installation, move the backed-up data folder back, and restart mysql The service is enough. In this case, you may need to check the database and then repair it once to prevent data errors.

3. Uninstall the database

Insert picture description here
(1) First stop the MySQL service of the window. Find "Control Panel" -> "Administrative Tools" -> "Services" and stop the MySQL background service.
Insert picture description here
(2) Then find mysql in the control panel and delete it

(3) Next, delete all files in the MySQL installation directory.

(4) Finally, delete the MySQL directory in the ProgramDate directory on the c disk. The path is: C:\ProgramData\MySQL (it is a hidden file and needs to be displayed)

4. Start and close the database service

Insert picture description here

首先以管理员身份打开cmd

Insert picture description here

To turn off the service, you only need to enter the following command in the console:net stop mysql

Insert picture description here
To open the service, enter:net start mysql

5. Log in to the database

There are a total of three ways
(1) The command to log in to the database is: if you mysql -u用户名 -p密码
Insert picture description here
don’t want the password to be seen by others when you enter it, enter -p first, and then press Enter
(2). mysql -hip 地址 -u 用户名 -p 密码
Insert picture description here
(3) mysql --host=ip 地址 --user=用户名 --password=密码
Insert picture description here
The command to exit mysql: `quit or exit

6. The relationship between data management system, database and table

Here is a picture to illustrate that Insert picture description here
our computer actually acts as a MySQL server. The server has database server software, the software stores databases, each database stores data tables, and the tables store one piece of data. It can also be called a record. , Client software can access our database server software through the network, so as to access the database and the data in it.

2. Formally learn database theory knowledge

The concept of SQL

1. What is SQL?

Structured Query Language

2. What is it for?

  1. It is a query specification for all relational databases, supported by different databases.
  2. A general database operating language can be used in different databases.
  3. Different database SQL statements have some differences. In
    Insert picture description here
    this way, after being written in one database system, it can be used in different database systems. Each database system has its own unique part

3. SQL statement classification

  1. Data Definition Language (DDL data definition language) such as: building a database, building a table
  2. Data Manipulation Language(DML data manipulation language), such as: adding, deleting and modifying records in the table
  3. Data Query Language(DQL data query language), such as: query operations in the table
  4. Data Control Language(DCL data control language), such as: setting user permissions

4.SQL syntax

  1. Each statement ends with a semicolon, if it is not necessary in SQLyog.
    Insert picture description here
    If you don’t add a semicolon, it’s useless to press enter all the time
  2. SQL is not case-sensitive. The keywords are considered to be the same as uppercase and lowercase. Uppercase is recommended
  3. 3 kinds of annotations:
Single line comment 1 Multi-line comments Single-line comment 2 (this is a MySQL-specific comment method)
--空格 /* */ #

5. DDL operation database

Create database

(1) Create a database: CREATE DATABASE 数据库名;
(2) Determine whether the database exists, or not create it if it exists: CREATE DATABASE IF NOT EXISTS 数据库名;
(3) Create a database and specify the character set:CREATE DATABASE 数据库名 CHARACTER SET 字符集;

View database

(1) View the database, view all databases: show databases;
(2) View the database, view the definition information of a certain database: `show create database database name;

Modify the database

(1) Modify the default character set of the databasealter database db1 character set utf8;
Insert picture description here

Delete database

(1) `drop database database name;

Use database

(1) View the database being used: select database();
(2) Switch/use the databaseuse 数据库名;

6. DDL operation table structure

The first is to enter a database

Create table

(1) Statement to create a table:create table table1(字段名 字段类型,字段名 字段类型);
Insert picture description here

(2) Detailed data types:
Insert picture description here
Insert picture description here
here is an extra talk about the data type
date: date, which only contains year, month and day, yyyy-MM-dd
datetime: date, which contains year, month, day, hour, minute and second yyyy-MM-dd HH:mm:ss
timestamp : The time error type includes year, month, day, hour, minute, and second yyyy-MM-dd HH:mm:ss.
If you do not assign a value to this field in the future, or assign a value to null, the current system time will be used by default to automatically assign the value
Another point to note is that the character length must be specified after the field type of varchar, otherwise an error will be reported! !
(3) Quickly create a table with the same table structurecreate table 新表名 like 旧表名;
Insert picture description here

View table

(1) View all tables show tables;
(2) View table structuredesc 表名;
Insert picture description here

(3) View the statement that creates the tableSHOW CREATE TABLE 表名;
Insert picture description here

Delete table

(1) Delete the table drop 表名;
(2) Delete the table if the table exists:DROP TABLE IF EXISTS 表名;

Modify table

(1) Add column alter table 表名 add 列名 数据类型
Insert picture description here
(2) Modify table type alter table 表明 modify 列名 新的类型;
Insert picture description here
(3) Modify column name: ALTER TABLE 表名 CHANGE 旧列名 新列名 类型;
Insert picture description here
Insert picture description here
(4) Delete column alter table 表名 drop 列名;
Insert picture description here
(5) Modify table name rename table 表名to 新表名;
Insert picture description here
(6) Modify character set ALTER TABLE 表名 character set 字符集;
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_54814599/article/details/115206186