当前位置:网站首页>MySQL learning record (3)
MySQL learning record (3)
2022-07-02 21:29:00 【White_ Silence (Learning version)】
- Update of data
UPDATE < Table name >
SET < Name > = < expression > [, < Name 2>=< expression 2>...];
WHERE < Conditions >;
-- Optional , It's very important .
ORDER BY Clause ; -
- Optional LIMIT Clause ; -- Optional
-- Modify all registration times ( Here we should connect them to make a sentence )
UPDATE product
SET regist_date = '2009-10-10';
-- Only modify the unit price of some goods
UPDATE product
SET sale_price = sale_price * 10
WHERE product_type = ' Kitchenware ';
--WHERE Is the condition , Select the desired operation object that meets the conditions .
UPDATE= to update , Use UPDATE When setting WHERE Conditions , Otherwise, it is the modification of the overall situation .
-- Single column update
-- take ID by 008 The date of the product of is changed to null NULL
-- Number the item as 0008 The data of ( Ball pen ) The registration date of is updated to NULL
UPDATE product
SET regist_date = NULL
WHERE product_id = '0008';
-- Multi column update
UPDATE product
SET sale_price = sale_price * 10,
purchase_price = purchase_price / 2
WHERE product_type = ' Kitchenware ';
Only unset Only columns with non empty constraints and primary key constraints can be cleared to NULL.** If the column with the above constraints is updated to NULL, Will report a mistake .
2.INSERT sentence ( Insert )
Create a new table first productins
CREATE TABLE productins
(product_id CHAR(4) NOT NULL,
product_name VARCHAR(100) NOT NULL,
product_type VARCHAR(32) NOT NULL,
sale_price INTEGER DEFAULT 0,
purchase_price INTEGER ,
regist_date DATE ,
PRIMARY KEY (product_id));
INSERT Basic syntax :INSERT INTO < Table name > ( Column 1, Column 2, Column 3, ……) VALUES ( value 1, value 2, value 3, ……);
-- Contains a list of columns INSERT INTO productins (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0005', ' pressure cooker ', ' Kitchenware ', 6800, 5000, '2009-01-15'); -- Omit the list INSERT INTO productins VALUES ('0005', ' pressure cooker ', ' Kitchenware ', 6800, 5000, '2009-01-15');
When inserting a full column , You can omit column names , Directly write the corresponding data to be inserted
-- Multiple lines INSERT ( DB2、SQL、SQL Server、 PostgreSQL and MySQL Multi line insertion )
INSERT INTO productins VALUES ('0002', ' Punch ',
' Office Supplies ', 500, 320, '2009-09-11'),
('0003', ' motion T T-shirt ', ' clothes ', 4000, 2800, NULL),
('0004', ' kitchen knife ', ' Kitchenware ', 3000, 2800, '2009-09-20');
-- Insert multiple lines with () and , Segmentation
INSERT The statement wants to assign... To a column NULL When the value of , Can be directly in VALUES Clause in the list of values NULL. Want to insert NULL Columns must not be set NOT NULL constraint .
INSERT INTO productins (product_id, product_name, product_type,
sale_price, purchase_price, regist_date) VALUES ('0006', ' Fork ',
' Kitchenware ', 500, NULL, '2009-09-20');
You can also insert default values into the table ( Initial value ). You can create a table by CREATE TABLE Set... In the statement DEFAULT Constraints to set default values .
Join in DEFAULT 0, The default value is 0
CREATE TABLE productins
(product_id CHAR(4) NOT NULL,
( A little )
sale_price INTEGER
( A little ) DEFAULT 0, -- The default value of the sales unit price is set to 0;
PRIMARY KEY (product_id));
4. Copy the data
Use INSERT … SELECT Statement to copy data from other tables .
-- Copy the data in the item table to the item table
INSERT INTO productocpy (product_id, product_name, product_type, sale_price, purchase_price, regist_date)
SELECT product_id, product_name, product_type, sale_price,
purchase_price, regist_date
FROM Product;
-- Find the source table data (FROM) Use SELECT Select data retrieval and then use INSERT Insert into the replication table
- DML : insert data
STARTTRANSACTION;
INSERT INTO product VALUES('0001', 'T shirt ', ' clothes ', 1000, 500, '2009-09-20');
INSERT INTO product VALUES('0002', ' Punch ', ' Office Supplies ', 500, 320, '2009-09-11');
INSERT INTO product VALUES('0003', ' motion T T-shirt ', ' clothes ', 4000, 2800, NULL);
INSERT INTO product VALUES('0004', ' kitchen knife ', ' Kitchenware ', 3000, 2800, '2009-09-20');
INSERT INTO product VALUES('0005', ' pressure cooker ', ' Kitchenware ', 6800, 5000, '2009-01-15');
INSERT INTO product VALUES('0006', ' Fork ', ' Kitchenware ', 500, NULL, '2009-09-20');
INSERT INTO product VALUES('0007', ' Clean the board ', ' Kitchenware ', 880, 790, '2008-04-28');
INSERT INTO product VALUES('0008', ' Ball pen ', ' Office Supplies ', 100, NULL, '2009-11-11');
COMMIT;
use shop;
-- /* Create a ADDRESSBOOK surface */
create TABLE Addressbook (
regist_no INTEGER NOT NULL,
name varchar(128) NOT NULL,
address VARCHAR(256) NOT NULL,
tel_no CHAR(10),
mail_address CHAR(20),
primary key (regist_no)
);
-- Add a column to the table Fixed length string (8) Non empty constraint
ALTER TABLE Addressbook add column postal_code CHAR(8) NOT NULL;
-- Delete the table
truncate table Addressbook;
I watched the tutorial from Alibaba cloud Tianchi , For personal notes only .
边栏推荐
- Research Report on market supply and demand and strategy of China's plastic trunking industry
- Analysis of enterprise financial statements [1]
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of signal distributors in the global market in 2022
- Micro SD Card Industry Research Report - market status analysis and development prospect forecast
- China's log saw blade market trend report, technological innovation and market forecast
- I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of precoated metallic coatings in the global market in 2022
- Construction and maintenance of business websites [7]
- Construction and maintenance of business websites [6]
猜你喜欢
Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #
Write the content into the picture with type or echo and view it with WinHex
[dynamic planning] p1220: interval DP: turn off the street lights
Baidu sued a company called "Ciba screen"
[shutter] shutter layout component (opacity component | clipprect component | padding component)
treevalue——Master Nested Data Like Tensor
One week dynamics of dragon lizard community | 2.07-2.13
[shutter] shutter layout component (Introduction to layout component | row component | column component | sizedbox component | clipoval component)
Highly qualified SQL writing: compare lines. Don't ask why. Asking is highly qualified..
Basic knowledge of tree and binary tree (detailed illustration)
随机推荐
I did a craniotomy experiment: talk about macromolecule coding theory and Lao Wang's fallacy from corpus callosum and frontal leukotomy
kernel tty_ struct
Huawei Hongmeng watch achieves fireworks display effect on New Year's Eve
Redis -- three special data types
China plastic bottle market trend report, technological innovation and market forecast
China's crude oil heater market trend report, technological innovation and market forecast
Golang embeds variables in strings
Codeworks global round 19 (CF 1637) a ~ e problem solution
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
It is said that this year gold three silver four has become gold one silver two..
[dynamic planning] p1220: interval DP: turn off the street lights
What is the difference between programming in real work and that in school?
China plastic box market trend report, technological innovation and market forecast
In depth research and investment feasibility report on the global and China active vibration isolation market 2022-2028
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of signal distributors in the global market in 2022
Analysis of enterprise financial statements [4]
China microporous membrane filtration market trend report, technological innovation and market forecast
Construction and maintenance of business websites [7]
Research Report on market supply and demand and strategy of China's right-hand outward rotation entry door industry
Structured text language XML