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),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,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 jason@example.com
2 RT435234 Robert Treat rob@example.com
3 GS998909 Greg Sabino Mullane greg@example.com
4 MW777983 Matt Wade matt@example.com

导出表 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 | jason@example.com
2 | RT435234 | Robert Treat | rob@example.com
3 | GS998909 | Greg Sabino Mullane | greg@example.com
4 | MW777983 | Matt Wade | matt@example.com
(4 rows)

输出对象ID(OIDS):

psql> COPY employee TO STDOUT OIDS;
24627 1 GM100011 Jason Gilmore jason@example.com
24628 2 RT435234 Robert Treat rob@example.com
24629 3 GS998909 Greg Sabino Mullane greg@example.com
24630 4 MW777983 Matt Wade matt@example.com

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

psql>COPY employee TO STDOUT DELIMITER '|';
1|GM100011|Jason Gilmore|jason@example.com
2|RT435234|Robert Treat|rob@example.com
3|GS998909|Greg Sabino Mullane|greg@example.com
4|MW777983|Matt Wade|matt@example.com

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

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

导出指定字段的数据:

psql> COPY employee (name,email) TO STDOUT;
Jason Gilmore jason@example.com
Robert Treat rob@example.com
Greg Sabino Mullane greg@example.com
Matt Wade matt@example.com

为 NULL 字段设置默认值:

psql> COPY employee TO STDOUT NULL 'no email';
Jason Gilmore no email
Robert Treat rob@example.com
Greg Sabino Mullane greg@example.com
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'换行符

相关文章

来源:http://www.postgres.cn/docs/11/ 4.1.1. 标识符和关键词 SQL标识符和关键词必须以一个...
来源:http://www.postgres.cn/docs/11/ 8.1. 数字类型 数字类型由2、4或8字节的整数以及4或8...
来源:http://www.postgres.cn/docs/11/ 5.1. 表基础 SQL并不保证表中行的顺序。当一个表被读...
来源:http://www.postgres.cn/docs/11/ 6.4. 从修改的行中返回数据 有时在修改行的操作过程中...
来源:http://www.postgres.cn/docs/11/ 13.2.1. 读已提交隔离级别 读已提交是PostgreSQL中的...
来源:http://www.postgres.cn/docs/11/ 9.7. 模式匹配 PostgreSQL提供了三种独立的实现模式匹...