Basic knowledge of PHP learning Day9---Database

Source: Internet
Author: User
Tags most popular database name database

Today I mainly learned the basic knowledge of MySQL.

First, the introduction of the database 1. Classification of databases

   To learn the database we first need to understand what is a database, the database is similar to a warehouse, can store a large number of different data, DBMS system, full name database management system is a data management systems, is a management software, The computer that installs the database software is the database server, the port defaults to 3306, the database server, provides the data update, the management, the security affairs and so on aspect function.

database According to the structure of the different can be divided into three kinds: hierarchical database, Mesh database, relational database. Of course, the most popular database today is the relational database.

Concepts related to several databases:

Data:

Database: Databases that are used to store data similar to a large folder.

DBMS: Database management systems, such as mysql,oracle, etc.

Table: A data table for storing data, owning rows and columns. Similar to files in a file.

Row: Record, a record in a data table, which is a row of data.

Field: A column of data in a column data table.

2. The mainstream database

      The most popular databases currently on the market are:

Access: A small database of Microsoft

Sqlsever: Microsoft's Midsize database

Oracle: Oracle's products in the United States, large or very large.

MySQL: Swedish AB Company's products, which were later acquired by Oracle, Small and medium Web applications, open source.

SQL: This is the standard language for relational databases, struct query LANGUAGE (Structured Query language)

The characteristic of relational database is that the data between table and table can establish a certain "correspondence relation". Such as:

          

3. Basic operating procedures of the database

     First, establish a connection to the database.

Second, the client sends SQL commands to the server, executes the command on the server side, and returns the execution results

Finally, the client accepts that the results are displayed and disconnected.

Ii. Basic syntax of the database  1. Start the database

How to start a database

    In the command line mode:

Startup: net start MySQL

          Close: net start MySQL

Such as:

      Note, of course, that you need to configure your own environment variables if you want to use them on the global path.

Service mode: Control Panel-"management tool-" service-"mysql-" Stop/start

2. Exit/Login Database

   Log in to the database:

         Mysql-h server address-u logon Name----Login password;

          MySQL--host= server address--user= user name--port= port--password

Quit: quit; exit;

   Note: Be sure to use "Set names GBK" to set the environment encoding of the current connected database after logging in to the database, that is, the encoding of the client that is currently dealing with the database.

3. Backup and recovery of databases

    Backup: A database is transformed into a file that can be carried and transmitted at any time.

How to back up:

         Mysqldump-h server address-u login name-p database name > file name

      

  Restore backup: Restore data after a backup to the database

        Mysql-h Server address -u login name - p database name < file name

      

Note: Both commands are used when not logged in to MySQL, where the mysqldump command also needs to be opened as an administrator, usually recovery, which means restoring all the table data and other information in the original database, indicating that it can be a new name.

4. Basic grammar Rules

    Comments:

There are three types of annotations:

Single-line Comment: # comment Content

Single-line Comment:--Comment content (--there is a space after it)

Multiline Comment:/* Comment content */

Statement line:

By default, an English semicolon is used as the end of a statement, and in a regular operation, one statement is executed at a time.

Case:

The MySQL language back itself is case-insensitive;

Some MySQL command execution will generate a folder, which may be case-sensitive at this time;

Naming problems:

Names that can be named by themselves are called identifiers.

Naming suggestions are the same as PHP, alphanumeric underscores, and do not start with numbers.

      A non-regular character or system keyword can be used as an identifier, but it is best to wrap it in an inverted quotation mark (the number 1 to the left of the backslash ') and is not recommended.

III. database Definition statements 1. Create a database

      After connecting to the database server, we need to create the database, of course, the database creation syntax is as follows:

        Create DATABASE db_name [CharSet UT8];

        

2. Deleting a database

   Since we can create a database, we can also delete the database, the syntax to delete the database is:

     Drop database "if exists" db_name;

        

3. Modify the Database

      Usually we modify the database to modify only two items:

Modify the code;

Modify the order of arrangement;

Grammar:

        Alter database name charset new encoding name collate new collation name

      

4. Enter the database

     Grammar:

      Use database name

Iv. basic functions of PHP Operational database

To complete the operation of the relevant database in PHP, first execute the following code:

$link = mysql_connect ("Database server address", "User name", "password");

mysql_query ("Set names UTF8");//can also use Mysql_set_charset ("");

mysql_query ("Use database name");//mysql_seleck_db ("") may also be used;

  Then, you begin to formally execute the Database Operation task (statement) that you want to complete:

$result = mysql_query ("Select/delete/update/insert/desc/show tables/drop ...");

   Add:

      Mysql_error (); Gets the failure information when the error is executed

Mysql_query ();

For the mysql_query () function, you can execute almost any SQL statement, which can be divided into two situations when executing a statement.

1. The execution returns all Boolean values: (The return result is a Boolean value) such as:

     Insert, UPDATE, delete, CREATE TABLE, CREATE DATABASE, drop ... Execution successfully returns TRUE, execution failure returns false;

2. Data is returned on execution: (execution failure returns false, result set is returned successfully ) ) such as:

      $result = mysql_query ("Select ... “);

while ($rec = Mysql_fetch_array ($result)) {

Mysql_fetch_array () takes out the "row of data" in the result set and assigns a value to the $REC after obtaining the data of that row ;

this $rec is an array whose subscript is the field name;

in this while Loop,mysql_fetch_array () takes out all the data in the result set over and over (one row).

then, you can handle the array $rec here :

}      

    $n 1 = mysql_num_rows ( result set ); gets the number of data rows for the result set;

   $n 2 = mysql_num_fields ( result set ); gets the number of data columns for the result set;

    $name = mysql_field_name ( result set , $i); Get the name of the first field of the result set ! I count from 0 onwards.

Continue tomorrow.

Basic knowledge of PHP learning Day9---Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.