当前位置:网站首页>Basic SQL statement - insert

Basic SQL statement - insert

2022-06-10 23:15:00 InfoQ


increase (insert)

insert Use standard

insert into table_name[(column [, column...])]
values (values [,values...]);

insert Code samples

--  Create a product table goods(id int,goods_name,varchar(10),price double);
CREATE TABLE `goods`(
 id INT,
 goods_name VARCHAR(10),
 pricre DOUBLE);
--  modify goods In the table pricre by price
ALTER TABLE `goods`
 CHANGE `pricre` `price` DOUBLE;
--  Use desc Check out the table details
DESC goods
--  Insert a set of elements
INSERT INTO `goods` (id,goods_name,price)
 VALUES(10,'huawei',5000);
--  Insert another set of elements
INSERT INTO `goods` (id,goods_name,price)
 VALUES(11,'iphone',6000);
--  Check the contents of the table
SELECT * FROM goods;

not null Set that the element cannot be null ,default Set the default value without assignment

--  Add default is 100
price double not null default 100

insert matters needing attention

  • The inserted data should be of the same type as the field .('30' Can be added to int in ),'abc' Can not be )
  • The data length should be within the specified range .
  • values The data positions listed in must correspond to the arrangement of the added columns .
  • Characters and dates should be added in
    ' '
    in .
  • Columns can be inserted with null values , If this field can be null ,insert into table values'null'.
  • insert into table_name( Name ...) values (),(),(), Form multiple records .
  • If you add data to all fields in the table , You can not write the previous field name .
  • Use of default values , When a field value is not given , If there is a default value, the default value will be added , Otherwise, the report will be wrong .( If a column , Is not specified not null, When adding data , If no value is given, it will default to null )
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102200169047.html