mysql数据库、数据表、数据基本操作

  1 连接数据库     mysql -h 主机地址 -u用户名 -p 密码
  2 查看数据库   show databases;
  3 查看版本号 select version();
  4 查看当前数据库  select database();
  5 查看现有的数据表  show tables;
  6 查看数据表结构   desc 表名;
  7 查看数据表创建语句   show create table table_name;
  8 查询数据   select * from 表名;
  9 查看数据表的前三条数据  select * from tbname limit 3;
 10 查看三条数据,按照指定的字段名的值从小到大排序  select * from tbname order by 字段名 【asc】 limit 3; asc可以省略
 11 查找最后三条数据    select * from 表名 order by id(字段)desc limit 3;   是降序输出的
 12 查看第m至n条数据  select * from 表名 limit m,n;    从m+1条开始, 返回第m+1行到第m+n行记录,m代表开始的下标
 13 
 14 创建数据库   create database [if not exists] dbname;
 15 创建数据表
 16 create table [if not exists] 表名(
 17   字段名1 数据类型 附加选项,
 18   字段名2 数据类型 附加选项,
 19   字段名3 数据类型 附加选项,
 20   ...
 21 );
 22 例如:
 23 create table if not exists student(
 24   id int(11) primary key auto_increment,
 25   name varchar(10) not null,
 26   age tinyint(3) not null,
 27   class varchar(10) not null,
 28   sex char(1) default '女',
 29   mobile char(11) unique
 30 )default charset=utf8;
 31 创建表的时候指定外键:
 32 create table student1 (
 33   id int(11) primary key auto_increment,
 34   name varchar(10) not null unique,
 35   class_id int(11),
 36   constraint foreign key fk_class_id (class_id) references class(id)
 37 );注意:建立外键,主表和子表相关联的两个字段的数据类型以及附加选项都必须一样。
 38 
 39 
 40 添加一个字段      alter table 表名 add 字段名 类型 附加选项;   或者  alter table tbname add column 列名 类型;
 41 添加一列并指定位置  alter table tbname add column 列名 类型 first;  alter table tbname add column 列名 类型 after 列名;
 42 插入数据  insert into tbname(id,字段1,字段2。。。) values(值1,值2,。。。);
 43 添加外键约束   alter table 子表表名 add constraint foreign key 外键名称(当前表的字段) references 主表表名(主表的主键);
 44 
 45 切换数据库 use dbname;
 46 修改表名              alter table 表名 rename [as] 新表名;
 47 修改字段类型         alter tabel 表名 modify 字段名 数据类型 附加选项;
 48 修改字段名和字段类型alter table 表名 change old字段名 new字段名 数据类型 附加选项;
 49 调整字段名顺序:
 50     (1)将id放到第一列      alter table 表名 modify id int(10) auto_increment first;
 51     (2)将name列放到id后面  alter table 表名 modify name varchar(30) after id;
 52 修改数据   update 表名 set 字段名=值, 字段名=值, 字段名=值, …… where 条件语句;
 53             说明:同时可以修改多个字段,之间用逗号隔开。通常需要指定条件,如果不指定条件,则更新全部数据。;    
 54     
 55 删除数据库   drop database [if exists] dbname;
 56 一次删除多个表  drop table tb1,tb2;
 57 清空数据表(id归1)   truncate 表名;
 58 删除字段     alter table 表名 drop [column] 字段名;
 59 删除一行  delete from tbname where id=n;
 60 删除一列  delete from tbname drop column 列名;
 61 删除数据表中的外键   alter table 表名 drop foreign key 外键约束名;
 62 去除某个字段的部分值     update chapter set word_count=replace(word_count,'万','');  
 63 删除数据  delete from 表名 where 条件;   #说明:如果不指定条件,则删除所有到数据。
 64 
 65 where条件语句
 66     where 字段名 [运算符] 值;
 67     = 等于
 68     != 不等于
 69     > 大于
 70     >= 大于或者等于
 71     < 小于
 72     <= 小于或者等于
 73     between x and y  在x和y之间,包括x和y
 74     in (a,b,c,d...) 在括号里任意匹配一个即可
 75 查询结果排序    select 字段列表 from 表名 where 条件 order by 排序字段 asc/desc;  #默认升序  排序字段可以为多个,多个字段之间用逗号隔开。先用第一个字段排序,如果排序出来的结果中有重复的数据,就会对重复的数据再使用第二个字段排序,如果还有重复的再使用第三个字段对重复数据排序,依此类推,直到排出来顺序或者没有排序字段为止。
 76 对于多字段排序,需要给每一个字段指定排序方式
 77 order by 语句必须写在条件语句的后面。
 78 多表连接查询  内连接:
 79     select 字段列表 from 主表 inner join 子表 on 主表.主键 = 子表.外键 where 条件;
 80     查询孙老师教的学生
 81  select student.name, teacher.name from teacher inner join course on teacher.id=course.teacher_id inner join score on score.course_id=course.id inner join student on student.id=score.student_id where teacher.name='孙老师';
 82  子查询
 83 把一个查询语句的结果做为另外一个语句的条件。
 84 例如:查询一年二班的学生
 85 select id from class where name='一年二班'; // 假如是3
 86 select * from student where class_id = 3;
 87 把上面两句合并:
 88 select * from student where class_id=(select id from class where name='一年二班');
 89 查询一年二班和一年三班的学生
 90 select * from student where class_id in (select id from class where name in('一年二班', '一年三班'));
 91 子查询可以用于删查改四种语句。
 92  6. 分组统计 group by 字段名
 93 例如:查询各个班级的总人数
 94 select count(id), class_id from student group by class_id;
 95 一般来说,使用group by分组,应该配合使用聚合函数
 96 7. 分组统计后需要过滤条件
 97 例如:查询学生人数2个以上的班级
 98 select class_id, count(*) from student group by class_id having count(*)>=2;
 99 使用as语句可以给字段名,表名,结果起别称,尤其是在多表查询时。
100     select name as 姓名, sex as 性别, tel as 电话 from student;
101 查询语句中支持运算表达式
102     select 100+10;
103     查询学生年龄,并给年龄+5
104     select age as 原来年龄, age+5 as 年龄加5 from student;
105 使用like进行模糊查询
106     like 像
107     % 表示任意个任意字符
108     _ 表示一个任意字符
109     例如:查询姓张的学生
110     select * from student where name like '张%'; 
111  
112  
113  
114 count() 统计记录条数
115 sum()求字段总和
116 avg()求字段平均数
117 max()求字段最大值
118 min()求字段最小值
119 group by 分组查询        select id,book_name,author,sort,word_count from chapter group by book_name;
120 group by + group_concat()
121     (1) group_concat(字段名)可以作为一个输出字段来使用,
122     (2) 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
123     select sex,group_concat(name) from employee group by sex;  group_concat(name)存放男生的全部姓名 和女生的全部姓名
124 group by + having
125     (1) having 条件表达式:用来分组查询后指定一些条件来输出查询结果
126     (2) having作用和where一样,但having只能用于group by
127     select sex,count(sex) from employee group by sex having count(sex)>2;    #查询的是性别和性别的数量,只显示大于2的
128 group by + with rollup
129     with rollup的作用是:在最后新增一行,来记录当前要查询的count(x)列里所有记录的总和,注意必须和group by 连用
130      select id,book_name,count(chapter_name) from chapter group by book_name with rollup; #最后会计算章节总数
131 distinct 不显示重复的查询结果
132     对于表中一些没有唯一性约束的字段,可能存在重复的值,这时可以使用distinct来消除那些查询结果中的重复值    
133     select distinct book_name from chapter;   #如果有相同的书名,也只返回一个
134 order by 查询结果排序
135 (1) order by 属性名 [desc|asc]
136       这个语句的意义是:对于查询结果,指定根据这个属性来进行升序|降序的排列
137 (2) 排序时对于字段中的NULL值:
138     asc升序时,NULL在最前面
139     desc降序时,NULL在最后面
140     #也就是说如果下面有一行cust_id 为null时,默认他的cust_id是最小的
141     select distinct cust_id from orders order by cust_id desc;
142 union合并查询结果
143     (1) union all: 把所有查询结果合并
144     (2) union     : 把所有查询结果合并且去除重复    
145     select vend_id from vendors union all select vend from products; #把vendors 表里的vend_id 和products表里的vend一起输出,不去重,union去重
146     
147 mysql导出数据和导入
148 在cmd命令行先进到mysql的bin目录下
149 (1)备份整个数据库,包括数据库中所有表:
150 输入 mysqldump -h localhost -u root -p 数据库名 > 文件路径/文件名.sql       备份整个数据库,包括数据库中所有表,恢复时可以恢复其所有的数据表和数据
151 (2)导出指定的一个或多个数据表mysqldump -h localhost -u root -p 数据库名 数据表1 数据表2 > 文件路径/文件名.sql    
152 mysql恢复数据表 
153 a.在数据库里执行恢复的命令
154 use 数据库;
155 source 路径/123.sql
156 b.先建好数据库,在cmd命令行进入MySQL的bin目录   输入  mysql -uroot -p 数据库名 表名 <路径/123.sql
157 
158 
159 
160 编码格式:
161 1.查看数据库编码格式
162 mysql> show variables like 'character_set_database';
163 2.查看数据表的编码格式
164 mysql> show create table <表名>;
165 3.创建数据库时指定数据库的字符集
166 mysql>create database <数据库名> character set utf8;
167 4.创建数据表时指定数据表的编码格式
168 create table tb_books (
169     name varchar(45) not null,
170     price double not null,
171     bookCount int not null,
172     author varchar(45) not null ) default charset = utf8;
173 5.修改数据库的编码格式
174 mysql>alter database <数据库名> character set utf8;
175 6.修改数据表格编码格式
176 mysql>alter table <表名> character set utf8;
177 7.修改字段编码格式
178 mysql>alter table <表名> change <字段名> <字段名> <类型> character set utf8;
179 mysql>alter table user change username username varchar(20) character set utf8 not null;

猜你喜欢

转载自www.cnblogs.com/zpdbkshangshanluoshuo/p/10426867.html