The third phase....

What is a database: a warehouse of data

In the second stage,

1. It is mainly to declare variable storage, the second is to declare arrays, and the third is to declare json objects. All of the above are stored in memory and will disappear as long as they are refreshed

2. It is stored in a file or hard disk, so the storage is safe, but the efficiency of reading the disk is not high, and the disk cannot be well managed

3. It exists in the browser, which is not safe and cannot be cross-browser

Now it exists in the database, solves all the above problems, advantages, and improves data persistence. It is a platform dedicated to managing data. It can add, delete, modify and query data very efficiently. It can be cross-platform, and it is safer. Data can be shared. Perform analysis on the data. There are many ways to store data when writing code. Variables, files, browsers, etc. all have their own shortcomings. We need data persistence, data security, cross-platform, and various operations on data. These are the reasons we use databases

database development

Hierarchical Model -

-Mesh model-

Relational model (relational database) oracle mysql, data is stored in a file similar to excel, a project database has multiple files. Every file has an association.

-Object model (object database (non-relational database)) db2 mongodb

What are common at work

What database to use under what circumstances

The number and data of relational data are relatively stable. big project. However, because the inertial system database not only stores data, but also stores relationships, it takes up a lot of memory.

If the data volume is very large, the query speed needs to be very fast, and the data analysis is very fast, and the object type is used.

database installation

Install the database directly.

Install phpstudy xmapp through the integrated environment...

database operation

/*Multiple lines*/
#Single-line comment
# If the word that comes with the system will become uppercase, the word you use will be lowercase
# Add at the end of a sentence;
#create database create database database name
CREATE DATABASE web329;
#database delete drop database database Name
DROP DATABASE web329;
# Show all databases show databases;
# Use database use database name
# Create this database if the database does not exist
CREATE DATABASE IF NOT EXISTS web329;
# Delete if it exists
DROP DATABASE IF EXISTS web329;

operation table

#Create table
/*create table table name (
   data type of field name column [column constraint],
   field name column data type [column constraint]
)
field name is the column name,
each row is called a data

Data type Mysql has a lot of data types.
    String char varchar
    The length of the statement is equal to the length of the statement. If 4 is written, 2 is used, and the length is still 4.
    Varchar is dynamic. If 4 is written, but only 2 is used, the length is 2.
    If If the length is fixed, use char;
    if it is not fixed, use varchar
    
    number int, integer float,
         floating point number
         
    date
    date yyyy-mm-dd
    datetime yyyy-mm-dd hh-mm-ss
    to suggest that the date use string
    
column constraints to make the data more complete, precise

1. Primary key constraints. A table can only have one primary key feature that is unique and cannot be empty.
    It is necessary to ensure that each piece of data in the database is unique. And meaningful
    primary key
2, self-increment auto_increment
3, unique constraint unique only, can be empty
4, cannot be empty not null cannot be empty
    unique not null unique and cannot be empty 
    primary key is also unique and cannot be empty, one primary key There can only be one table, unique and cannot be empty, multiple
5, default value default
6, foreign key constraints
    The reason why tables and tables are related is because there are foreign key constraints
    Foreign key constraints are to connect a table with a
    table Main table From Table
    1, who is the main data, and who is the main table. The other is from table
    2. When creating a table, create the main table first, and then create the slave table
    3. When deleting, first delete the slave table, and then delete
    the fields associated with the master table and the master table, which is called the foreign key of the foreign key
    from the table. To be consistent with the primary key type of the primary table, the
    foreign key of the secondary table can only be associated with the primary key
    constraint foreign key of the primary table (foreign key of the secondary table) references primary table (primary key)
*/
CREATE TABLE class(
    c_id INT PRIMARY KEY AUTO_INCREMENT,
    c_name VARCHAR(6)
)
CREATE TABLE t_user(
    u_id INT PRIMARY KEY AUTO_INCREMENT,
    u_name VARCHAR(4) NOT NULL,
    u_sex CHAR(1) DEFAULT '女',
    u_time2 DATETIME UNIQUE NOT NULL,
    u_tel CHAR(11)  UNIQUE NOT NULL,
    u_c_id INT,
    CONSTRAINT FOREIGN KEY(u_c_id) REFERENCES class(c_id) 
);

# Delete table
DROP TABLE t_user;

# Modify table
   # Add column alter table table name add column name column data type [column constraint]
   ALTER TABLE t_user ADD u_age INT NOT NULL;
   # modify column alter table table name change old column name new column name data type [column constraints]
   ALTER TABLE t_user CHANGE u_age u_year INT;
   # delete column alter table table name drop column name
   ALTER TABLE t_user DROP u_year;

Operations on data

#Data addition
    /*
        auto value null
        default write default
    
    */

# Add data insert into table (column name) values ​​(value)
INSERT INTO t_user(u_id,u_name,u_tel) VALUES(NULL,'ccc','21223');
#Once the column name is not written, then all subsequent values ​​must be Write, and follow the order
INSERT INTO t_user VALUES(NULL,'ddd',DEFAULT,'2023-04-01 12:12:12','222222',1); #
modify data update table name set column name=value[ Condition]
UPDATE t_user SET u_sex='male';
UPDATE t_user SET u_sex='female' WHERE u_id=1;
# delete data delete from table [condition]
DELETE FROM t_user WHERE u_id=1;
# truncate table table name is deleted There is no record, the deletion of delete is recorded 

Guess you like

Origin blog.csdn.net/m0_62843289/article/details/130800186