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 |
+----+--------+-----+-----------+

猜你喜欢

转载自www.cnblogs.com/wangbaby/p/10289578.html
今日推荐