postgresql以及mysql的数据导出导入到文件

 创建复合主键:create table mapping(adminId integer,roleId integer, primary key(adminId,roleId));

这样我的两个字段就都成为主键了。

创建符合主键的同时建立外键:

myd=# create table mapping(adminId integer references admin(id),roleId integer references role(id), primary key(adminId,roleId));

这样我的的两个字段既是主键的组成部分,又分别参照了别的表的字段。

创建符合主键的同时建立外键并级联删除级联跟新:

myd=# create table mapping(adminid integer references admin(id) on delete cascade on update cascade,roleid integer references role(id) on delete cascade on update cascade, primary key(adminid,roleid));

下面是提示信息:
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "mapping_pkey" for table "mapping"
CREATE TABLE

多表连接:
myd=# select (admin.id,admin.name,role.name) from admin,mapping,role where admin.id=mapping.adminid and mapping.roleid=role.id;

多表连接同时指定列的别名:
myd=# select admin.id as id,admin.name as name,role.name as role from admin,mapping,role where admin.id=mapping.adminid and mapping.roleid=role.id;

对已经存在的表添加主键:
 alter table t add primary key(id);
其中t为表明,id为要设置为主键的字段,注意,如果字段中有重复值,会创建失败。

distinct去重
select count(distinct col) from A;

select count(1) from (select 1 from A group by col) alias;

copy导入文件和导出文件:
COPY 命令可以快速的导入数据到 PostgreSQL 中,文件格式类似CVS之类。适合批量导入数据,比 \i 和恢复数据表快。

导出表数据到文件或 STDOUT :

COPY tablename [(column [, ...])]
   TO {'filename' | STDOUT}
   [[WITH]
      [BINARY]
      [OIDS]
      [DELIMITER [AS] 'delimiter']
      [NULL [AS] 'null string']
      [CSV [HEADER]
         [QUOTE [AS] 'quote']
         [ESCAPE [AS] 'escape']
         [FORCE NOT NULL column [, ...]]

导入文件或者 STDIN 到表中:

COPY tablename [(column [, ...])]
   FROM {'filename' | STDIN}
   [[WITH]
      [BINARY]
      [OIDS]
      [DELIMITER [AS] 'delimiter']
      [NULL [AS] 'null string']
      [CSV [HEADER]
         [QUOTE [AS] 'quote']
         [ESCAPE [AS] 'escape']
         [FORCE QUOTE column [, ...]]

导出表 employee 到默认输出 STDOUT:

psql> COPY employee TO STDOUT;
1       JG100011        Jason Gilmore         [email protected]
2       RT435234        Robert Treat          [email protected]
3       GS998909        Greg Sabino Mullane   [email protected]
4       MW777983        Matt Wade             [email protected]

导出表 employee 到 sql 文件:

psql> COPY employee TO '/home/smallfish/employee.sql';

从文件导入数据:

psql> COPY employeenew FROM '/home/smallfish/employee.sql';
psql> SELECT * FROM employeenew;
employeeid  | employeecode |     name            |       email
------------+--------------+---------------------+---------------
          1 | JG100011     | Jason Gilmore       | [email protected]
          2 | RT435234     | Robert Treat        | [email protected]
          3 | GS998909     | Greg Sabino Mullane | [email protected]
          4 | MW777983     | Matt Wade           | [email protected]
(4 rows)

输出对象ID(OIDS):

psql> COPY employee TO STDOUT OIDS;
24627  1       GM100011        Jason Gilmore         [email protected]
24628  2       RT435234        Robert Treat          [email protected]
24629  3       GS998909        Greg Sabino Mullane   [email protected]
24630  4       MW777983        Matt Wade             [email protected]

指定导出间隔符,默认是 \t ,这里为 | :

psql>COPY employee TO STDOUT DELIMITER '|';
1|GM100011|Jason Gilmore|[email protected]
2|RT435234|Robert Treat|[email protected]
3|GS998909|Greg Sabino Mullane|[email protected]
4|MW777983|Matt Wade|[email protected]

导入文件数据,指定间隔符为 | :

psql> COPY employeenew FROM '/home/smallfish/employee.sql' DELIMITER |;

导出指定字段的数据:

psql> COPY employee (name,email) TO STDOUT;
Jason Gilmore         [email protected]
Robert Treat          [email protected]
Greg Sabino Mullane   [email protected]
Matt Wade             [email protected]

为 NULL 字段设置默认值:

psql> COPY employee TO STDOUT NULL 'no email';
Jason Gilmore         no email
Robert Treat          [email protected]
Greg Sabino Mullane   [email protected]
Matt Wade             no email

导出为CVS格式:

psql> COPY employee (name, email) TO '/home/smallfish/employee.csv' CSV HEADER;

 






Mysql导出数据到文件: select *(或者字段名) from tablename into outfile "/root/aa.txt";

Mysql导入到Mysql中: load data infile "/root/aa.txt" into table tablename;   table需要提前创建好。

MySQL INTO OUTFILE/INFILE导出导入数据

--使用SELECT ... INTO OUTFILE 以逗号分隔字段的方式将数据导入到一个文件中:

SELECT * INTO OUTFILE 'D:\\log1.txt' FIELDS TERMINATED BY ',' FROM log.log1

--将刚刚导出的文件log1.txt导入到表log1相同结构的log2中:

LOAD DATA INFILE 'D:\\log1.txt' INTO TABLE aa.log2 FIELDS TERMINATED BY ','

-- 使用select * into outfile 导出:

SELECT * INTO OUTFILE 'd:\\test.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test.table

-- 导入

LOAD DATA INFILE '/tmp/fi.txt' INTO TABLE test.fii FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

 

FIELDS TERMINATED BY ',' 字段间分割符
OPTIONALLY ENCLOSED BY '"' 将字段包围 对数值型无效
LINES TERMINATED BY '\n' 换行符


你可能感兴趣的:(PostgreSQL)