MySQL Basic Knowledge Notes 1--[Basic Grammar]

Today's content

  1. Basic concepts of database

  2. MySQL database software

    1. installation
    2. Uninstall
    3. Configuration
  3. SQL

Basic concepts of database

  1. English words of database: DataBase Abbreviation: DB

  2. What database?

    Warehouse for storing and managing data.

  3. Features of the database:

    Persistent storage of data. In fact, the database is a file system

    Conveniently store and manage data

    Use a unified way to operate the database-SQL

MySQL database software

installation

Refer to the previous blog

Uninstall

  1. Go to the mysql installation directory to find the my.ini file

    Copy datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"

  2. Uninstall MySQL

  3. Delete the MySQL folder in the C:/ProgramData directory.

Configuration

MySQL service start

  1. Manually.
  2. cmd--> services.msc to open the service window
  3. Open cmd with administrator
    • net start mysql: start mysql service
    • net stop mysql: close mysql service

MySQL login

  1. mysql -uroot -p password
  2. mysql -hip -uroot -p connection target password
  3. mysql --host=ip --user=root --password=The password of the connection target

MySQL exit

  1. exit
  2. quit

MySQL directory structure

  1. MySQL installation directory: basedir="D:/develop/MySQL/"

    Configuration file my.ini

  2. MySQL data directory: datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"

    Several concepts:

    Database: Folder

    Table: File

    Data: data

SQL

What is SQL

​ Structured Query Language: Structured Query Language
​ actually defines the rules for operating all relational databases. Each way of database operation is different, which is called "dialect".

SQL general syntax

  1. SQL statements can be written in single or multiple lines, ending with a semicolon.

  2. Spaces and indentation can be used to enhance the readability of statements.

  3. The SQL statements of the MySQL database are not case sensitive, and it is recommended to use uppercase for keywords.

  4. 3 annotations

    Single-line comment: – comment content or # comment content (mysql-specific)

    Multi-line comment: /* comment*/

  5. SQL classification

    DDL (Data Definition Language) data definition language: used to define database objects: databases, tables, columns, etc. Keywords: create, drop, alter, etc.
    DML (Data Manipulation Language) data manipulation language: used to add, delete, and modify the data in the table in the database. Keywords: insert, delete, update, etc.
    DQL (Data Query Language) data query language: used to query the records (data) of the tables in the database. Keywords: select, where, etc.
    DCL (Data Control Language) data control language (understand): used to define database access rights and security levels, and create users. Keywords: GRANT, REVOKE, etc.

DDL: Operation database, table

Operating database: CRUD

C(Create): Create

Create a database:

  • create database database name;

Create a database, determine that it does not exist, and then create:

  • create database if not exists database name;

Create a database and specify the character set

  • create database database name character set character set name;

Exercise: Create a db4 database, determine whether it exists, and set the character set to gbk

  • create database if not exists db4 character set gbk;

R(Retrieve): query

  • Query the names of all databases: show databases;
  • Query the character set of a database: query the creation statement of a database: show create database database name;

U(Update): modify

  • Modify the character set of the database: alter database database name character set character set name;

D(Delete): delete

  • Delete database: drop database database name;
  • Determine if the database exists, and then delete it: drop database if exists database name;

Use database

  • Query the name of the database currently in use: select database();
  • Use database: use database name;

Operation table

C(Create): Create

Syntax:
create table table name (
column name 1 data type 1,
column name 2 data type 2,

column name n data type n
);

Note: There is no need to add a comma (,) in the last column

Database type

  1. int: integer type

    age int,

  2. double: decimal type

    score double(5,2)

  3. date: date, only contains year, month and day, yyyy-MM-dd

  4. datetime: date, including year, month, day, hour, minute and second yyyy-MM-dd HH:mm:ss

  5. timestamp: The timestamp 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

  6. varchar: string

    name varchar(20): The name can be up to 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 table being copied;

R(Retrieve): query

  • Query all the table names in a database: show tables;
  • Query table structure: desc table name;

U(Update): modify

  1. Modify the table name
    alter table table name rename to the new table name;
  2. Modify the character set of the
    table alter table table name character set character set name;
  3. Add a column of
    alter table table name add column name data type;
  4. Modify column name type
    alter table table name change column name new column type new data type;
    alter table table name modify column name new data type;
  5. Delete column
    alter table table name drop column name;

D(Delete): delete

drop table table name;

drop table if exists 表名 ;

Client graphical tools:Navicat

DML: add, delete and modify data in the table

adding data:

grammar:

  • insert into table name (column name 1, column name 2, ... column name n) values ​​(value 1, value 2, ... value n);

note:

  1. The column name and value should correspond one to one.
  2. If the column name is not defined after the table name,
    insert into table name values ​​(value 1, value 2, ... value n) is added by default to all columns ;
  3. In addition to number types, other types need to be enclosed in quotation marks (single and double)

delete data:

grammar:

  • delete from table name [where condition]

note:

  1. If no conditions are added, all records in the table are deleted.
  2. If you want to delete all records
    1. delete from table name;-not recommended. How many delete operations will be performed as many records
    2. TRUNCATE TABLE table name;-recommended, more efficient, delete the table first, and then create the same table.

change the data:

grammar:

  • update table name set column name 1 = value 1, column name 2 = value 2,... [where condition];

note:

If no conditions are added, all records in the table will be modified.

DQL: Query the records in the table

select * from 表名;

grammar:

select
	字段列表
from
	表名列表
where
	条件列表
group by
	分组字段
having
	分组之后的条件
order by
	排序
limit
	分页限定

Basic query

1. Query of multiple fields
select field name 1, field name 2... from table name;

note:

If you query all fields, you can use * to replace the field list.

2. Remove duplication: distinct

3. Calculation column: Generally, you can use the four arithmetic calculations to calculate the value of some columns. (Generally only numerical calculations are performed)

ifnull(expression1,expression2): the operation that null participates in, the calculation result is all null

  • Expression 1: Which field needs to be judged whether it is null
  • If the field is null, the replacement value.

4. Get an alias:

  • as: as can also be omitted

Condition query

where clause followed by conditions

Operator

<<=>==<>

BETWEEN...AND  

IN( 集合) 

LIKE:模糊查询

* 占位符:
  * _:单个任意字符
  * %:多个任意字符

IS NULL  

and&&

or|| 

not!

Basic practice

-- 查询年龄大于20岁
SELECT * FROM student WHERE age > 20;
SELECT * FROM student WHERE age >= 20;

-- 查询年龄等于20岁
SELECT * FROM student WHERE age = 20;

-- 查询年龄不等于20岁
SELECT * FROM student WHERE age != 20;
SELECT * FROM student WHERE age <> 20;

-- 查询年龄大于等于20 小于等于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;

-- 查询年龄22岁,18岁,25岁的信息
SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
SELECT * FROM student WHERE age IN (22,18,25);

-- 查询英语成绩为null
SELECT * FROM student WHERE english = NULL; -- 不对的。null值不能使用 = (!=) 判断
SELECT * FROM student WHERE english IS NULL;

-- 查询英语成绩不为null
SELECT * FROM student WHERE english  IS NOT NULL;

-- 查询姓马的有哪些? like
SELECT * FROM student WHERE NAME LIKE '马%';

-- 查询姓名第二个字是化的人
SELECT * FROM student WHERE NAME LIKE "_化%";

-- 查询姓名是3个字的人
SELECT * FROM student WHERE NAME LIKE '___';

-- 查询姓名中包含德的人
SELECT * FROM student WHERE NAME LIKE '%德%';

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/109083794