Mariadb learning summary (4): data insertion, deletion and modification
INSERT statement format:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_list)] [(col,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col=expr [, col=expr] ... ]
The table structure is as follows:
MariaDB [mydb]> DESC user; +----------+-------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(10) | NO | | NULL | | | password | varchar(10) | NO | | NULL | | | regtime | timestamp | NO | | CURRENT_TIMESTAMP | | | logtime | timestamp | NO | | 0000-00-00 00:00:00 | | | logip | varchar(20) | YES | | NULL | | +----------+-------------+------+-----+---------------------+----------------+ 6 rows in set (0.01 sec)
MariaDB [mydb]> INSERT INTO user VALUES(1,'test','test',NOW(),NOW(),'127.0.0.1');
Because in many cases some values are default, so we can specify which columns to insert data for, while other columns use default values, as follows:
MariaDB [mydb]> INSERT INTO user(username,password) VALUES('test2','test2');
Similarly, inserting specific column data can also be written like this:
MariaDB [mydb]> INSERT INTO user SET username='test3',password='test3';
This will only insert the username and password, and use the default values for other values.
MariaDB [mydb]> SELECT * FROM user; +----+----------+----------+---------------------+---------------------+-----------+ | id | username | password | regtime | logtime | logip | +----+----------+----------+---------------------+---------------------+-----------+ | 1 | test | test | 2018-02-24 15:43:41 | 2018-02-24 15:43:41 | 127.0.0.1 | | 2 | test2 | test2 | 2018-02-24 15:45:16 | 0000-00-00 00:00:00 | NULL | | 3 | test3 | test3 | 2018-02-24 15:46:56 | 0000-00-00 00:00:00 | NULL | +----+----------+----------+---------------------+---------------------+-----------+ 3 rows in set (0.00 sec)
Many times we have the need to use one INSERT statement to insert multiple records into the data table. We can write it like this:
MariaDB [mydb]> INSERT INTO user(username,password) VALUES('test4','test4'),('test5', -> 'test5');
When the storage engine used (MyISAM, MEMORY, MERGE) uses table-level locks, you can use the two keywords LOW_PRIORITY | HIGH_PRIORITY:
When using the LOW_PRIORITY keyword, data is written when no client reads the table again.
When HIGH_PRIORITY is used, the INSERT statement has the same priority as the SELECT statement. (default policy)
So, when a SELECT statement is executed before the INSERT statement is executed, the INSERT blocks and waits for the SELECT to be read, but at this time, if a SELECT enters the schedule again, the SELECT is blocked (the read lock can be read directly), but at this time because The INSERT statement has the same priority as the SELECT statement, so SELECT cannot be executed until the INSERT is completed, so INSERT can add LOW_PRIORITY to optimize the reading speed.
Read it slowly here, I don’t know much about locks.
UPDATE statement syntax is as follows:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference [PARTITION (partition_list)] SET col1={expr1|DEFAULT} [,col2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
When the WHERE clause is not used to constrain the selection conditions, all data is updated. For example, the login time of all records in the user table is modified to be now:
MariaDB [mydb]> UPDATE user SET logtime=NOW(); Query OK, 5 rows affected (0.01 sec) Rows matched: 5 Changed: 5 Warnings: 0
MariaDB [mydb]> UPDATE user SET logip='127.0.0.1' ORDER BY regtime LIMIT 3;
The ORDER BY statement can be used in SELECT UPDATE DELETE, etc., to indicate which field arrangement of the table should be followed when outputting, deleting, and updating the table.
For example, in the above, ORDER BY regtime is to sort and update in order according to the registration time, and only the first three rows are updated with the LIMIT statement.
Use DESC to specify the flashback arrangement, for example: ORDER BY regtime DESC
LIMIT statement: used to limit the number of query results.
Usage:
LIMIT[位置偏移量,]行数
The first line starts at 0, so the following:
SELECT * FROM user LIMIT 2,2; //从第3行开始,取两行,即取第3、4条记录。
MariaDB [mydb]> UPDATE user SET logip='192.168.1.2' WHERE username='test2';
Because there are many things in the WHERE clause, I won’t post too much content here.
LOW_PRIORITY: This is the same as LOW_PRIORITY of INSERT.
MariaDB [mydb]> REPLACE INTO user VALUES(1,'test111','test111',NOW(),NOW(),'192.168.1.1');
The above statement is MariaDB's extended SQL, which is equivalent to deleting duplicate (primary key or unique index) records and adding new records.
It seems a bit tasteless? ....
DELETE语语法:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [PARTITION (partition_list)] [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] [RETURNING select_expr [, select_expr ...]]
不使用WHERE语句来约束条件时,则删除所有数据,如下:
MariaDB [mydb]> DELETE FROM user;
MariaDB [mydb]> DELETE FROM user WHERE username='test1';
那么?这篇文章就简单写到这吧~~
The above is the detailed content of Mariadb learning summary (4): data insertion, deletion and modification. 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

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
