//导入练习数据库root为用户名
// 先进入到employees目录下面,输下面的命令
mysql -t -u root -p < employees.sql
登录命令:mysql –h localhost –u root -p<回车>
再输密码:
//---客户端显示数据乱码:
set charset gbk;// 客户端如果是gbk时
数据库相关命令:
1、 Show databases; <显示所有数据库>
2、 create database shool; <创建名为shool的数据库>
3、 use school; <使用school数据库>
4、 show tables; <显示所有表>
-----------------建表命令-------------------------
5、 create table student
(
Id int not null auto_increment primary key, // id自增,不为空,是主键
name VARCHAR(10),
age int(4),
sex varchar(4) default ‘男’,
classid int(4)
);
6、desc student; <查看表结构>
7、drop table student; <删除名为student的表>
8、drop database school; <删除名为school的数据库>
更改表名:
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改为 YouClass
mysql> rename table MyClass to YouClass;
------------------------------修改表结构---------------------------------
相关语句:
------------------------------删除自增--------------------------------
1、 alter table student change id id int(10);//删除自增长
------------------------------删除主键--------------------------------
2、 alter table student drop primary key; // 删除主键
------------------------------ 增加主键(要先删除主键和自增,如果有)---------------------------------
3、 alter table student add primary key(id); // 添加id为主键(只能是一个主键)
< key(id,name) 这样就是设了两个主键了>
------------------------------修改id为自增--------------------------------
4、 alter table tb_name modify id int auto_increment primary key; <修改id为自增主键>
5、 alter table 表名 add constraint 约束的名字 primary key(字段名);
------------------------------添加一个字段--------------------------------
6、 alter table student add classid int(4); -- 添加一个classid的int属性
------------------------------删除一个字段--------------------------------
7、 alter table student drop classid; // 删除一个属性< 有自增主键时,要先删除自增才可以再删除主键 >
------------------------------修改字段名--------------------------------
8、 alter table student change classid class int(4); // 修改属性名称classid->class
------------------------------修改字段类型--------------------------------
9、 alter table student modify class int(10); //修改属性类型
-------表的编码相关--------------------
相关语句:
1、show create table t1; -- 显示表t1建表时的信息包括编码
2、alter table t1 default charset utf8; 把表t1的编码设为utf8
3、show variables like ‘character_set_database’; //查看当前数据库的编码:<先进到某一数据库>
4、alter database school character set utf8; -- 把数据库school的编程设为utf8;
------------------------------增、删、改、查-----------------------------
相关语句:
1、 增加记录insert into
Ex: insert into student (id,name,age,sex,classid) values (1000, ’jack’, 20, ’male’, 1);<value前的括号不写就默认所有属性>
Ex: insert into temp values (1, 3, now()); -- 往temp表里插入当前时间;
2、 删除记录delete from
Ex: delete from student where id = 1000; -- 删除id为1000的记录
Ex: delete from student where name = ‘jack’ and age = 20; <and为且同时满足两个条件,or为或者 >
Ex: delete from student; -- 删除student表的所有记录。
<假设我们要删除users表中name等于"Mike"的前6条记录。可以使用如下的DELETE语句>
Ex: DELETE FROM users WHERE name = 'Mike' LIMIT 6;
<一般MySQL并不确定删除的这6条记录是哪6条,为了更保险,我们可以使用ORDER BY对记录进行排序>
Ex: DELETE FROM users WHERE name = 'Mike' ORDER BY id DESC LIMIT 6;
3、 修改记录update
Ex: update student set age=21 where id=1004; -- 把id为1004的记录的age设成21
Ex: update student set age=21, classid=4; -- 不跟条件,全部改为age=24,classid=4;
4、 查询记录:select … from
Ex: select * from student; -- 查询所有记录
Ex: select id from student; -- 查询studetn表全部的id
Ex: select id, name from student where classid > 1; -- 查询classid大于1的记录的id和name
Ex: select id, name, age+10, from student; -- 查询后age+10
Ex: select * from student where age is null; -- 查询age是空的所有记录
< is not null>
Ex: SELECT * from temp where date between '2016-8-1' and '2016-9-31' – 查该范围内的记录
< not between … and … 排除该范围>
Ex: select * from student where name like ‘%s%’; -- 实现模糊查询 字符要用‘’号包起来
< %表示零个或多个字符 _ 下划线表示一个任意字符 >
--------in子句的使用-----------
1、 in 后面是记录集,如:
Ex: select * from table where uname in(select uname from user);
2、 in 后面是字符串或数字,如:
EX: select * from table where uname in('aaa',bbb','ccc','ddd','eee',ffff'');
Ex: SELECT * from temp where age IN (14,25,18);
3: 实例:
-- 查询目录是带果的, 对应的所有产品
select * from products where categoryID in (select categoryID from categorys where category_name like '%果%')
―――左、右、内连接―――
Ex: SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id; --左连接
<先将左边的表全部显示出来,然后右边的表id与左边表id相同的记录就“拼接”上去>
Ex: SELECT * FROM t2 LEFT JOIN t1 ON t1.id = t2.id; -- 右连接
<右连接,则以右边的表为基础。和左连接相反>
Ex: SELECT * FROM t1 INNER JOIN t2 ON t1.id = t2.id; -- 内连接 < 同等于:select * from t1,t2 where t1.id = t2.id;>
< t1表和t2表id相同的记录全部显示出来>
Ex: SELECT * FROM t1 FULL JOIN t2; -- 全外连接 <同等于:SELECT * from t1,t2;>
< t1表和t2表id相同的记录全部交叉显示出来>
--------------------------------组函数---------------------------------------------
组函数:count() 计数;max() 最大值; min() 最小值; avg() 平均值;sum() 求和;
Ex: select count(id) from student; -- 找出有几条记录
Ex: select max(age) from student; -- 找出最大年年龄
Ex: select * from student where age = ( select max(age) from student ); < 子查询做为条件>
//----排序
Ex: select * from student order by classid asc; -- 按classid升序排例
Ex: select * from student order by classid desc; -- 按classid降序排例
//----分组
Ex: select classid, avg(age) from student group by classid; -- 查询各班的平均年龄,按班级分组
-------------------------------Having的用法-------------------------------------
mysql中,当我们用到聚合函数,如sum,count , max等后,又需要筛选条件时,having就派上用场了,因为WHERE是在聚合前筛选记录的,having和group by是组合着用的,如:
(根据cid分组,分组统计每组商品的平均价格,并且平均价格大于20000元)
//--查询总结
select 一般在的后面的内容都是要查询的字段
from 要查询到表
where
group by
having 分组后带有条件只能使用having
order by 它必须放到最后面
//---2表联查
Ex: select s.* , t.* from student s , teacher t where s.classid = 1 and t.classid = 1; < 多表联查,要起别名 >
―――分页―――
Ex: SELECT * from temp limit 0,3; -- limit 0,3 < 0表示忽略几条记录,3表示一次显示几个>
< limit的两个值,limit (page-1)*size , size >