导入数据库
从sql脚本导入
- 首先建空数据库
mysql>create database abc; - 导入数据库
方法一:
(1)选择数据库mysql>use abc;
(2)设置数据库编码mysql>set names utf8;
(3)导入数据(注意sql文件的路径)mysql>source /home/abc/abc.sql;
方法二:mysql -u用户名 -p 数据库名 < 数据库名.sql
mysql -uabc_f -p abc < abc.sql
从excel等导入
- 另存为csv用
,
分割 load data local infile 'C:\\abc.txt' into table t fields terminated by ',';
导出数据库
用mysqldump命令(注意MySQL的安装路径,即此命令的路径):
1、导出数据和表结构:
mysqldump -u用户名 -p 数据库名 > 数据库名.sql/usr/local/mysql/bin/mysqldump -uroot -p abc > abc.sql
敲回车后会提示输入密码
2、只导出表结构
mysqldump -u用户名 -p -d 数据库名 > 数据库名.sql/usr/local/mysql/bin/mysqldump -uroot -p -d abc > abc.sql
创建数据库
使用 create database
语句可完成对数据库的创建, 创建命令的格式如下:
create database 数据库名 [其他选项];
例如我们需要创建一个名为 samp 的数据库, 在命令行下执行以下命令:
create database samp;
提示: 可以使用 show databases;
命令查看已经创建了哪些数据库。
use 语句可以不加分号, 执行 use samp
来选择刚刚创建的数据库, 选择成功后会提示: Database changed
创建数据库表
使用 create table 语句可完成对表的创建, create table 的常见形式:
create table 表名称(列声明);
以创建 students 表为例, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel) 这些内容:
操作 MySQL 数据库
向表中插入数据
insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:
insert into students values(NULL, "王刚", "男", 20, "13811371377");
有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:
insert into students (name, sex, age) values("孙丽华", "女", 21);
查询表中的数据
select 语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:
select 列名称 from 表名称 [查询条件];
例如要查询 students 表中所有学生的名字和年龄, 输入语句
select name, age from students;
也可以使用通配符 查询表中所有的内容, 语句: select * from students;
按特定条件查询
where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;
以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";
where 子句不仅仅支持 “where 列名 = 值” 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询, 以后还会学到更加高级的条件查询方式, 这里不再多做介绍。
示例:
查询年龄在 21 岁以上的所有人信息: select * from students where age > 21;
查询名字中带有 “王” 字的所有人信息: select * from students where name like "%王%";
查询 id 小于 5 且年龄大于 20 的所有人信息: select * from students where id<5 and age>20;
更新表中的数据
update 语句可用来修改表中的数据, 基本的使用形式为:
update 表名称 set 列名称=新值 where 更新条件;
使用示例:
将 id 为 5 的手机号改为默认的”-“:update students set tel=default where id=5;
将所有人的年龄增加 1: update students set age=age+1;
将手机号为 13288097888 的姓名改为 “张伟鹏”, 年龄改为 19:update students set name="张伟鹏", age=19 where tel="13288097888";
删除表中的数据
delete 语句用于删除表中的数据, 基本用法为:
delete from 表名称 where 删除条件;
使用示例:
删除 id 为 2 的行: delete from students where id=2;
删除所有年龄小于 21 岁的数据: delete from students where age<20;
删除表中的所有数据: delete from students;
创建后表的修改
alter table 语句用于创建后对表的修改, 基础用法如下:
添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];
示例:
在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
修改列
基本形式: alter table 表名 change 列名称 列新名称 新数据类型;
示例:
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";
将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
删除列
基本形式: alter table 表名 drop 列名称;
示例:
删除 birthday 列: alter table students drop birthday;
重命名表
基本形式: alter table 表名 rename 新表名;
示例:
重命名 students 表为 workmates: alter table students rename workmates;
删除整张表
基本形式: drop table 表名;
示例: 删除 workmates 表: drop table workmates;
删除整个数据库
基本形式: drop database 数据库名;
示例: 删除 samp_db 数据库: drop database samp_db;