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 |
    +----+--------+-----+-----------+
    
  • 相关阅读:
    【协议分析】Wireshark 过滤表达式实例
    学习Javascript闭包(Closure)
    如何解决 touchstart 事件与 click 事件的冲突
    JS实现控制HTML5背景音乐播放暂停
    $.ajax()方法详解
    js将汉字转为相应的拼音
    js 滚动到一定位置导航定位在页面最顶部
    javascript获取网页地址栏的id
    jquery 上传图片转为base64,ajax提交到后台
    jquery 图片转为base64
  • 原文地址:https://www.cnblogs.com/wangbaby/p/10289578.html
Copyright © 2011-2022 走看看