week 10
1 Introduction
1.1 Database
A database is a warehouse that organizes, stores and manages data according to the data structure.
1.2 Relational database management system
Features of RDBMS (Relational Database Management System):
- The data comes in the form of a table
- Various record names for each row
- Each column corresponds to the data field of the record name
- Many rows and columns form a form
- Several forms form a database
Some terms for RDBMS:
- Database: A database is a collection of related tables. .
- Data table: A table is a matrix of data. A table in a database looks like a simple spreadsheet.
- Column: A column (data element) that contains the same data, such as zip code data.
- Row: A row (=tuple, or record) is a group of related data, such as a piece of data subscribed by a user.
- Redundancy : store twice the data, redundancy can make the system faster. (The higher the degree of normalization of the table, the more the relationship between the table and the table; the query may often require a join query between multiple tables; and the join operation will slow down the query speed. For example, student information is stored in In the student table, the information of the department is stored in the department table. A relationship is established with the department table through the dept_id field in the student table. If you want to query the name of a student's department, you must find the student's department number in the student table ( dept_id), and then go to the department to find the name of the department according to this number. If this operation is often required, the connection query will waste a lot of time. Therefore, a redundant field dept_name can be added to the student table, which is used to store the student's location. The name of the department. This saves you having to connect every time.)
- Primary key : The primary key is unique. A data table can only contain one primary key. You can use the primary key to query data.
- Foreign Key: A foreign key is used to relate two tables.
- Compound key : A compound key (composite key) uses multiple columns as an index key and is generally used for compound indexes.
- Indexes: Use indexes to quickly access specific information in database tables. An index is a structure that sorts the values of one or more columns in a database table. A catalogue similar to a book.
- Referential Integrity: Referential integrity requires that no reference to a non-existent entity be allowed in a relationship. Entity integrity is an integrity constraint that a relational model must satisfy, in order to ensure data consistency.
1.3 Mysql database
Mysql is the most popular relational database management system, and MySQL is one of the best RDBMS (Relational Database Management System: relational database management system) application software in WEB application. Other RDMSs include: Oracle, Sql Server, DB2, Postresql, Sqlite, Access, etc. Non-relational: mongodb, redis, memcache.
2 Mysql installation
2.1 Linux
yum -y install mysql-server mysql
1. Unzip the tar package cd /software tar -xzvf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.21-linux-glibc2.5-x86_64 mysql-5.6.21 2. Add users and groups groupadd mysql useradd -r -g mysql mysql chown -R mysql:mysql mysql-5.6.21 3. Install the database on mysql cd mysql-5.6.21/scripts ./mysql_install_db --user=mysql --basedir=/software/mysql-5.6.21 --datadir=/software/mysql-5.6.21/data 4. Configuration file cd /software/mysql-5.6.21/support-files cp my-default.cnf /etc/my.cnf cp mysql.server /etc/init.d/mysql vim /etc/init.d/mysql #If the installation directory of mysql is /usr/local/mysql, you can omit this step to modify the two changed values in the file basedir=/software/mysql-5.6.21 datadir=/software/mysql-5.6.21/data 5. Configure environment variables vim /etc/profile export MYSQL_HOME="/software/mysql-5.6.21" export PATH="$PATH:$MYSQL_HOME/bin" source /etc/profile 6. Add a self-starting service chkconfig --add mysql chkconfig mysql on 7. start mysql service mysql start 8. Log in to mysql, change password and configure remote access mysqladmin -u root password ' your_password ' #Modify the root user password mysql -u root -p #Log in to mysql, you need to enter the password mysql>GRANT ALL PRIVILEGES ON *.* TO ' root ' @ ' % ' IDENTIFIED BY ' your_password ' WITH GRANT OPTION; #Allow root user to remotely access mysql>FLUSH PRIVILEGES; #Refresh permissions
2.2 Window
# 1. Download: MySQL Community Server 5.7.16 http://dev.mysql.com/downloads/mysql/ # 2. Decompression If you want to install MySQL in the specified directory, then move the decompressed folder to the specified directory, such as: C:\mysql-5.7.16- winx64 # 3. Add an environment variable [Right-click Computer]--"[Properties]--"[Advanced System Settings]--"[Advanced]--"[Environment Variables]--"[Find the variable in the second content box A line named Path, double-click] --> [Append the MySQL bin directory path to the variable value, use; split] # 4. Initialize mysqld --initialize- insecure # 5. Start MySQL service mysqld #Start MySQL service # 6. Start the MySQL client and connect to the MySQL service mysql -u root -p #Connect to the MySQL server The previous step solved some problems, but it was not thorough enough, because when executing [mysqd] to start the MySQL server, the current terminal will be hanged, so this problem can be solved by setting: Note: -- Before installing, you must use the absolute path of the mysql startup command # to create a Windows service for MySQL, and execute this command in the terminal: " c:\mysql-5.7.16-winx64\bin\mysqld " -- install #Remove the Windows service of MySQL, execute this command in the terminal: " c:\mysql-5.7.16-winx64\bin\mysqld " -- remove After registering as a service, you only need to execute the following commands when you start and close the MySQL service later: #Start MySQL service net start mysql #Close MySQL service net stop mysql
3 Mysql data types
MySQL supports a variety of types, which can be roughly divided into three categories: numeric, date/time, and string (character) types.
3.1 Numeric types
Types of | size | range (signed) | range (unsigned) | use |
---|---|---|---|---|
TINYINT | 1 byte | (-128,127) | (0,255) | small integer value |
SMALLINT | 2 bytes | (-32 768,32 767) | (0,65 535) | large integer value |
MEDIUMINT | 3 bytes | (-8 388 608,8 388 607) | (0,16 777 215) | large integer value |
INT or INTEGER | 4 bytes | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | large integer value |
BIGINT | 8 bytes | (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | very large integer value |
FLOAT | 4 bytes | (-3.402 823 466 E + 38,1.175 494 351 E-38) , 0 , (1.175 494 351 E-38,3.402 823 466 351 E + 38) | 0 , (1,175 494 351 E-38,3,402 823 466 E + 38) | single-precision floating-point value |
DOUBLE | 8 bytes | (1.797 693 134 862 315 7 E + 308,2.225 073 858 507 201 4 E-308) , 0 , (2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) | 0 , (2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E + 308) | Double-precision floating-point value |
DECIMAL | For DECIMAL(M,D), if M>D, it is M+2 else it is D+2 | depends on the values of M and D | depends on the values of M and D | decimal value |
3.2