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

Mysql sets boolean type operations

Mysql sets boolean type 1. Tinyint type We create...

Explain the difference between iframe and frame in HTML with examples

I don't know if you have used the frameset at...

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a pr...

Solution for using Baidu share on Https page

Since enabling https access for the entire site, ...

Analysis of mysql view functions and usage examples

This article uses examples to illustrate the func...

How to use the Clipboard API in JS

Table of contents 1. Document.execCommand() metho...

How to use the Linux seq command

1. Command Introduction The seq (Sequence) comman...

React sample code to implement login form

As a Vue user, it's time to expand React. Fro...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

How to Rename Multiple Files at Once in Linux

Preface In our daily work, we often need to renam...

Details after setting the iframe's src to about:blank

After setting the iframe's src to 'about:b...