Chapter 4 MySQL database. Learning points: 1. web database Overview 2. mySQL operation 3. mySQL common functions 4. SQL statement details 5. phpMyadmin 1. web database Overview now, we are familiar with the basic knowledge of PHP
Learning points:
1. Web database overview
2. MySQL operations
3. MySQL common functions
4. SQL statement details
5. phpMyadmin
1. Web database overview
Now, we are familiar with the basic knowledge of PHP. this is the chapter about leaving PHP for the time being.
Let's look at relational databases to make it easier to understand databases than file storage. These advantages include:
1. relational databases access data faster than normal files.
2. relational databases are easier to view and extract data that meets specific conditions.
3. relational databases have a dedicated built-in mechanism to process concurrent access. as a programmer, you do not need to worry about this.
4. relational databases can provide immediate access to data.
5. relational databases have built-in permission systems.
Concept of relational database
So far, relational databases are the most common database types. They have good theories in relational algebra.
Basic. When using relational databases, you do not need to understand the relational theory (this is a good thing), but you still need
Understand some basic concepts about databases.
1) table
A relational database is composed of relational tables. As the name implies, a link is a data
Table. A workbook is a type of table.
2) columns
Each column in the table has a unique name, containing different data. In addition, each column has a related data class.
Type.
3) rows
Each row in the table represents a customer. Each row has the same format and attributes. Success
Is a record.
4) value
Each row is composed of a single value corresponding to each column. Each value must be of the same type as the data defined in this column.
5) key
The unique identifier of each data entry.
6) mode
The full set of database tables is designed as the database mode.
7) relationship
The foreign key identifies the relationship between two table data.
How to Design Web databases
1) consider the actual object to be modeled.
2) avoid storing redundant data.
3) use the atomic column value (only one data is stored for each attribute of each row .)
4) select a meaningful key.
5) consider the database issue.
6) avoid designing multiple null attributes
Web database architecture
Communication between browsers and Web servers:
Communication between browsers and PHP & MySQL servers
1) the user's Web browser sends an HTTP request to request a specific Web page.
2) the Web server receives a. php request to obtain the file and transmits it to the PHP engine for processing.
3) The PHP engine starts parsing scripts. The script contains a database connection command and a query command.
PHP opens the connection to the MYSQL database and sends the appropriate query.
4) the MYSQL server receives and processes database queries. Return the result to the PHP engine.
5) PHP uses where you are going to run the script. Generally, this includes formatting the query result into HTML format. However
And then output HTML to the Web server.
6) the Web server sends HTML to the browser.
II. MySQL operations
Log on to MySQL
1) open MySQL Command Line Client
2) enter the root password.
MySQL common commands
1) displays the version number and date of the current database.
Select version (), CURRENT_DATE ();
2) set the field name using the AS keyword.
Select version () AS version; // you can set Chinese characters using single quotes.
3) return the calculation result through SELECT execution
SELECT (20 + 5) * 4;
4) implement database users and dates through multiple rows
> SELECT
> USER ()
>,
> NOW ()
>;
5) display database users and dates in one row
> Select user (); select now ();
6) command cancellation
> \ C
7) exit the MySQL window
> Exit;
Common MySQL data types
Integer: TINYINT, SMALLINT, INT, BIGINT
FLOAT, DOUBLE, DECIMAL (M, D)
Character type: CHAR, VARCHAR
DATE type: DATETIME, DATE, TIMESTAMP
Remarks: TINYTEXT, TEXT, LONGTEXT
MySQL database operations
1) display the existing database
> Show databases;
2) select the database you need
> USE guest;
3) view the selected database
> Select database ();
4) view all the contents of a table
> SELECT * FROM guest; // you can use show tables; to check the number of TABLES.
5) set Chinese encoding based on the database
> Set names gbk; // set names utf8;
6) create a database
> Create database book;
7) create a table in the database
> Create table users (
> Username VARCHAR (20), // not null cannot be blank
> Sex CHAR (1 ),
> Birth DATETIME );
8) display the table structure
> DESCIRBE users;
9) insert a data entry to the table
> Insert into users (username, sex, birth) VALUES ('Lil', 'X', NOW ());
10) filter specified data
> SELECT * FROM users WHERE username = 'Lil ';
11) modify the specified data
> UPDATE users SET sex = 'male' WHERE username = 'Lil ';
12) delete the specified data
> Delete from users WHERE username = 'Lil ';
13) sort by specified data
> SELECT * FROM users order by birth DESC; // positive sequence
14) delete the specified table
> Drop table users;
15) delete the specified database
> Drop database book;
III. MySQL common functions
IV. SQL statement details
1. create a class Database school, which contains a class table grade, including the number (id), name (name ),
Email, score, and registration date ).
Mysql> create database school; // CREATE a DATABASE
Mysql> create table grade (
// UNSIGNED indicates UNSIGNED, TINYINT (2) UNSIGNED integer 0-99, not null indicates NOT
Null. AUTO_INCREMENT indicates that no field is added from 1, accumulating one
-> Id TINYINT (2) unsigned not null AUTO_INCREMENT,
-> Name VARCHAR (20) not null,
-> Email VARCHAR (40 ),
-> Point TINYINT (3) unsigned not null,
-> Regdate datetime not null,
-> Primary key (id) // indicates that id is the primary key and the id value must be unique.
-> );
2. add 5-10 student records to the grade table.
Mysql> insert into grade (name, email, point, regdate) VALUES
('Lil', 'y60. com@gmail.com ', 95, NOW ());
3. view the records of all fields in the class, and view the records of class id, name, and email.
Mysql> SELECT * FROM grade;
Mysql> SELECT id, name, email FROM grade;
4. for a student whose name is equal to 'Lil', if the score is greater than 90, the email is not an empty member, but a member between 70 and 90.
Mysql> SELECT * FROM grade WHERE name = 'Lil ';
Mysql> SELECT * FROM grade WHERE point> 90;
Mysql> SELECT * FROM grade WHERE email is not null;
Mysql> SELECT * FROM grade WHERE point BETWEEN 70 AND 90;
Mysql> SELECT * FROM grade WHERE point IN (, 82, 78 );
5. search for students who use 163 of emails and do not contain yc60.com strings
Mysql> SELECT * FROM grade WHERE email LIKE '% 163.com ';
Mysql> SELECT * FROM grade WHERE email not like '% yc60.com % ';
6. Sort by student registration date in descending order and by score in positive order
Mysql> SELECT * FROM grade order by regdate DESC;
Mysql> SELECT * FROM grade order by point ASC;
7. only the data of the first three students is displayed, and 2 from 3rd
Mysql> SELECT * FROM grade LIMIT 3;
Mysql> SELECT * FROM grade LIMIT 2, 2;
8. email with the name changed to 'Lee'
Mysql> UPDATE grade SET email = 'yc4. com@163.com 'WHERE name = 'Lil ';
9. delete student data numbered 4
Mysql> delete from grade WHERE id = 4;
10. grouping functions over once
.
11. check the table information.
Mysql> show table status \ G;
12. optimize a table
Mysql> optimize table grade;
V. PhpMyAdmin
PhpMyAdmin (PMA for short) is written in PHP and can be controlled and operated online over the Internet.
MySQL. He is the preferred database maintenance tool for many MySQL administrators and website administrators.
You can perform operations on the MySQL database.
Create a database scholl
Create a database-> select the utf8 character set
Export another Database SQL
1. select another database-> Export
2. select the table to be exported-> Select All
3. select Add drop table/drop view (once the basic TABLE is deleted, the data in the TABLE and the corresponding creation
The indexes and views of are automatically deleted)
4. select save as file
5. select run to save the SQL file.
Import database
1. select the database to be imported
2. Select Import and select the SQL file
3. run the command.
Recreate a table
1. find the table creation statement output in the SQL file.
2. copy the table creation statement
3. select SQL, paste, and execute.
Repair Data tables
1. select the table to be repaired
2. select repair table from the selected items.
Optimize Data tables
1. select the table to optimize
2. in the selected item, select the optimize table.
Modify, delete, and insert table records
Execute SQL statements
Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.
Overview 1.Web Database Overview 2. MySQL operations 3. Explanation of MySQL common functions 4. SQL statements 5. phpMyadmin 1. Overview of Web database now, we are familiar with the basic knowledge of PHP...