找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手
1.创建一个数据库: create database +库名    例子:创建一个db1数据库 create database db1
2.显示有哪些数据库:show databases
3.进入某个数据库  use +库名  例子:进入db1数据库 use db1
4.删除某个数据库 drop databases +库名  例子: 删除db1数据库 drop databases db1
5.创建一个表 create table +表名(字段1名称,数据类型,约束,字段2名称,数据类型,约束,字段3名称,数据类型,约束,)    例如:创建一张学生表student,学号id,姓名name。 create table student(id int(20) primary key,nam varchar(30);

主键约束 primary key
外键约束 foreign key
非空约束 not null
默认值约束 default
自增长约束 auto_increment

修改表结构
1.查看表结构 desc +表名
2.修改表名 alter table +表名 rename +新表名
3.修改表字段 alter table +表名 change +原字段名 +新字段名 数据类型,约束。
4.添加表字段,并放到第一个字段前 alter table +表名 add +字段名 数据类型 约束first
5.添加表字段,并放到某个字段后 alter table +表名 add +字段名 数据类型 约束 after +字段名
6.同时添加两个字段,默认添加到字段最后 alter table +表名 add(字段1 数据类型,字段2 数据类型)
7.删除表字段  alter table +表名 drop +字段
8.删除表两个字段 alter table +表名 drop +字段1drop +字段2
9修改主键id为自增长 alter table +表名 change +字段名*2 数据类型auto_increment
10.删除表 drop table +表名

1.查询表中所有字段的数据 select * from +表名 “ * ”代表所有
2.查询表中所有的name    select name from +表名
3.查询表中name和math的数据 select  name,math  from +表名
4.查询满足某个条件的数据 select * from student where class=1833 查询班级为1833班的所有信息  where后面接满足条件
5.查询不满足某个条件的所有数据 select * from +表名 where 字段! =value
select * from +表名 where class !=1833 !=1833表示不等于,查询班级不为1833班的信息
select * from +表名 where class <>1833 <>这个也表示不等于,查询班级不为1833班的信息
6.查询班级为1833班和1834班的信息 select * from +表名 where class=1833 or class=1834  
7.  select *  from +表名 where 字段 between m and n  
  select *  from student where age between 22 and 27  查询年龄在22到27之间的学生信息,包含22和27
8.select *  from student where age >=22 and age>=27 查询年龄大于等于22和小于等于27之间的学生信息,其作用等同于 between and
9.select *  from student where age >22 and age>27  查询年龄大于22和小于27之间的学生信息,不包含22和27.

10.select *  from student where class is not null 查询班级不为空的学生信息,这里的空是空属性
     select *  from student where class  null 查询班级为空的学生信息
11.select *  from student where name like '%ao%'  查询姓名中间含ao学生信息
12.select *  from student where where id limit 3,5   查询4到8行的数据,3指的是4-1=3,5指的的8-3=5
13.select *  from student where id limit 8    limit 后面接一个数字,表示取前几行,这里取前8行数据
14.select *  from student  order by age asc   将年龄按照从小到大的顺序排列
15.select *  from student  order by age desc  将年龄按照从大到小的顺序排列
16.select *  from student  +字段 where in (1,2)查询字段in或and 括号内需同时满足两个条件才能查询
17.select *  from student  +字段 where not in (1,2) 查询字段不满足括号内两个条件才能查询
18.select  count(class) from student  统计class字段的行数
19.select count(*)  from student *表示所有字段,统计class字段总共有多少行
20.select  sum(class) from student  查询表中数学成绩总和
21.select  avg(math) from student   查询平均数学成绩
22.select  select(math) from student 查询最大数学成绩是多少
23.select  min(math) from student  查询最小的数学成绩是多少
24.select distinct(sex) from student  对sex字段进行去重
25.select class,sum(math) from student group by class  查询每个班级的数学总分 group by 分类
26.select class,sum(math) from student group by class having sum(math)>100 查询每个班级数学总分大于100分的班级和总分数,
27.select class,sum(math)as s from student group by class having s>100 为聚合函数查询出来的字段取别名后面的条件可以使用别名
28.select class,sum(math)m from student sex=1 group by class having m>100 查询每个班级性别为1数学总分大于100的班级和总分数
29.select  class,min(math) from student group by class 查询每个班级数学最小成绩
30.select  class,max(math)  from student  where sex=0  group by class 查询每个班级性别为0的数学最高分数。
[img=620,0]C:\Users\Administrator\AppData\Local\YNote\data\weixinobU7Vji8XhfyEo49O9Lcd2vgBUbY\1c4e4a14259e47d0b2f7d48518ebb276\9e[kslk[gab7@p_wyz0)~4r.png[/img]

31.create table student1 like student   创建一个student1表,表结构和student一样,这里相当于备份表的意思
32.insert into student select * from student  往studen1里面插入数据,插入的数据来源于从student表查询出来的所有数据
33.create table student2 like student 创建一个student2表,用于验证student2表插入部分字段的数据
34.insert into student2(id,name) select id,name from student 往student2表插入id和name字段的数据,数据来源于从student表查询出来的id和name 字段对应的数据
35. mysqldump -uroot -p123456 dcs8>/dcs8.sql  在linux指令操作界面将dcs8数据库进行备份,备份的sql脚本文件放在根目录下,命名为dcs8.sql 脚本名是自定义的
36.cleate databases dcs8_back    备份后接下来是还原数据库,还原前先进入数据库交互界面创建空的数据库dcs8_back
37.mysql -uroot -p123456 dcs8_back</dcs8.sql    创建好空库后,回到Linux指令操作界面进行还原数据库,将aql脚本的数据还原到dcs8_back空库里面


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册