Five days to learn MySQL, the first day

First, the basic concept of database

1. English word of database: DataBase Abbreviation: DB
2. Database: warehouse for storing and managing data
3. Features of database:
– Persistent storage of data. In fact, the database is a file system
- convenient for storing and managing data
- using a unified way to operate the database - SQL
4, common database software
Oracle MySQL SQL Server
5, MySQL service startup
- cmd - > services.msc Open the service window
– Use administrator to open cmd to
open: net start mysql
to close: net stop mysql
MySQL login: mysql -uroot -p password
MySQL exit: exit quit

2. SQL

2.1. What is SQL?

Structured Query Language: Structured Query Language
actually defines the rules for operating all relational databases. There are differences in the way each database operates, called "dialects".

2.2, SQL general syntax

SQL statements can be written on a single or multiple lines, ending with a semicolon.
Spaces and indentation can be used to enhance the readability of the statement.
The SQL statement of the MySQL database is not case-sensitive, and the keywords are recommended to be capitalized
. 3 Comments: Single-line comment: -- comment content or # comment content
Multi-line comment: /* comment*/

2.3, SQL classification

DDL (operational database, table)

1. Operating the database: CRUD

-c(create): create
Create a database: create database database name;
create a database, judge that it does not exist, then create: create database if not exists database name;
create a database and specify a character set: create database database name character set character set name ;
Exercise: Create a db4 database, determine whether it exists, and specify the character set as 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;
judge that the database exists, and then delete it if it exists: drop database if exists database name;

- Use database to
query the name of the database currently in use: select database();
use database: use database name;

2. Operation table: DQL (data in query table)

Data type: int integer type double decimal type
date date, only includes year, month and day, yyyy-MM-dd
datetime date, including year, month, day, hour, minute and second, yyyy-MM-dd HH-mm-ss
timestamp: time error type includes year Month, day, hour, minute, second, yyyy-MM-dd HH-mm-ss (if this character is not assigned a value in the future, or the assignment is null, the current system time will be used by default to automatically assign value)
varchar string char

-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);
copy table: create table table name like copied table name;

-r(retrieve): Query
Query all table names in a database: show tables;
query table structure: desc table name;

-d(delete): delete
the drop table table name;
drop table if exists table name;

-u(update): Modify
Modify the table name: alter table table name rename to new table name;
modify the character set of the table: alter table table name character set character set name;
add a column: alter table table name add column name data type ;
Modify column name and type: alter table table name change column name new column name new data type;
alter table table name modify column name new data type;
delete column: alter table table name drop column name;

3. DML (add, delete, modify the data in the table)

Add data: insert into table name (column name 1, column name 2...column name n) values ​​(value 1, value 2... value n);
Note: Column names and values ​​should be in one-to-one correspondence. If the column name is not defined after the table name, insert into table name values ​​(value 1, value 2... value n)
is added to all columns by default ; except for numeric types, other types need to be enclosed in quotation marks (single and double can be used)

-Delete data: delete from table name [where condition]
Note: If no conditions are added, delete all records in the table.
If you want to delete all records: 1. delete from table name; How many times to perform delete operations 2. truncate table table name; -- Recommended use, more efficient, delete the table first, and then create the same table.

-Modify data: update table name set column name 1 = value 1, column name 2 = value 2... where condition;
Note: If no condition is added, all records in the table will be modified

4. DQL (query data in the table)

select * from table name;

Syntax:
select field list
from table name list
where condition list
group by grouping field
having condition after grouping
order by sorting
limit paging limit

Basic query: 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.
Removing duplicates: distinct
Calculated columns: Generally, four arithmetic operations can be used to calculate the values ​​of some columns. (Generally, only numerical calculations are performed)
ifnull (expression 1, expression 2): the operation involving null, the calculation results are all null
expression 1: which field needs to be judged whether it is null
expression 2: the field is null The replacement value
after it is aliased: as :as can be omitted.

Conditional query:
where clause followed by conditional
operator: > < <= >= = <> between…and in(set)
like fuzzy query _: single character %: multiple characters
is null and or && or or || not or !

Guess you like

Origin blog.csdn.net/weixin_45573296/article/details/123219287