Home >Backend Development >PHP Tutorial >PHP introductory learning - database learning
1. Introduction to database
(1) Database is a warehouse where data is stored. Data is not placed directly in the database. Tables are placed in the database, and data is stored in the tables;
(2) Development history of database
Embryonic stage - —File system: Use disk files to store data;
Initial stage - first generation database: Network model and hierarchical model database appeared;
Intermediate stage - second generation database: relational database and structured query language ;
Advanced stage - new generation database: "relational-object" database;
(3) Hierarchical model: The hierarchical data model simulates various hierarchical organizations in real life, and the resource manager can be considered to be based on the hierarchical model Organized;
Advantages: Classified management, it is very convenient if you query the same type of data;
Disadvantages: If you query a lot of data that is not the same type, the efficiency is very low;
(4) Relational model; between records Links are made through the relationship between attributes to ensure data independence and form relationships between data sets;
In the relational model, each table is independent and relationships are established through public fields;
Relationship: two tables The public field is called a relationship;
Advantages: The tables are independent, and any data needed can be queried in which table;
Disadvantages: Multi-table query, low efficiency;
2. Introduction to SQL statements
(1) Structured Query Language Structured Query Language is used to operate relational databases;
(2) Commonly used relational databases: access, MySQL, SQL server, Oracle;
Standard SQL is an operation statement supported by all relational databases. Standard SQL is also called SQL- 92, but each database extends its own things based on standard SQL;
SQL server extended SQL statement: T-SQL;
Oracle extended SQL statement: PL/SQL;
MySQL extended SQL statement: MySQL;
Three , Connect to the database
(1) After installing the database, MySQL comes with a MySQL command line client. This client is very convenient, but it can only connect to local MySQL;
Connect to the server through the windows command line;
(2) dos command:
Enter other disks: disk name: ;
Enter a folder under the drive letter: cd path;
Enter the upper-level directory of the current path: cd ../;
Enter the root directory: cd /;
Four, database operations
Database It is essentially a file. Multiple databases can be created and managed through MySQL;
(1) Create a database:
Syntax: create database database name;
If you create an existing database, an error will be reported; check when creating, if it does not exist Just create;
Syntax: create database if not exists database name;
If the created database name is a keyword, an error will be reported; adding backticks to the name can solve the problem;
When creating a database, specify the characters of the database Encoding, syntax: create database database name charset=utf8;
(2) Query database:
Syntax: show database database name;
(3) Change database:
Change the character encoding of the database, syntax: alter database database name charset=utf8 ;
(4) Delete database:
Syntax: drop database database name;
When deleting a database, determine whether the database exists, and delete it if it exists;
Syntax: drop database if exists database name;
(5) Select database:
Syntax: use database name;
(6) Show all databases:
Syntax: show databases;
(7) Display database creation statement:
Syntax: show create database database name;
5. The concept of tables
(1 ) A row is also called a record, and a row is a record;
(2) A column is also called a field, and a column is a field, and a field is also called an attribute;
(3) A table contains multiple fields;
(4) Create a table, syntax: create table data table name (field 1 data type);
Characteristics of primary key: cannot be repeated, cannot be empty, a table can only have one primary key, and the primary key can be composed of multiple fields together;
Data type: int, decimal (total digits, decimal places), char() fixed length, varchar() variable length, text large text,
6. Create tables
(1) View all tables
Syntax: show tables;
(2) View creation Table SQL statement
Syntax: show create table data table name;
(3) Show table structure
Syntax: describe data table name;
(4) Delete table
Syntax: drop table table 1, table 2, table 3;
( 5) Create complex tables
Syntax:
7. Data operations
(1) Insert data
Syntax: inset into table name (field name 1, field name 2) values (value 1, value 2);
Insert fields can be and The order of the fields in the database is inconsistent, but the value and the order of the inserted fields must be consistent;
The inserted fields can be omitted, and the inserted values are consistent with the order and number of fields in the data table;
Syntax: inset into table name values (value 1, value 2, value 3...);
Insertion of automatic growth: inset into table name values (null, value 2, value 3...);
Insert of default value: insert into table name values ( null, value 2, value 3, default ...);
(2) Modify data
Syntax: update table name set field 1 = value 1, field 2 = value 2 where condition;
(3) Delete data
Syntax: delete from table name [where condition];
(4) Query data
Syntax: select * from table name;
Syntax: select column name from table name [where condition] [order by sort] [limit limit];
(5) operator
The above introduces the introductory learning of PHP - database learning, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.