zoukankan      html  css  js  c++  java
  • PostgreSQL自学笔记:7 插入、更新与删除数据

    7 插入、更新与删除数据

    7.1 插入数据

    先创建表person:

        create table person(
            id int not null,
            name char(40) not null default '',
            age int not null default 0,
            info char(50) null,
            primary key(id)
        );      
    

    7.1.1 为表的所有字段插入数据

    insert into 表名 (属性1,属性2,...) 
        values(值1,值2,...) [,(值1,值2,...),(值1,值2,...),...]
    
    insert into person(id,name,age,info) 
        values(1,'Green',21,'Lawyer');
    
    insert into person
        values(2,'Suse',22,'dancer');
    

    7.1.2 为表的指定字段插入数据

    insert into person(id,name,age)
        values(3,'Mary',24);
    

    7.1.3 同时插入多条记录

    insert into person
        values(4,'Laura',25,'Musician'),(5,'Evans',27,'secretary');
    

    7.1.4 将查询结果插入表中

    insert into 插入表名 (属性)
        select (属性) from 查询表名 where 条件;
    

    先建表personNew:

        create table personNew(
            id int not null,
            name char(40) not null default '',
            age int not null default 0,
            info char(50) null,
            primary key(id)
        );  
    
    insert into personNew
        select * from person; 
    

    该表显示:

        +----+-------+-----+-----------+
        | id | name  | age | info      |
        +----+-------+-----+-----------+
        |  1 | Green |  21 | Lawyer    |
        |  2 | Suse  |  22 | dancer    |
        |  3 | Mary  |  24 | NULL      |
        |  4 | Laura |  25 | Musician  |
        |  5 | Evans |  27 | secretary |
        +----+-------+-----+-----------+
    

    7.2 更新数据

    update 表名 set 属性1=值1 [,属性2=值2,...] where 条件;

    update person set age=15,name='LiMing' where id=3;
    update person set info='student' where age between 19 and 24;

    7.3 删除数据

    delete from 表名 [where 条件];

    delete from person where id=1;

    +----+--------+-----+-----------+
    | id | name   | age | info      |
    +----+--------+-----+-----------+
    |  2 | Suse   |  22 | student   |
    |  3 | LiMing |  15 | NULL      |
    |  4 | Laura  |  25 | Musician  |
    |  5 | Evans  |  27 | secretary |
    +----+--------+-----+-----------+
    
  • 相关阅读:
    python常用函数 A
    从__name__=='__main__'说到__builtin__
    python常用魔法函数
    MySQL内核:InnoDB存储引擎 卷1
    “胡”说IC——菜鸟工程师完美进阶
    恶意代码分析实战
    转折点:移动互联网时代的商业法则
    大型网站系统与Java中间件实践
    《Node.js实战(双色)》作者之一——吴中骅访谈录
    网站运维技术与实践
  • 原文地址:https://www.cnblogs.com/wangbaby/p/10289578.html
Copyright © 2011-2022 走看看