PHP and MySQL Study Notes 9: creating a Web database 1. basic steps for deploying MySQL services on the server
1) install MySQL (Command installation and installation package installation)
2) check whether an independent operating system user permission is required to run the MySQL program.
3) path settings
4) root password (Appendix A: The system is insecure without the root password)
5) delete anonymous users and databases for testing only
6) start the MySQL server and set it to run automatically.
2. Notes on using MySQL command line or other terminals
1) in the MySQL console command line, each command is separated by a semicolon (;). The semicolon tells MySQL to execute this command. if the semicolon is missing, MySQL will not execute it.
If the semicolon is missing, a new line may be added to a command. Because MySQL provides a continuous symbol, an arrow is similar to->, such
This symbol indicates that MySQL is looking forward to more input. each time you press Enter, these prompts will appear, and it will only end after you Enter a semicolon.
PS: This problem is easy to ignore for new users. I felt very cute at the beginning.
2) SQL statements are case-insensitive, but the names of databases and tables are case-sensitive.
3. log on to MySQL
On the machine command line, call the mysql program
Mysql-h hostname-u username-p
1) run on the local machine. ignore-h hostname.
2)-u is only used to connect to the database user name. If this parameter is not specified, it is only the user name used to log on to the operating system by default.
3) The-p command option is used to tell the server to connect to the server with a password. if the user name used for logon has no password, ignore this option. In addition, we do not need to include a password in this command. the MySQL server will ask you for the password. In fact, it is better not to do so. If you enter a password on the command line, it is displayed in plain text (plaintext) on the screen.
4. basic commands
Create database dbname; create a database
Mysql-D dbname-h hostname-u username-p can also directly specify the database,
Use dbname; you can easily select or switch the current database after logging on.
Show databases; view the database list (you can only view the permissions of the current logon account)
Show tables; View database tables
Describe tablename; view the detailed information of a specific table. The information provided during database creation is displayed.
5. permissions
Note: If you want to create a Web database, it is best to create a separate user for each website application. why? Permission.
Emphasize the "minimum permission principle": a user (or a process) should have the lowest level of permissions to execute the tasks assigned to him.
6. MySQL permission system
1) GRANT and REVOKE commands are used to GRANT and REVOKE permissions of MySQL Users. these permissions are divided into four levels:
Global;
Database;
Table;
Column.
GRANT commands are commonly used to create commands and GRANT corresponding permissions. common forms are:
|
GRANT privileges [columns]
ON item
TO user_name [identified by 'password']
[REQUIRE ssl_options]
[WITH [grant option | limit_options]
|
The clause in square brackets [] is optional. the preceding example contains many placeholders. Several explanations:
> The first placeholder privileges should be a group of permissions separated by commas.
> Columns is optional and used to specify permissions for each column (names of a single column or a group of columns separated by commas ).
> The placeholder item is the database or table to which the new permission applies. If it is specified as *. *, the permission is applied to all databases, which is to grant global permissions. If you do not use a specific database, you can grant global permissions by specifying. It is common to specify all tables in the database in the form of dbname. * and dbname. tablename.
> User_name is a database user. users in different domains can be distinguished by a host name. Ss @ localhost and ss@somewhere.com. you can also use this feature to specify where the user connects to the local machine, or even where the user can access the tables and databases. fine-grained permission control can help improve security.
> The REQUIRE clause allows you to specify whether the user must use encrypted socket links or other SSL options.
> With grant option. if this OPTION is specified, the specified user is allowed to GRANT permissions to others.
You can also specify the WITH clause to specify the number of queries, updates, and connections that each user performs per hour. (These clauses are useful when the shared system limits the load of a single user)
For example: MAX_QUERIES_PER_HOUR n; MAX_UPDATES_PER_HOUR n; MAX_CONNECTIONS_PER_HOUR n;
2) permissions are stored in five system tables named mysql, including mysql. user, mysql. db, mysql. host, mysql. tables_priv, and mysql. columns_priv. (Directly modify these tables and replace them with the GRANT command)
3) permission types and categories
MySQL has three basic types of permissions, which are applicable to general user permissions, administrator permissions, and specific permissions.
In addition to the permissions in Table 9-1, there are two types of permissions REFERENCES and EXECUTE that are no longer in use.
In most cases, you usually only need the common basic functions of adding, deleting, modifying, and querying,
Insert (SELECT), DELETE, UPDATE, SELECT)
For example:
Grant select, insert, delete, update on books. * to bookeditor identified by 'book123456 ';
REVOKE is used for commands corresponding to GRANT. the usage is similar:
REVOKE
|
REVOKE privileges [(columns)]
ON item
FROM user_name
|
If the with grant option is provided, you can revoke it as follows,
Revoke all privileges, GRANT
FROM user_name
Example:
Note: If you enter the quit command to exit the MySQL monitoring program, you are advised to log on as the Web user you just created to test whether everything works normally. If the GRANT statement has been executed (flush privileges should be required), the logon attempt is rejected because the anonymous account is not deleted during installation.
7. create a database table
Create table tablename (columns)
Note: MySQL provides multiple table types and storage engines, including some transaction security types.
Example description:
Note 1: Each MySQL table can have only one primary key, but this primary key can be composed of one field or multiple fields. Generally, text cannot be set as a primary key or indexed (this feature is related to the selected data engine)
Note 2: char (50): This data type will allocate a storage space of 50 characters to the name. Although the name is usually not so long, MySQL will then fill the spare part with spaces, alternatively, you can choose to use the varchar type. This type of storage space can be allocated as needed (plus a byte), which occupies a small amount of space, but the char type data speed is faster.
Note 3: not null. this is a small optimization solution. you need to explain it as follows:
|
Why should I set the mysql field to not null?
Now, I want to explain this to you.
First, we create a table where the asd field is null and the qwe field is not null. no default value exists.
CREATETABLE`tb` ( `id`INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `asd`VARCHAR(45) NULL, `qwe`VARCHAR(45) NOT NULL, PRIMARYKEY (`idtb`))
Then
insert tb(asd) values('123123123'); insert tb(qwe) values('123123123');
Now let's check this table.
We can see that
If the qwe field of not null is not set, it is an empty string.
If the null asd field is not set, it is NULL.
All of the above results can be imagined.
So why should we set not null?
Because MYSQL is not PHP, NULL is not equal to an empty string
For example, if qwe is empty
select * from tb where qwe=''
You can find
However
select * from tb where asd=''
You cannot find anything.
|
8. basic indexing methods
Database performance problems usually occur when no index is created on the database (it is possible to create a table without a primary key or index)
The common format of the create index statement is as follows:
CREATE [UNIQUE | FULLTEXT] INDEX index_name
ON table_name (index_column_name [(length)] [ASC | DESC],...)
Note: FULLTEXT indexes are used to index text Fields. the optional length field allows you to specify that the first length of the field will be indexed. you can also specify an index in ascending or descending order.
9. understand the MySQL identifier
In MySQL, five types of identifiers are provided:
Database, Table, Column, index, and Alias)
Note 1: databases in MySQL will be mapped to directories with certain file structures, while tables will be mapped to files. This has a direct impact on the names assigned to them. it can also affect the case sensitivity of these names, if the operating system is case sensitive to directories and files.
Program)
Note 2: MySQL identifiers cannot contain ASCII (0) (null), ASCII (255) (blank ff), or quotation marks (these three characters)
10. column data type
MySQL has three basic column data types: numbers, dates, times, and strings. Each type contains many sub-classes. The three types of buckets have different buckets. Generally, when selecting the column data type, the basic principle is to select the minimum data type.
Note: For many data types, you can specify the maximum display length when creating a column of this type. in the following table, M is used. The maximum value of M can be 255. if M is optional, put it in square brackets.
1) number type
Numeric types include integer and floating point
Note 1: integer data can be specified as unsigned.
Note 2: The ZEROFILL attribute can be specified for all numeric types. the free part is supplemented by leading 0. If a field is specified as ZEROFILL, it is automatically UNSIGNED.
2) date and time types
MySQL date and time type can be used to input data in string or number format. if you do not set it manually, the TIMESTAMP column in a specific row will be set to the time when the row was last modified, this makes sense for transaction records.
3) string type
The first type is a common string, that is, short text, including CHAR (fixed length) and VARCHAR (variable length.
Note: No matter how large the data size is, Hu searched for the CHAR type columns and used spaces to fill in extra spaces to obtain CHAR type data. MySQL filters out extra spaces when storing VARCHAR type data.
Class 2: TEXT and BLOB
Applicable to long text or binary data respectively. BLOB (binary large objects) is called binary data. it supports any data, such as image and sound data.
Category 3: SET and ENUM
The SET type is used to specify that the values in a column must come from a specified value in a specific SET. it can contain multiple values in the SET. A set of up to 64 elements.
ENUM is an enumeration, similar to SET, but this type of column can only accommodate one of the listed values, or NULL, which can contain a maximum of 65535 elements.
Concluding remarks:
If you ask me why I want to learn this carefully, because I just want to figure it out.