当前位置:网站首页>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 the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
- AES encryption CBC mode pkcs7padding filling Base64 encoding key 32byte iv16byte
- Add two numbers of leetcode
- Is it safe to buy funds on securities accounts? Where can I buy funds
- rwctf2022_ QLaaS
- Chinese Indian seasoning market trend report, technical dynamic innovation and market forecast
- Research Report on the overall scale, major manufacturers, major regions, products and applications of sliding door dampers in the global market in 2022
- Activation function - relu vs sigmoid
- Lantern Festival, come and guess lantern riddles to win the "year of the tiger Doll"!
- Research Report on ranking analysis and investment strategic planning of RFID market competitiveness of China's industrial manufacturing 2022-2028 Edition
猜你喜欢
Redis sentinel cluster working principle and architecture deployment # yyds dry goods inventory #
Volvo's first MPV is exposed! Comfortable and safe, equipped with 2.0T plug-in mixing system, it is worth first-class
[shutter] statefulwidget component (floatingactionbutton component | refreshindicator component)
Customized Huawei hg8546m restores Huawei's original interface
[dynamic planning] p1220: interval DP: turn off the street lights
Friends who firmly believe that human memory is stored in macromolecular substances, please take a look
Web3js method to obtain account information and balance
Common routines of compressed packets in CTF
JDBC | Chapter 4: transaction commit and rollback
[CV] Wu Enda machine learning course notes | Chapter 12
随机推荐
qwb2018_ core kernel_ rop
[hands on deep learning]02 softmax regression
China's Micro SD market trend report, technology dynamic innovation and market forecast
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of power management units in the global market in 2022
China's crude oil heater market trend report, technological innovation and market forecast
When Valentine's Day falls on Monday
Welfare | Pu Aries | liv heart co branded Plush surrounding new products are on the market!
China plastic bottle and container market trend report, technological innovation and market forecast
Construction and maintenance of business website [2]
Research Report on market supply and demand and strategy of Chinese garden equipment industry
Research Report on the overall scale, major manufacturers, major regions, products and applications of micro hydraulic cylinders in the global market in 2022
It is said that this year gold three silver four has become gold one silver two..
Investment strategy analysis of China's electronic information manufacturing industry and forecast report on the demand outlook of the 14th five year plan 2022-2028 Edition
Baidu sued a company called "Ciba screen"
[shutter] statefulwidget component (bottom navigation bar component | bottomnavigationbar component | bottomnavigationbaritem component | tab switching)
Cardinality sorting (detailed illustration)
Research Report on market supply and demand and strategy of microplate instrument industry in China
Download vagrant box file locally from Atlas and configuring it
Chinese Indian seasoning market trend report, technical dynamic innovation and market forecast
Construction and maintenance of business websites [9]