Table of Contents
       一插入记录" >       一插入记录
       (1)INSERT命令" >       (1)INSERT命令
       1)省略所有字段的情况" >       1)省略所有字段的情况
        2)插入指定字段的情况:" >        2)插入指定字段的情况:
        3)插入多条记录的情况:" >        3)插入多条记录的情况:
       (2)INSERT SET命令" >       (2)INSERT SET命令
       (3)INSERT SELECT命令(超纲)" >       (3)INSERT SELECT命令(超纲)
       二更新记录" >       二更新记录
       (1)省略关键字WHERE的情况" >       (1)省略关键字WHERE的情况
       1)更新一列:" >       1)更新一列:
       2)更新多列:" >       2)更新多列:
       (2)有关键字WHERE下指定几条记录更新的情况" >       (2)有关键字WHERE下指定几条记录更新的情况
       三删除记录" >       三删除记录
Home Database Mysql Tutorial MySQL学习13:操作数据表中的记录(一)

MySQL学习13:操作数据表中的记录(一)

Jun 07, 2016 pm 02:50 PM
mysql study operate data sheet Record

操作MySQL数据库中的数据表的记录包括:记录的插入、记录的修改、记录的删除和记录的查询。简单来说、就 是数据表中的记录的增删改查。 一插入记录 MySQL数据库中的数据表的记录的插入包括三种形式,在前面我们操作数据表的时候就已经使用过记录的插入。 那

       操作MySQL数据库中的数据表的记录包括:记录的插入、记录的修改、记录的删除和记录的查询。简单来说、就

是数据表中的记录的增删改查。

       一插入记录

       MySQL数据库中的数据表的记录的插入包括三种形式,在前面我们操作数据表的时候就已经使用过记录的插入。

那只是我们最常使用的一种方式而已,接下来我们来看看记录的三种插入方式:

       (1)INSERT命令

       MySQL数据库中的数据表中插入记录的第一种语法格式,也是最常用的一种语法格式为:

       INSERT [INTO] table_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),...;

       例子:

       我们在向数据表中写入记录的同时,首先先来创建一张数据表,并来查看所有字段:

       CREATE TABLE users3(

           id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

           username VARCHAR(20) NOT NULL,

           password VARCHAR(32) NOT NULL,

           age  TINYINT UNSIGNED NOT NULL DEFAULT 10,

           sex BOOLEAN

       );

       DESC users3;


       1)省略所有字段的情况

       下面记录的插入列出了插入字段的各种情况:

       INSERT users3 VALUES(NULL,'Tom','123',25,1);

       INSERT users3 VALUES(NULL,'John','223',DEFAULT,0);

       INSERT users3 VALUES(DEFAULT,'Rose','323',25,1);

       INSERT users3 VALUES(DEFAULT,'Paul','123',23-5+1,1);

       SELECT * FROM users3;


       INSERT users3 VALUES(DEFAULT,'Tom','123',25);


       上面的出错情况表明插入的记录与列的个数不匹配,因此如果我们做记录插入时省略所有字段名称的情况下,要

依次插入与列数相同个数的值,否则记录的写入不成功,向数据表中写记录的时候切记这一点。

        2)插入指定字段的情况:

        INSERT users3(username,password,age) VALUES('Kobe','111',23);

        SELECT * FROM users3;


        3)插入多条记录的情况:

        INSERT users3 VALUES(NULL,'Dave','456',23,0),(NULL,'Jack','456',24,1);

        SELECT * FROM users3;


       (2)INSERT SET命令

       MySQL数据库中向数据表中插入记录的第二种语法格式为:

       INSERT [INTO] table_name SET col_name={expr | DEFAULT},...;

       解释说明:与第一种插入记录方式的区别在于,此方法可以使用子查询(SubQuery)。引发子查询有三种情况,其

中有一种情况叫做由比较运算符引起的子查询,这里等号(=)也可以算是一种典型的比较运算符。此方法还有一个与第

一种方法的区别是,第一种可以一次性插入多条记录,但是第二种方法一次性只能插入一条记录。

       例子:

       INSERT users3 SET username='Jord',password='345';

       SELECT * FROM users3;


       (3)INSERT SELECT命令(超纲)

       MySQL数据库中向数据表中插入记录的第三种语法格式为:

       INSERT [INTO] table_name [(col_name,...)] SELECT ...;

       解释说明:此方法可以将查询结果插入到指定数据表。由于我们现在了解的SELECT语句比较少,现在并不好理

解,但我们还是照常写出例子,学完了后面记录的查询我们也可以回过头来再次学习。

       例子:

       1在插入记录前,先来创建一个空的数据表test:

       CREATE TABLE test(

           id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,

           username VARCHAR(20)

       );

       2我们将数据表users3中的年龄大于25的记录的username插入到数据表test中:

       INSERT test(username) SELECT username FROM users3 WHERE age > 25;

       3查询数据表test的记录:

       SELECT * FROM test;


       二更新记录

       MySQL数据库更新数据表的记录也是再经常不过的事了,简单的说就是更改某些记录的某些字段,或者重新赋值

等等。更新记录的方式包括单表更新和多表更新,这里我们只学单表更新,多表更新在后面叙述。

       来看看单表更新的语法格式:

       UPDTAE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1 | DEFAULT} [,col_name2=

{expr2 | DEFAULT}] ... [WHERE where_condition];

       (1)省略关键字WHERE的情况

       如果省略关键字WHERE,导致的是将更新所有的记录。

       例子:

       1)更新一列:

       SELECT * FROM users3;

       UPDATE users3 SET age = age + 5;

       SELECT * FROM users3;


       2)更新多列:

       SELECT * FROM users3;

       UPDATE users3 SET age = age - id,sex = 0;

       SELECT * FROM users3;


       (2)有关键字WHERE下指定几条记录更新的情况

       UPDATE users3 SET age = age + 10 WHERE id % 2 =0;

       SELECT * FROM users3;


       三删除记录

       既然MySQL数据库中的数据表的记录有插入和更新,肯定的就有删除记录的情况,删除记录的方式也有两种:单

表删除和多表删除,这里也是只说单表删除。

       单表删除的语法格式:

       DELETE FROM table_name [WHERE where_condition];

       例子:

       指定条件删除,如果不指定删除哪几条记录,将会全部删除。

       DELETE FROM users3 WHERE id = 6;

       SELECT * FROM users3;


       假设我们再重新插入一条记录,那么插入记录的id字段的值是多少呢?

       INSERT users3 VALUES(NULL,'Bob','234',34,1);

       SELECT * FROM users3;


       


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

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 Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

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

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

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

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles