The basic idea of the database
-
English word database: DataBase abbreviation: DB
-
What database?
* For warehouse storage and management of data. -
Database Features:
1. persistent storage of data. In fact, the database is a file system
2. convenience store and manage data
3. Use a unified approach to operational database - SQL -
Common database software
* See "MySQL basis .pdf"
MySQL database software
-
Installation
* see "MySQL basis .pdf" -
Uninstall
the installation directory 1. mysql to find my.ini file
* Copy = datadir "C: / ProgramData / MySQL / MySQL Server 5.5 / the Data /"
2. Uninstall MySQL
3. To delete C: / MySQL files in the ProgramData directory folder. -
Configuration
- MySQL service start
- Manually.
- cmd-> services.msc open window services
- Use Administrator cmd to open
-
net start mysql: mysql start service
-
net stop mysql: mysql shut down service
-
MySQL Login
- mysql -uroot -p password
- mysql -hip -uroot -p password link destination
- mysql --host = ip --user = root --password = password connection target
-
MySQL exit
- exit
- quit
-
MySQL directory structure
- MySQL installation directory: basedir = "D: / develop / MySQL /"
- Profile my.ini
- MySQL data directory: datadir = "C: / ProgramData / MySQL / MySQL Server 5.5 / Data /"
- Several concepts
- Database: Folder
- Table: File
- Data: Data
- Several concepts
- MySQL installation directory: basedir = "D: / develop / MySQL /"
-
SQL
1. What is SQL?
Structured Query Language: Structured Query Language
is actually defines all the rules relational database operations. Existence of each database operation is not the same place, called "dialects."
2.SQL universal grammar
- SQL statements can be single or multiple rows of writing, end with a semicolon.
- You can use spaces to indent and enhance the readability of the statement.
- MySQL database SQL statements are not case sensitive, use uppercase keyword suggestions.
- Three kinds of comments
- Single-line comments: - Annotation content annotation content or # (mysql-specific)
- Multi-line comments: / * comment * /
3. SQL classification
- DDL (Data Definition Language) data definition language
* is used to define the database objects: databases, tables, columns and the like. Keywords: create, drop, alter, etc. - DML (Data Manipulation Language) data manipulation language
* to the data tables in the database additions and deletions. Keywords: insert, delete, update, etc. - DQL (Data Query Language) data query language
* used to query records (data) tables in the database. Keywords: select, where, etc. - DCL (Data Control Language) Data Control Language (understanding)
* is used to define the level of access and security of the database, and create a user. Keywords: GRANT, REVOKE, etc.
DDL: operation of the database, table
1. The operation of the database: CRUD
- C (Create): Creating
-
Create the database:
* the Create Database database name; -
Create a database, judgment does not exist, then create:
* not the Create Database IF EXISTS database name; -
Create a database and specify the character set
* create database database name of the character set and character set; -
Exercise:
* Create db4 database to determine whether there is, and to develop character set is GBK
not the SET GBK Character db4 EXISTS * IF the Create Database;
-
- R (Retrieve): inquiry
- All queries the database name:
* Show Databases; - Query a database character sets: a database query creation statement
* show create database database name;
- All queries the database name:
- U (Update): Modify
- Character set to modify the database
* alter database database name of the character set the character set names;
- Character set to modify the database
- D (Delete): Delete
- Delete the database
* drop database database name; - Judgment database exists, there is then deleted
* drop database if exists database name;
- Delete the database
- Use Database
- Query the database name is currently in use
* select database (); - Use Database
* use the database name;
- Query the database name is currently in use
2. Operating Table
- C (Create): Creating
- Syntax:
Create table Table (
Column name Data type 1,
the column 2 data type name 2,
...
the column name Data type n n
);
* Note: The last column, no comma (,) - Database type:
- int: integer type
* age int, - double: decimal type
* score double (5,2) - date: date, containing only the date, yyyy-MM-dd
- datetime: date, time of year, month, day, hour comprising yyyy-MM-dd HH: mm: ss
- timestamp: Time wrong type contains the minutes and seconds yyyy-MM-dd HH when the date: mm: SS
* If in the future not to the field assignment, or assignment is null, the current system time by default, automatically assign - varchar: String
* name varchar (20): name up to 20 characters
* zhangsan 8 characters seating two characters
- int: integer type
- 创建表
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
); - Copy the table:
- create table like the table name table name to be copied;
- Syntax:
- R (Retrieve): inquiry
- Query a database of all the table names
* show tables; - Lookup table structure
* desc table;
- Query a database of all the table names
- U (Update): Modify
- Modify the table name
* alter table table name rename to new table name; - Modify the table character set
* alter table table name character set the character set name; - Add a
* alter table name table add column data type; - Column Name Type modification
* alter table change table column names new column not new data types;
* alter table column name table modify the new data type; - Remove Columns
* alter table table name drop column names;
- Modify the table name
- D (Delete): Delete
* drop table table name;
* drop table IF EXISTS table;
- The client graphical tools: SQLYog
DML: deletions in the data table changes
- Adding Data:
* Syntax:
* INSERT INTO table name (column names 1, 2 column names, column names ... n) values (value 1, value 2, ... n-value);
* Note:
1. The column names and values correspond to .
2. If the table name, column name is not defined, the default value is added to all columns
insert into table values (value 1, value 2, ... value n-);
3. In addition to the digital type, other types require the use of quotation marks (single and double can) cause to - To delete data:
* Syntax:
* the Delete from table [where condition]
* Note :
1. If without conditions, then delete all records in the table.
2. If you want to delete all records
* delete from table name; - not recommended. How many records how many times will delete operations performed
* TRUNCATE TABLE table name; - recommended to use, more efficient to delete the table, and then create a different table. - Modify data:
* Syntax:
* Update table column names set value 1 = 1, 2 column name = value 2, ... [WHERE condition];
* NOTE :
1. If conditions without any, will be all in all records in the table modify.
DQL: record lookup table
- select * from 表名;
-
Syntax:
Selec field list
from table listing
the conditions where the list
group by grouping field
conditions after having packet
order by sorting
limit defined tab -
Underlying query
multiple fields of inquiry 1.
select field name 1, field name 2 ... from table name;
* Note:
* If the query all the fields, you can use * instead of the field list.
2. deduplication:
* DISTINCT
3. computed columns
* using the four arithmetic operations can be generally a series of calculated value. (Usually only for numerical calculation)
* IFNULL (Expression 1, Expression 2): null involved in operation, the results are null
1. Expression 1: which field determines whether the required null
2. If the field replace the value is null.
4. aliases:
* AS: AS can be omitted -
Conditions inquiry
-
where clause followed by the condition
-
Operator
*>, <, <=,> =, =, <>
* the BETWEEN ... the AND
* the IN (set)
* the LIKE: Fuzzy queries
* placeholders:
* _: any single character
*%: any number of characters
* NULL IS
* and or &&
* or or ||
* or not -
Exercise
* Query older than 20 years oldSELECT * 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 '%德%';
-