Mysql数据的导入和导出

    xiaoxiao2022-07-14  162

    数据导入 1、作用:把文件系统中的内容导入到数据库中 2、语法: load data infile "文件名" into table 表名 fields terminated by "分隔符" #每条记录的各字段值之间 lines terminated by "\n"; 3、将scoretable.csv文件导入到数据库的表中 1、在数据库中创建对应的表 create table student( id int primary key auto_increment, name varchar(7), score float(5,2), number bigint, class char(7) ); 2、把文件拷贝到数据库的默认搜索路径中 1、查看默认搜索路径 show variables like "secure_file_priv"; #默认为:/var/lib/mysql-files/ 2、拷贝文件 sudo cp ~/scoretable.csv /var/lib/mysql-files/ 3、执行数据导入语句 load data infile "/var/lib/mysql-files/scoretable.csv" into table student fields terminated by "," lines terminated by "\n"; 4、Excel表格如何转化为CSV文件 打开Excel文件->另存为->CSV(逗号分隔) 5、更改文件编码格式 用记事本/编辑器打开,文件->另存为->选择编码 数据导出 1、作用: 将数据库中表的记录导出到系统文件里 2、语法格式 select ... from 表名 into outfile "/var/lib/mysql-files/文件名" fields terminated by "分隔符" lines terminated by "\n"; 3、将mysql库下的user表中的user、host两个字段的值导出到user.txt select user,host from mysql.user into outfile "/var/lib/mysql-files/user.txt" fields terminated by " " lines terminated by "\n"; 练习 1、把/etc/passwd 导入到数据库表里面 king : x : 1000 : 1000 : king,,, : /home/king 用户名 密码 UID号 GID号 用户描述 用户的家目录 : /bin/bash 登录权限 2、在userinfo第一列添加一个id字段,主键、自增长、显示宽度 为3,位数不够用0填充
    最新回复(0)