最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
mysql数据库基本操作学习笔记
时间:2013-04-08 编辑:简简单单 来源:一聚教程网
以下以数据库”ceshi”为例
1、连接数据库
代码如下 | 复制代码 |
mysql -u username -p password |
2、创建/删除数据库
代码如下 | 复制代码 |
创建:create database ceshi; 删除:drop database ceshi; |
3、创建/删除数据表
创建:
代码如下 | 复制代码 |
create table students (sid int(10) auto_increment primary key,name varchar(255),course varchar(255),score int(10)) ; |
删除:
代码如下 | 复制代码 |
drop table students; |
设置数据表编码:
代码如下 | 复制代码 |
alter table `students` default character SET utf8 collate utf8_general_ci; |
4、插入数据
代码如下 | 复制代码 |
•单条插入 :insert into students (name,course,score) values(value1,value2,value3); |
•从另外的一张表中读取多条数据添加到新表中:
代码如下 | 复制代码 |
insert into students(col1,col2,col3) select a,b,c from tableA ; |
•从其他的多张表中读取数据添加到新表中:
代码如下 | 复制代码 |
insert ioto tableName(col1,col2,col3) select a,b,c from tableA where a=1 union all select a,b,c from tableB where a=2 |
上边代码中的union all如果换成union,则相同记录只插入一次,不会重复插入。
•上边代码中的into都可以省略!
5、order by语句
代码如下 | 复制代码 |
select * from students order by score (asc); 从低往高排,默认,asc可省去 select * from students order by score desc; 从高往低排 |
6、group by语句
代码如下 | 复制代码 |
select * from students group by course; 查询数据按课程分组,只显示查询到的第一条 select * from students group by course order by socre; order by |
必须在 group by之后,group by 比order by先执行,order by不会对group by 内部进行排序,如果group by后只有一条记录,那么order by 将无效。要查出group by中最大的或最小的某一字段使用 max或min函数。
--查看学生表的全部数据
代码如下 | 复制代码 |
select * from studio |
--插入一个新的学生信息
代码如下 | 复制代码 |
insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黄兰淇",0,36,'南充','13943943334') |
--查看class全部数据
代码如下 | 复制代码 |
select * from class |
--向class表增加两条条数据
代码如下 | 复制代码 |
insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新电实训班','GXA-ncs-001','2008-03-11','都是很优秀的朋友') insert into class(cl_class,cl_coding,cl_o_time) values('阿坝师专实训班','GXA-ABSZ-001','2008-03-11') |
--更新一条的数据 条件的重要性
代码如下 | 复制代码 |
update class set cl_remark='真的是不错' where cl_id=5 |
--删除一条数据 条件的重要性
代码如下 | 复制代码 |
delete from class where cl_id=7 |
--修改列标题
代码如下 | 复制代码 |
select cl_id as '班级主键',cl_class as '班级名称' from class select 名字=st_name from studio |
--使用文字串
代码如下 | 复制代码 |
select '名字是:',st_name from studio |
--=============条件稍微复杂点的查增删改==============
代码如下 | 复制代码 |
--主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null --查询cl_id 大于 1 的所有信息 select * from class where cl_id>1 --使用 or select * from class where cl_id<>10 or cl_class='百杰一班' --使用and select * from class where cl_id<>10 and cl_class='百杰一班' --使用like 和 % select * from class where cl_class like '百杰%' select * from class where cl_remark like '%上午%' --使用 between select * from class where cl_id between 3 and 5 --使用 between 配合上 not select * from class where cl_id not between 3 and 5 --使用 is not null select * from class where cl_remark is not null --使用 in select * from class where cl_class in('千星一班','百杰二班')
|
--=================使用数学运算符======================
代码如下 | 复制代码 |
--查询Java相关课程分别要上多少周 按照每周5天,每天6节课来计算 select '结果'=co_num/5/6 from course where co_name in ('Java基础','Java项目入门') --==================使用汇总函数 ======================== --涉及到COUNT SUM AVG MAX MIN --查询课时数小于50的课程一共有多少门 select count(*) from course where co_num<50 --查询所有课程一共多少课时 select sum(co_num) from course --计算全部课时费,假设每节课50块钱 select sum(co_num)*50 from course --查询课时最少的课程 select min(co_num) from course --查询课时最多的课程 select max(co_num) from course --查询平均每门课多少课时 select avg(co_num) from course
|
--=================使用数学函数=============================
--包括求绝对值函数ABS函数、求圆周率函数PI()、求正玄值SIN()函数、求指数函数EXP()等。
代码如下 | 复制代码 |
--查询每门课的正弦值 select sin(co_num) from course --查询每门课的绝对值 select abs(co_num) from course www.111com.net --查询每门课课时数 乘以 圆周率 ,具体有什么用我也不知道,反正这好像绝对是8.5杆子都打不到的 select pi()*co_num from course --查询每门课的指数 select exp(co_num) from course --随机返回5个随机生成的数(返回的是0~1之间的随机float值) declare @i tinyint set @i=1 while @i<=5 begin select rand(@i) as '随机生成的数' , @i as '当前值' set @i=@i+1 end --返回数字表达式并四舍五入为指定的长度或精度 - ROUND select round(345.456,-1) as '参数为-1' , round(345.456,-2,1) as '参数为-2' , round(345.456,0) as '参数为0' , round(345.456,1) as '参数为1' , round(345.456,2) as '参数为2' --================使用日期函数====================== --DAY()、MONTH()、YEAR()——返回指定日期的天数、月数、年数; select day(cl_s_time) as '日' from class --返回天 select '月'=month(cl_s_time) from class --返回月 select '年'=year(cl_s_time) from class --返回年 --DATEADD(datepart,number,date)——在日期上增加给定日期类型的数量; select dateadd(yyyy,4,cl_s_time) as '增加4年后' from class --datepart - 年份 yy、yyyy select dateadd(q,2,cl_s_time) as '增加2季度后' from class --datepart - 季度 qq、q select dateadd(mm,3,cl_s_time) as '增加3月度后' from class --datepart - 月份 |
mm、m
--datepart - 每年的某一日
dy、y
--datepart - 日期
dd、d
--datepart - 星期
wk、ww
--datepart - 小时
hh
--datepart - 分钟
mi、n
--datepart - 秒
ss、s
--datepart - 毫秒 www.111com.net
ms
--DATEDIFF(datepart,date1,date2)——获取两个日期之间给定的日期类型的数量差(整个函数结果是date2-date1);
select datediff(mm,cl_s_time,cl_o_time) as '共持续月' from class
--datepart(datepart,date)——在给定日期基础上返回指定日期类型的值(整数);
--其实这个等同于DAY、MONTH、和 YEAR 函数
select datepart(dd,cl_s_time) as '日期' from class
--GETDATE()——返回当前日期和时间。我们在设计数据库的时候,通常也可能把他作为默认值
相关文章
- mysql数据库入门学习笔记 09-21
- mysql数据库分组(GROUP BY)学习笔记 12-03
- mysql数据库入门教程之学习笔记 10-17
- mysql数据库学习笔记 12-30
- MySQL登录、访问及退出操作解析 10-18
- sql语句 update字段null不能用is null问题解析 09-28