云数据库 GAUSSDB-INSERT:示例

时间:2024-07-01 18:37:09

示例

  • 插入一条数据
    示例:
    --建表。
    gaussdb=# CREATE TABLE test_t1(col1 INT,col2 VARCHAR);
    
    --插入数据。
    gaussdb=# INSERT INTO test_t1 (col1, col2) VALUES (1,'AB');
    
    --只给表中部分列添加值。
    gaussdb=# INSERT INTO test_t1 (col1) VALUES (2);
    
    --VALUES关键字左边没有括号,右边括号里面必须严格按照表结构的顺序给所有的字段添加值。
    gaussdb=# INSERT INTO test_t1 VALUES (3,'AC');
    
    --查询表。
    gaussdb=# SELECT * FROM test_t1;
     col1 | col2 
    ------+------
        1 | AB
        2 | 
        3 | AC
    (3 rows)
    
    --删除。
    gaussdb=# DROP TABLE test_t1;
  • 插入多条数据
    示例:
    --建表。
    gaussdb=# CREATE TABLE test_t2(col1 INT,col2 VARCHAR);
    gaussdb=# CREATE TABLE test_t3(col1 INT,col2 VARCHAR);
    
    --插入多条数据。
    gaussdb=# INSERT INTO test_t2 (col1, col2) VALUES (10,'AA'),(20,'BB'),(30,'CC');
    
    --查询。
    gaussdb=# SELECT * FROM test_t2;
     col1 | col2 
    ------+------
       10 | AA
       20 | BB
       30 | CC
    (3 rows)
    
    --把test_t2中的数据插入到test_t3中。
    gaussdb=# INSERT INTO test_t3 SELECT * FROM test_t2;
    
    --查询。
    gaussdb=# SELECT * FROM test_t3;
     col1 | col2 
    ------+------
       10 | AA
       20 | BB
       30 | CC
    (3 rows)
    
    --删除。
    gaussdb=# DROP TABLE test_t2;
    DROP TABLE test_t3;
  • ON DUPLICATE KEY UPDATE

    示例:

    --建表。
    gaussdb=# CREATE TABLE test_t4 (id INT PRIMARY KEY, info VARCHAR(10));
    gaussdb=# INSERT INTO test_t4 VALUES (1, 'AA'), (2,'BB'), (3, 'CC');
    
    --使用ON DUPLICATE KEY UPDATE关键字。
    gaussdb=# INSERT INTO test_t4 VALUES (3, 'DD'), (4, 'EE') ON DUPLICATE KEY UPDATE info = VALUES(info);
    
    --查询。
    gaussdb=# SELECT * FROM test_t4;
     id | info 
    ----+------
      1 | AA
      2 | BB
      4 | EE
      3 | DD
    
    --删除。
    gaussdb=# DROP TABLE test_t4;
  • INSERT IGNORE

    示例1:破坏NOT NULL约束

    --建表。
    gaussdb=# CREATE TABLE test_t5(f1 INT NOT NULL);
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t5 VALUES(NULL);
    WARNING:  null value in column "f1" violates not-null constraint
    DETAIL:  Failing row contains (null).
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM test_t5;
     f1
    ----
      0
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t5;
    DROP TABLE

    示例2:唯一键冲突

    --建表。
    gaussdb=# CREATE TABLE test_t6(f1 INT PRIMARY KEY);
    NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_t6_pkey" for table "test_t6"
    CREATE TABLE
    
    --插入数据。
    gaussdb=# INSERT INTO test_t6 VALUES(1);
    INSERT 0 1
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t6 VALUES(1);
    INSERT 0 0
    
    --查询表。
    gaussdb=# SELECT * FROM test_t6;
     f1
    ----
      1
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t6;
    DROP TABLE

    示例3:插入的值没有找到对应的分区

    --建表。
    gaussdb=# CREATE TABLE test_t7(f1 INT, f2 INT) PARTITION BY LIST(f1) (PARTITION p0 VALUES(1, 4, 7), PARTITION p1 VALUES (2, 5, 8));
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t7 VALUES(3, 5);
    INSERT 0 0
    
    --查询表。
    gaussdb=# SELECT * FROM test_t7;
     f1 | f2
    ----+----
    (0 rows)
    
    --删除表。
    gaussdb=# DROP TABLE test_t7;
    DROP TABLE

    示例4:指定分区插入时,插入的数据与指定的分区不匹配

    --建表。
    gaussdb=# CREATE TABLE test_t8(f1 INT NOT NULL, f2 TEXT, f3 INT) PARTITION BY RANGE(f1)(PARTITION p0 VALUES LESS THAN(5), PARTITION p1 VALUES LESS THAN(10), PARTITION p2 VALUES LESS THAN(15), PARTITION p3 VALUES LESS THAN(MAXVALUE));
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t8 PARTITION(p2) VALUES(20, 'Jan', 1);
    INSERT 0 0
    
    --查询表。
    gaussdb=# SELECT * FROM test_t8;
     f1 | f2 | f3
    ----+----+----
    (0 rows)
    
    --删除表。
    gaussdb=# DROP TABLE test_t8;
    DROP TABLE

    示例5:子查询返回多行

    --建表。
    gaussdb=# CREATE TABLE test_t9(f1 INT, f2 INT);
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --插入数据。
    gaussdb=# INSERT INTO test_t9 VALUES(1, 1), (2, 2), (3, 3);
    INSERT 0 3
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t9 VALUES((SELECT f1 FROM test_t9), 0);
    WARNING:  more than one row returned by a subquery used as an expression
    CONTEXT:  referenced column: f1
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM test_t9 WHERE f2 = 0;
     f1 | f2
    ----+----
        |  0
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t9;
    DROP TABLE

    示例6:数据过长

    --建表。
    gaussdb=# CREATE TABLE test_t10(f1 VARCHAR(5));
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t10 VALUES('aaaaaaaaa');
    WARNING:  value too long for type character varying(5)
    CONTEXT:  referenced column: f1
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM test_t10;
      f1
    -------
     aaaaa
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t10;
    DROP TABLE

    示例7:时间函数溢出

    --建表。
    gaussdb=# CREATE TABLE test_t11(f1 DATETIME);
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f1' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t11 VALUES(date_sub('2000-01-01', INTERVAL 2001 YEAR));
    WARNING:  Datetime function: datetime field overflow
    CONTEXT:  referenced column: f1
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM test_t11;
     f1
    ----
    
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t11;
    DROP TABLE

    示例8:被0除

    --建表。
    gaussdb=# CREATE TABLE test_t12(f1 INT);
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t12 VALUES(1/0);
    WARNING:  division by zero
    CONTEXT:  referenced column: f1
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM test_t12;
     f1
    ----
    
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t12;
    DROP TABLE

    示例9:值不正确

    --建表。
    gaussdb=# CREATE TABLE test_t13(f0 INT, f1 FLOAT);
    NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'f0' as the distribution column by default.
    HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
    CREATE TABLE
    
    --使用IGNORE关键字。
    gaussdb=# INSERT IGNORE INTO test_t13 VALUES(1, '1.11aaa');
    WARNING:  invalid input syntax for type real: "1.11aaa"
    LINE 1: INSERT IGNORE INTO test_t13 VALUES(1, '1.11aaa');
                                                  ^
    CONTEXT:  referenced column: f1
    INSERT 0 1
    
    --查询表。
    gaussdb=# SELECT * FROM  test_t13;
     f0 |  f1
    ----+------
      1 | 1.11
    (1 row)
    
    --删除表。
    gaussdb=# DROP TABLE test_t13;
    DROP TABLE
  • WITH [ RECURSIVE ] with_query [, ...]
    示例:
    --成绩表。
    gaussdb=# CREATE TABLE grade (
        sid INT,
        course VARCHAR(20),
        score FLOAT
    );
    --学生表。
    gaussdb=# CREATE TABLE student(
        sid INT PRIMARY KEY,
        class INT,
        name  VARCHAR(50),
        sex INT CHECK (sex = 0 or sex = 1)
    );
    
    --插入数据。
    gaussdb=# WITH student_sid AS ( INSERT INTO student ( sid, CLASS, NAME, sex ) VALUES ( 1, 1, 'scott', 1 ) RETURNING sid ) 
        INSERT INTO grade ( sid, course, score )
        VALUE ( ( SELECT sid FROM student_sid ), 'match', '96' ),
    	  ( ( SELECT sid FROM student_sid ), 'chinese', '82' ),
    	  ( ( SELECT sid FROM student_sid ), 'english', '86' );
    
    --查询数据。
    gaussdb=# SELECT * FROM student;
     sid | class | name  | sex 
    -----+-------+-------+-----
       1 |     1 | scott |   1
    (1 row)
    
    gaussdb=# SELECT * FROM grade;
     sid | course  | score 
    -----+---------+-------
       1 | match   |    96
       1 | chinese |    82
       1 | english |    86
    (3 rows)
    
    --删除。
    gaussdb=# DROP TABLE student;
    gaussdb=# DROP TABLE grade;
support.huaweicloud.com/distributed-devg-v8-gaussdb/gaussdb-12-0645.html