Examples of adding, modifying, deleting and clearing values ​​in MySQL database

Examples of adding, modifying, deleting and clearing values ​​in MySQL database

3. MySQL Data Management

The first method: not recommended, it seems complicated

-- The gradeid field of the student table should reference the gradeid field of the grade table.
-- Define foreign key
-- Add constraints to this foreign key, (execute reference), REFERENCES reference key `FK_gradeid`(`gradeid`)
CONSTRAINT `FK_gradeid` FOREIGN KEY (`gradeid`) REFERENCES `grade`(`gradeid`)

The second method: Suggestion

-- After successfully creating the table, add a foreign key constraint -- There is no foreign key relationship when creating the table ALTER TABLE `student`
ADD CONSTRAINT `FK_gradeid` FOREIGN KEY(`gradeid`) REFERENCES `grade`(`gradeid`);
-- ALTER TABLE table ADD CONSTRAINT `` FOREIGN KEY (``) REFERENCES `field`

The above operations are all physical foreign keys, database-level foreign keys, which we do not recommend using (to avoid confusion caused by too many databases, just understand here)

Best Practices

  • The database is a simple table
  • We want to use multiple table data and use foreign keys (implemented by program)

3.2 DML Language

Database language: data storage, data management

3.3. Add INSERT

-- Insert statement INSERT INTO table name ([field 1, field 2, field 2])
values(''),(''),('')

-- INSERT INTO 'grade'('gradename') VALUES ('Senior Year')
-- Generally, when writing insert statements, the data and the fields to be inserted must correspond one to one -- Add multiple fields to a field INSERT INTO `indicates`(`attribute column field`, ...) 
VALUES ('matched value 1', 'matched value 2', 'matched value 3')
/*
If you do not want to write the field names of the attribute columns, then match each attribute column exactly and write the following values ​​one by one*/

Precautions

  • Use commas to separate fields.
  • The field can be omitted, but the following values ​​must correspond one to one.
  • You can insert multiple data at the same time. The values ​​after VALUES need to be separated by VALUSE(),(),......
-- Insert multiple data <i.e. tuple>, multiple rows of data INSERT INTO `s`(`SNO`,`SNAME`,`SEX`,`Sage`) VALUES ('180109','王五','男','76'),('180108','李四','男','88')

3.3 Modifications

update Modify whom (condition)

-- Modify the student's name UPDATE `student` SET `name`='CJ7' WHERE id = 1; -- Conditional-- If not specified, all tables will be changed! ! ! ! !
UPDATE `student` SET `name` = 'CJ7'
-- Syntax -- UPDATE table name SET colum_name = value, [colum_name = value, ....] where [condition]

Condition: where operator


Operators meaning scope result
= equal 5=6 false
<> or != Not equal to 5<>6 true
> Greater than 5>6 false
< Less than 5<6 true
>= Greater than or equal to 5>=6 false
<= Less than or equal to 5<=6 true
between A and B Between A and B between 1 and 10 Between 1 and 10, excluding 10
AND Connection conditions && Condition 1 and Condition 2 All established All true is true
OR or|| Condition 1 and Condition 2 Just one establishment Only one truth is true

Note:

  • colnum_name is the database column, try to include ``
  • Condition, the filter condition. If not specified, all columns will be modified.
  • VLAUE is a specific value, or it can be a variable, such as birthday = CURRENT_TIME current_time is the current time
  • Use commas to separate multiple properties.
-- Example of modifying the statement UPDATE `student` SET `birthday` = CURRENT_TIME WHERE `name` = 'CJ7' AND SEX = 'female'

3.4 Deletion

delete command

Syntax: delete from indicates where judgment condition

-- Delete data (use with caution, it is easy to delete the database and run away)
DELETE FROM `STUDENT`

-- Delete specified data DELETE FROM `student` WHERE id = 1;

TRUNCATE command

Function: Completely clear a database table, the table structure and index constraints will not change!!

-- Clear the data table, for example, clear the student table TRUNCATE `student`
-- Safe writing, delete table, prevent deletion of database named `student`
TRUNCATE TABLE `student`

Difference between delete and TRUNCATE

Similarities: Both can delete data, but will not delete the table structure

different:

  • TRUNCATE resets the auto-increment column and the counter returns to zero
  • TRUNCATE does not affect transactions
-- Test CREATE TABLE `test`(
    `id` INT(4) NOT NULL AUTO_INCREMENT,
    `COLL` VARCHAR(20) NOT NULL,
    PRIMARY KEY('id')
)ENGINE=INNODB DEFAULT CHARSET=UTF8

INSERT TNTO `test` (`coll`) VALUES('1')('2')('3')

DELETE FROM `test` -- will not affect auto-increment TRUNCATE TABLE `test` -- will reset to zero

Understand: delete deletion problem, restart the database, phenomenon

  • INNODB auto-increment columns start at 1 (they are lost in memory if power is off)
  • MYISAM continues from the last sub-increment (the data in the file will not be lost)

+++

+++

Add examples after class

Add to:

-- An example of adding a tuple insert into `Student` values('180103','对象三','女','22','CS')

-- Test adding multiple tuples, separated by commas INSERT INTO `Student` VALUES('180105','对象五','男','19','IS'),('180106','对象六','女','20','CS'),('180107','对象七','女','20','CS')

-- Because the table structure is set to non-empty, there is no test here to only add the value of a certain attribute column 

+++

Modification: update

UPDATE `student` SET `sname` = 'Operation modification object 1' WHERE `sno` = 180102;
-- Remember not to use commas when adding. My problem was that I used commas below -- UPDATE `student` SET `sname` = 'Operation modification object 1', WHERE `sno` = 180102;

After adding successfully, the table result screenshot is shown:

Delete: delete

-- Delete a single record <Delete this record if it meets the WHERE condition>
DELETE FROM `student` WHERE sname = 'Operation modification object 1';
-- Use TRUNCATE to clear the data table TRUNCATE Table `student`
-- Or use delete to delete all databases DELETE FROM `student`
-- Will not affect auto-increment

Operation results display

-- Because all data is deleted, the result is that the table is empty

Summarize

This is the end of this article about adding, modifying, deleting and clearing MySQL database values. For more information about adding, modifying, deleting and clearing MySQL values, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • PHP+MYSQL implements user addition, deletion, modification and query
  • Python connects to MySQL database example (to perform addition, deletion and modification operations)
  • mysql add modify field type and delete field type
  • PHP+MySQL implements simple add, delete, modify and query functions
  • mysql add, delete, modify and query basic statements
  • Summary of basic addition, deletion, query and modification operations on indexes in MySQL
  • PHP connects to MySQL to perform add, delete, modify, and query operations
  • A simple example of adding, deleting, modifying and checking PHP MySql

<<:  Tutorial on building a JMeter+Grafana+influxdb visual performance monitoring platform in docker environment

>>:  Summary of web designers' experience and skills in learning web design

Recommend

Echart Bar double column chart style most complete detailed explanation

Table of contents Preface Installation and Config...

The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands

What does Ctrl+c, Ctrl+d, Ctrl+z mean in Linux? C...

Running PostgreSQL in Docker and recommending several connection tools

1 Introduction PostgreSQL is a free software obje...

MySQL uses frm files and ibd files to restore table data

Table of contents Introduction to frm files and i...

MySQL 5.7.20 installation and configuration method graphic tutorial (win10)

This article shares the installation and configur...

Detailed explanation of redo log and undo log in MySQL

The most important logs in the MySQL log system a...

js implements a simple calculator

Use native js to implement a simple calculator (w...

CSS writing format, detailed explanation of the basic structure of a mobile page

1. CSS writing format 1. Inline styles You can wr...

Summary of problems that may occur when using JDBC to connect to Mysql database

First, clarify a few concepts: JDBC: Java databas...

How to quickly add columns in MySQL 8.0

Preface: I heard a long time ago that MySQL 8.0 s...

MySQL Series 12 Backup and Recovery

Table of contents Tutorial Series 1. Backup strat...