当前位置:网站首页>MySQL learning notes
MySQL learning notes
2022-06-25 12:58:00 【Lingtree】
To be improved after the re examination
Basic concepts of database
1. English words in the database : DataBase abbreviation : DB
2. What database ?
* A warehouse for storing and managing data .
3. The characteristics of database :
1. Persistent storage of data . In fact, the database is a file system
2. Easy to store and manage data
3. Using a unified way to operate the database -- SQLMySQL Database software
1. install
* See 《MySQL Basics .pdf》
2. uninstall
1. Go to mysql The installation directory is found my.ini file
* Copy datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"
2. uninstall MySQL
3. Delete C:/ProgramData In the catalog MySQL Folder .
3. To configure
* MySQL Service startup
1. Manual .
2. cmd--> services.msc Open the service window
3. Use the administrator to open cmd
* net start mysql : start-up mysql Service for
* net stop mysql: close mysql service
* MySQL Sign in
1. mysql -uroot -p password
2. mysql -hip -uroot -p Password to connect to target
3. mysql --host=ip --user=root --password= Password to connect to target
* MySQL sign out
1. exit
2. quit
* MySQL Directory structure
1. MySQL The installation directory :basedir="D:/develop/MySQL/"
* The configuration file my.ini
2. MySQL Data directory :datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"
* Several concepts
* database : Folder
* surface : file
* data : data
SQL
1. What is? SQL?
Structured Query Language: Structured query language
In fact, it defines the rules for operating all relational databases . There are different ways of database operation , be called “ dialect ”.
2.SQL General grammar
1. SQL Statements can be written in one or more lines , It ends with a semicolon .
2. You can use spaces and indents to enhance the readability of statements .
3. MySQL Database SQL Statement is case insensitive , It is recommended to use uppercase .
4. 3 Species notes
* Single-line comments : -- The comment or # The comment (mysql specific )
* Multiline comment : /* notes */
3. SQL classification
1) DDL(Data Definition Language) Data definition language
Used to define database objects : database , surface , Column, etc. . keyword :create, drop,alter etc.
2) DML(Data Manipulation Language) Data operation language
It is used to add, delete and modify the data in the database . keyword :insert, delete, update etc.
3) DQL(Data Query Language) Data query language
Used to query the records of tables in the database ( data ). keyword :select, where etc.
4) DCL(Data Control Language) Data control language ( understand )
It is used to define the access rights and security level of the database , And create users . keyword :GRANT, REVOKE etc.
DDL: Operating the database 、 surface
1. Operating the database :CRUD
1. C(Create): establish
* Create database :
* create database Database name ;
* Create database , Judgment does not exist , To create a :
* create database if not exists Database name ;
* Create database , And specify the character set
* create database Database name character set Character set name ;
* practice : establish db4 database , Judge whether it exists , And make character set as gbk
* create database if not exists db4 character set gbk;
2. R(Retrieve): Inquire about
* Query all database names :
* show databases;
* Query the character set of a database : Query the creation statement of a database
* show create database Database name ;
3. U(Update): modify
* Modify the character set of the database
* alter database Database name character set Character set name ;
4. D(Delete): Delete
* Delete database
* drop database Database name ;
* Determine that the database exists , Exist and delete
* drop database if exists Database name ;
5. Using a database
* Query the database name currently in use
* select database();
* Using a database
* use Database name ;
2. Operation table
1. C(Create): establish
1. grammar :
create table Table name (
Name 1 data type 1,
Name 2 data type 2,
....
Name n data type n
);
* Be careful : The last column , There is no need to add comma (,)
* Database type :
1. int: Integer types
* age int,
2. double: Decimal type
* score double(5,2)
3. date: date , Include only mm / DD / yyyy ,yyyy-MM-dd
4. datetime: date , Including month, day, hour, minute, second yyyy-MM-dd HH:mm:ss
5. timestamp: Wrong type of time Including month, day, hour, minute, second yyyy-MM-dd HH:mm:ss
* If you don't assign a value to this field in the future , Or assign the value to null, The current system time is used by default , To automatically assign
6. varchar: character string
* name varchar(20): The biggest name 20 Characters
* zhangsan 8 Characters Zhang San 2 Characters
* Create table
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
* Copy table :
* create table Table name like The name of the copied table ;
2. R(Retrieve): Inquire about
* Query all table names in a database
* show tables;
* Query table structure
* desc Table name ;
3. U(Update): modify
1. Modify the name of the table
alter table Table name rename to New table name ;
2. Modify the character set of the table
alter table Table name character set Character set name ;
3. Add a column
alter table Table name add Name data type ;
4. Change column name type
alter table Table name change Name New listing New data types ;
alter table Table name modify Name New data types ;
5. Delete column
alter table Table name drop Name ;
4. D(Delete): Delete
* drop table Table name ;
* drop table if exists Table name ;
SQLYog Installation and use :
Reference blog :
DML: Add, delete and modify the data in the table
1. Add data :
* grammar :
* insert into Table name ( Name 1, Name 2,... Name n) values( value 1, value 2,... value n);
* Be careful :
1. Column name and value should correspond one by one .
2. If the name comes after , No column name defined , The default is to add values to all columns
insert into Table name values( value 1, value 2,... value n);
3. Except for the number type , Other types require quotation marks ( Single and double can ) Lead up
2. Delete data :
* grammar :
* delete from Table name [where Conditions ]
* Be careful :
1. If there is no condition , Then delete all records in the table .
2. If you want to delete all records
1. delete from Table name ; -- It is not recommended to use . How many records will be deleted
2. TRUNCATE TABLE Table name ; -- Recommended , More efficient Delete the table first , Then create the same table .
3. Modifying data :
* grammar :
* update Table name set Name 1 = value 1, Name 2 = value 2,... [where Conditions ];
* Be careful :
1. Without any conditions , All records in the table will be modified .
DQL: Look up the records in the table
* select * from Table name ;
1. grammar :
select
Field list
from
List of table names
where
List of conditions
group by
Grouping field
having
Conditions after grouping
order by
Sort
limit
Paging limit
2. Basic query
1. Multiple field queries
select Field name 1, Field name 2... from Table name ;
* Be careful :
* If you query all fields , You can use * To replace the field list .
2. Remove duplication :
* distinct
3. Calculated column
* Generally, four operations can be used to calculate the values of some columns .( Generally, only numerical calculation will be carried out )
* ifnull( expression 1, expression 2):null Operations involved , The results are null
* expression 1: Which field needs to determine whether it is null
* If the field is null The replacement value after .
4. names :
* as:as You can omit it
3. Conditions of the query
1. where Clause followed by condition
2. Operator
* > 、< 、<= 、>= 、= 、<>
* BETWEEN...AND
* IN( aggregate )
* LIKE: Fuzzy query
* Place holder :
* _: Any single character
* %: More than one arbitrary character
* IS NULL
* and or &&
* or or ||
* not or !
-- Query age is greater than 20 year
SELECT * FROM student WHERE age > 20;
SELECT * FROM student WHERE age >= 20;
-- Query age = 20 year
SELECT * FROM student WHERE age = 20;
-- Inquiry age is not equal to 20 year
SELECT * FROM student WHERE age != 20;
SELECT * FROM student WHERE age <> 20;
-- Query age is greater than or equal to 20 Less than or equal to 30
SELECT * FROM student WHERE age >= 20 && age <=30;
SELECT * FROM student WHERE age >= 20 AND age <=30;
SELECT * FROM student WHERE age BETWEEN 20 AND 30;
-- Check age 22 year ,18 year ,25 Age information
SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
SELECT * FROM student WHERE age IN (22,18,25);
-- The English score is null
SELECT * FROM student WHERE english = NULL; -- It's not right .null Value cannot be used = (!=) Judge
SELECT * FROM student WHERE english IS NULL;
-- The English score is not null
SELECT * FROM student WHERE english IS NOT NULL;
-- Find out what's the name of Ma ? like
SELECT * FROM student WHERE NAME LIKE ' Horse %';
-- The second word for a name is the person who changed
SELECT * FROM student WHERE NAME LIKE "_ turn %";
-- The search name is 3 One word man
SELECT * FROM student WHERE NAME LIKE '___';
-- Search for the person whose name contains De
SELECT * FROM student WHERE NAME LIKE '% Virtue %';
边栏推荐
猜你喜欢

剑指 Offer II 028. 展平多级双向链表

3+1 guarantee: how is the stability of the highly available system refined?

Another night when visdom crashed

2021-09-28
![[转]以终为始,详细分析高考志愿该怎么填](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[转]以终为始,详细分析高考志愿该怎么填

Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token

Idea2017 how to set not to automatically open a project at startup

深圳民太安智能二面_秋招第一份offer

Embedded software development written examination and interview notes (latest update: February 17, 2022)

剑指offer 第 3 天字符串(简单)
随机推荐
2021-09-28
Elemntui's select+tree implements the search function
Oracle backup or restore database (expdp, impdp)
GPS receiver design (1)
Oracle trigger error report table or view does not exist
Drawing cubes with Visio
1024水文
The push(), pop(), unshift(), shift() method in the JS array
Visual studio2019 link opencv
又是被Visdom搞崩溃的一夜
My first experience of go+ language -- a collection of notes on learning go+ design architecture
Methods of strings in JS charat(), charcodeat(), fromcharcode(), concat(), indexof(), split(), slice(), substring()
nacos无法修改配置文件Mysql8.0的解决方法
使用Visio画立方体
2021-09-02
JS SMS countdown implementation (simple code)
康威定律,作为架构师还不会灵活运用?
20220620 面试复盘
Connect with the flight book and obtain the user information according to the userid
Jenkins Pipeline使用