Simple knowledge points of MySQL database


1. Overview of MySQL
1. What is a database? A
database is a warehouse for storing data.
2. Which companies are using databases?
Financial institutions, game websites, shopping websites, forum websites...
3. Software that provides database services
1 . , Software classification
MySQL, SQL_Server, Oracle, Mariadb, DB2, MongoDB...
2. In the production environment, how to choose which database to use
1. Whether it is open source Open
source software: MySQL, Mariadb, MongoDB
Commercial software: Oracle, DB2, SQL_Server
2 、Whether it is cross-platform
Not cross-platform:
SQL_Server Cross-platform: MySQL, Mariadb, MongoDB, DB2, Oracle
3, type of company
Commercial software: government departments, financial institutions
Open source software: game websites, shopping websites, forum websites...
4. Features of MySQL 1.
Relational database
1. Features of relational database 1.
Data is stored in the form of rows and columns
2. This series of rows and columns is called table
3. Each row in the table It is called a record
4. Each column in the table is called a field
. 5. The logical association between the table and the table is called a relationship
. 2. Cross-platform
Can run database services on Unix, Linux, Windows
3, support multiple programming languages
​​Python, java, php...
2, MySQL installation
1, Ubuntu install MySQL service RedHat (Red Hat), CentOS, Ubuntu
1 , install the server
sudo apt-get install mysql-server
2, install the client
sudo apt-get install mysql-client
2, install the MySQL service on Windows
1, download the MySQL installation package (windows)
mysql-installer***5.7.** *.msi
2. Double-click and install it according to the tutorial
3. Start and connect MySQL service
1. Start the server
1. Check MySQL service status
sudo /etc/init.d/mysql status
2. Stop, start, restart MySQL service
sudo /etc/init.d/mysql stop | start | restart
2. Client connection
1. Command format
mysql -h host address -u username -p password
mysql -hlocalhost -uroot -p password
2. Local connection can be omitted -h Options
mysql -u username -p password
4. Basic SQL commands
1. Rules for using SQL commands
1. Each SQL command must end with ;
2. SQL commands are not case sensitive
3. Use \c to terminate the execution of the command ( ctrl + c in Linux) 2. Library management 1.
Basic
operations
of the library The statement of the library show create database library name; 4. View the current library select database(); 5. Switch the library use library name; 6. View the existing tables in the library show tables; 7. Delete the library drop database library name; 2. Naming rules for library names 1. Numbers, letters, and _ can be used, but pure numbers cannot be used . 2. Library names are case-sensitive . 3. Library names are unique. 4. Special characters and MySQL keywords cannot be used . 3. Table management 1. Basic operations of tables 1. Create a table (specify character set)





















create table table name (
field name data type,
field name data type,
... ...
);
2. View the statement to create the table (character set)
show create table table name;
3. View the table structure
desc table name;
4 , delete the table
drop table table name;
2, the naming rules of the table (same library naming rules)
3, exercise
1, create a library python
create database python;
2, create a table py_mysql in the library python, specify the character set utf8
table fields There are two fields of id int and name char(20)
use python;
create table py_mysql(
id int,
name char(20)
)default charset=utf8;
3. View the character set of table py_mysql and storage engine
show create table py_mysql;
4 , View the table structure of
py_mysql desc py_mysql;
5. Delete table py_mysql
drop table py_mysql;
4. Note
1. All data is stored in the database directory in the form of files
2. Database directory: /var/lib/mysql
5. Change the default character set of the library and table
1. Method
By changing the configuration file of the MySQL service
2. Step
1. Obtain root privilege
sudo -i
2. Switch to the path where the configuration file is located cd
/etc/mysql/mysql.conf.d
3. Backup
cp -p mysqld.cnf mysqld.cnf.bak 
(-p option Copy along with the original file permissions)
4. Open mysqld.cnf with vi
vi mysqld.cnf
[mysqld]
character_set_server = utf8 save and exit
a -> write -> ESC -> shift + : -> wq
5. Restart mysql service
/etc /init.d/mysql restart | reload (reload configuration file)
6. Exit superuser exit
7. Log in to mysql to verify: mysql -uroot -p123456
                   create database library name;
show create database library name;
6. Table record management
1. Insert records into the
table 1. insert into table name values ​​(value 1), (value 2),..., (value N);
2. insert into table name (field name list) values (value 1),...(value N);
2. Query table record
1, select * from table name;
2, select field 1, field name 2,..., field name N from table name;
7. Customer The process of storing data on the database server
1. Connect to the database server: mysql -uroot -p enter the password
2. Select a library: use library name;
3. Create a table or modify a table: create ...
4. Disconnect Connection with database server: exit | quit | \q
8. Introduction to terms
1. DB (database)
DB is a database, a warehouse for storing data
2. DBMS (database management system)
database management system Software for
managing databases: MySQL, Oracle, MongoDB...
3. DBS (database system)
database system
DBS = DB (storage) + DBMS (database software) + database application (financial management system, personnel management system) + user
9, data type
1. Numerical type (signed signed and unsigned unsigned)
1. Integer
1, int large integer (4 bytes) 
Value range: 0 ~ 2 ** 32 -1 (more than 4.2 billion)
2. tinyint tiny Integer (1 byte)
1. Signed (signed default): -128 ~ 127
2. Unsigned (unsigned): 0 ~ 255
3. smallint Small integer (2 bytes)
Value range: 0 ~ 65535 4. The value range
of bigint is a very large integer (8 bytes) : 0 ~ 2 ** 64 - 1 2. Floating point type 1, float (4 bytes, up to 7 significant digits) 1. Usage Field name float(m,n) m: total number of digits n: decimal digits salary float(5,2) Value range? -999.99 ~ 999.99 2. Note 1. When the floating-point type is inserted into an integer, the number of decimal places will be automatically completed. 2. If the number of decimal places is more than the specified number of digits, the next digit of the specified digit will be rounded float(5,2 ) -> 23.128 -> 23.13 2. double (8 bytes, display up to 15 significant digits) 1. Usage Field name double(m,n) 3. decimal (M+2 bytes, display up to 28 valid digits) bit) 1. Usage















decimal(28,5)
2. Character type
1, char (fixed length)
1. Value range: 1 ~ 255
2. If no width is given, the default is 1 
2. varchar (variable length)
1. Value range: 1 ~ 65535
2. Note
1. varchar does not have a default width, and a width value must be given.
name varchar(20)
3. Characteristics of char and varchar
1. Char
wastes storage space, but has high performance
2. Varchar
saves storage space, but low performance
3 , enumeration type
1, definition
field value can only be selected within the range of enumeration
2, enum(...) radio selection (up to 65535 different values)
field name enum (value 1, value 2,..., Value N)
3. set(...) Multiple selection (up to 64 different values)
field name set (value 1, value 2,..., value N)
"Python,boy,Mysql" when inserting a record
4 , datetime type
1, year: year YYYY 
2, date: date YYYYMMDD
3, time: time HHMMSS
4, datetime: date time YYYYMMDDHHMMSS
5. timestamp: date and time YYYYMMDDHHMMSS
6. Note
1. When inserting a record, the datetime field does not give a value and returns NULL by default
. 2. When inserting a record, the timestamp field does not give a value and returns the current system time by default.
10. Operation of table fields
1. Syntax: alter table
1. Add field
alter table table name add field name data type first | after field name; 2.
Delete field
alter table table name drop field name;
3. Modify field data type
alter table table name modify field name new Data type;
# When modifying the data type, it will be limited by the original data in the table
4. Modify the field name
alter table table name change old name new name data type;
5. Modify the table name
alter table table name rename new table name;
relational database The core content is the relationship, that is, the two-dimensional table














































































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325347109&siteId=291194637