sql常用命令
1.建表:
create table 表名(字段设定列表);
例:create table student
(stuid char(10) primary key,
name char(20),
grade int(3),
age int(3)
);
2.删表:
drop table 表名;
例:drop table student;
3.将表中记录清空:
delete from 表名;
例:delete from student;
4.显示表中的记录:
select * from 表名;
例:select * from student;
5.给表改名:
rename table 旧表名 to 新表名;
例:rename table student to people;
6.修改字段属性:
(1)alter table 表名 change 字段名称 字段名称 字段类型 [是否允许非空];
例:alter table student change name newname char(20) null;
(2)alter table 表名称 modify 字段名称 字段类型 [是否允许非空];
例:alter table student modify name char(20) null;
7.修改表设置默认字段:
(1)alter table 表名 modify 字段名称 字段类型 default 默认值;
例:alter table student modify name char(10) defalt 7;
(2)alter table 表名 alter 字段名称 set default value;
例:alter table student alter name set default 7;
8.增加表的一个字段:
alter table 表名 add column 字段名称 字段类型 (default 默认值);
例:alter table student add column sex char(10) (default 1);
9.删除表的一个字段
alter table 表名 drop column 字段名称;
例:alter table student drop column name;
10.删除表主键
alter table 表名 drop primary key;
例:alter table student drop primary key;
11.添加新主键:
alter table 表名 add primary key(字段);
例:alter table student add primary key(stuid);
12.往表里插入一行数据:
insert into 表名 values(字段数据1,字段数据2,•••)
例:insert into student values(‘123’,‘qqqq’,‘80’);
13.往表里插入多行数据:
insert into 表名 values(字段数据1,字段数据2,•••) ,(字段数据1,字段数据2,•••),•••;
例:insert into student values(‘123’,‘qqqq’,‘80’),(‘124’,‘yyyy’,‘90’);
14. 修改表的数据:
update 表名 set 字段名=value where 范围
例:update student set name=‘qqq1111’ where stuid=‘123’;
15.模糊查询
select * from 表名 where 字段 like ’%value1%’;
例:select * from student where name like ‘q%’;
16.排序查询:
select * from 表名 order by 字段名1,字段名2 [desc]
例:selec * from student order by grade;(升序)
select * from student order by grade desc;(降序)
17.左连接查询:
select 表1.字段1,表1.字段2,表2.字段1,表2.字段2 from 表1 left (outer) join 表2 on 表1.字段=表2.字段;
例:select student.num,student.name,people.name,people.age from student left (outer) join people on student.name=people.name;
18.右连接查询:
select 表1.字段1,表1.字段2,表2.字段1,表2.字段2 from 表1 right (outer) join 表2 on 表1.字段=表2.字段;
例:select student.num,student.name,people.name,people.age from student right (outer) join people on student.name=people.name;
19.全连接查询(mySql不支持全连接,所以用左连接union右连接)
select 表1.*,表2.* from 表1 left (outer) join 表2 on 表1.字段=表2.字段 union select表1.*,表2.* from 表1 right (outer) join 表2 on 表1.字段=表2.字段;
例:select s.*,p.* from student s left join people p on s.name = p.name union select s.*,p.* from student s right join people p on s.name = p.name;
20.关于年份的查询
例:查询在1990-1993年之间出生的学生的全部信息
select * from student where year(birthday)between 1990 and 1993;
查询在1990年12月5日之前出生的学生的全部信息
select * from student where birthday < date(‘1990-12-05’);
备份与还原
1.备份数据库:
mysqldump –u 用户名 –p 数据库名 > 保存路径+文件名;
例:mysqldump –u root –p yingyu > /home/yingyu/yingyu.sql;
2.还原数据库:
mysql –u 用户名 –p 数据库名 < 文件路径+文件名;
例:mysql –u root –p yingyu < /home/yingyu/yingyu.sql;
3.直接将MySQL数据库压缩备份
mysqldump –u 用户名 –p 数据库名 | gzip > 保存路径+文件名
例:mysqldump –u root –p yingyu | gzip > /home/yingyu/yingyu.sql.gz;
4.还原压缩的Mysql数据库
gunzip < 文件路径+文件名 | mysql –u 用户名 –p 数据库名
例:gunzip < /home/yingyu/yingyu.sql.gz | mysql –u root –p yingyu;
5.备份数据库中的某些表:
mysqldump –u 用户名 –p 数据库名 表名1 表名2 > 保存路径+文件名
例:mysqldump –u root –p yingyu student > /home/yingyu/yingyu.sql;
6.备份数据库中的某些数据库:
mysqldump –u 用户名 –p –B 库1 库2 > 保存路径+文件名
例:mysqldump –u root –p –B yingyu1 yingyu2>/home/yingyu/yingyu.sql;
7.还原数据库中的某些数据库:
mysqldump –u 用户名 –p –D 库1 库2 < 文件路径 + 文件名;
例:mysqldump–u root –p–D qiuyingyu yingyu</home/yingyu/yingyu.sql;
8.还原数据库中的某些表:
mysql –u 用户名 –p 数据库名 < 保存路径+表文件名
例:mysql –u root –p yingyu < /home/yingyu/yingyu.sql;