MYSQL study notes
Basic operations of the database
1. Create a database: Crete database database name;
Before creating the database table, use use database name m; and then create the database.
2. Create a database table: create table table name (field name data type constraints);
3. View the data table structure: desc table name; and show create table table name\g
4 .Constraints; primary key constraints primary key
Non-null constraints not null
Unique constraints unique
Default constraints default
Set the auto-increment of attribute values auto_increment
5. Modify the data table; modify the table name Alter table table name Rename new table name
Modify field name Alter table table name Modify field data type data type
Modify data type Alter Table Table name, old field, new field name data type data type
Add field alter table table Name ADD field name Data Type FIRST/AFTER field Name
Delete Field Alter Table Table Drop field Name
Storage engine Alter Table Table Name Engine = Storage Engine Summary: Modify: alter table table name modify etc;
Data type and operator
Data type: string type, numeric type, date and time type
1. Integer type; tinyint smallint mediumint int bigint.
2. Floating point type; float DOUBLE decimal.
3. Date and time type; year time date datetime timestamp.
4. Text string; char varchar tinytxet text mediumtext longtext enum set.
mysql function
abs() sqrt() ceil() ceiling() floor() rand() round()
Query data and insert, update and delete
Create database table: create database database name;
Open the database: use database name;
Create a new data table: create table name (field name data type);
{
Data type: string type, numeric type, date and time type
1. Integer type; tinyint smallint mediumint int bigint.
2. Floating point type; float DOUBLE decimal.
3. Date time type; year time date datetime timestamp.
4. Text string; char varchar tinytxet text mediumtext longtext enum set .
Constraints: primary key primary key
unique primary key unique
non-null constraint not null
default constraint default default value
foreign key constraint foreign key name foreign key (field name) references primary key name Primary key column;
# Modify field name alter table table name change field name new field name data type
Modify data type alter table table name modify field name data type
Add field alter table table name add field name data type (first /after)
Delete Field Alter Table Table Drop field Name
Storage engine Alter Table Table Name Engine = Storage Engine to Delete the outer key to restrain the table name Drop Foreign Key outer key. Field name from table name ( where 1. Field = table name 2. Field;
Left join select field name from Table name 1 left outer join Table name 2 on Table name 1. Field = table name 2. Field;
Right join select field name from Table name 1 right outer join Table name 2 on Table name 1. Field = Table name 2. Field;
* The left join is the field to be queried. The result includes all the fields of the left table
Subquery (nested query) ( any(some)
exists (exists is used together with conditional expressions. If the query statement after exists returns at least one record,
then the exists result is true. At this time, the outer query statement will query, and not exists is the opposite.)
in )
Merge query results select field name from table name query condition union (all) select field name from table name query condition
Note: The number of columns and field names corresponding to the two tables must be equal. Not using all results in deleting duplicate records.
]
}
Insert into data table: insert into table name (field name) values (data to be inserted);
Update data: updata table name set field = value condition;
Delete Data: delete from table name condition;
Index
Advantages: Create a unique index to ensure the uniqueness of each row of data in the database table, greatly speeding up query speed, and in terms of referential integrity of event data, you can Accelerate the connection between tables
Using grouping and sorting clauses for data query can also reduce the time of grouping and sorting in the query.
Ordinary index index (field name): The most basic index type, without uniqueness restrictions, just to speed up access to data
Unique index unique idenx Index name (field) The index value must be unique. However, null values are allowed to reduce the execution time of query summary operations, especially for huge data tables.
Single column index A table can have multiple indexes
Combined index Create an index on multiple fields, follow the leftmost prefix when querying, and the index can only be used when the query matches such a leftmost prefix
Full text index fulltext Can be used for full-text search. Only the MYISAM storage engine supports fulltext index and only char varchar text
Spatial index spatial must be created in a MYISAM type table, and the spatial type field must be empty,
Create an index on an existing table
alter table table name add index index name (field name (index length))
create index index name on table name (field name);
alter table Table name drop index index name;
drop index index name on table name
Create stored procedures and functions
create procedure name of stored procedure () begin stored procedure body end;
Each call will execute the stored procedure body.
Create stored function
create stored function name (parameter list)
returns return value type
return (function body);
Use of variables
Define variables :declare variable name variable type default default value;
Assign value to variable: set variable name = value;
Use of cursor
Declare cursor: declare name cursor for for query statement;
Open Cursor: open name;
Use cursor: fetch name into Save the result queried in the cursor into the parameter;
Close the cursor; close name;
Use of process control (case loop leave iterate repeat while )
Call the stored procedure: call name (parameter);
Call the stored function: select name (parameter);
View
The meaning of the view: A view is a virtual table, which is a table exported from one or more tables in the database.
create [or replace] view view name as select statement
create view view name (self-defined column name) as select Statement
desc view name;
show create view view name\G
show table status like 'view name'\G
Modifying the view is the same as creating the view.
alter view view name as select statement
updata view name set field = value;
delete view drop view if exists view name;
MYSQL trigger Trigger
Like stored procedures, they are both programs embedded in mysql. Triggers trigger an operation based on time. These events include insert update delete
Create trigger
Create only one Trigger for executing the statement
create trigger trigger name identifies the triggering time (before/after) identifies the triggering event (insert update delete) on identifies the table name of the trigger for each trigger execution statement;
This article Explained the related content of mysql, please pay attention to php Chinese website for more content.
Related recommendations:
MySQL database multi-table operation
MySQL database single table query
The above is the detailed content of MYSQL study notes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
