关于mysql的sql语句的汇总(学习笔记)01

来源:互联网 发布:淘宝卖家在哪里拿货 编辑:程序博客网 时间:2024/06/17 13:44
-- 创建数据库    create database gradem
-- 创建表sc    create table sc(    sno char(10) not null,    cno char(5) not null,    degree DECIMAL(4,1) null CHECK(degree>1 and degree<100),    primary key(sno,cno)  -- 这个创建外键是表级约束    )

注意:

注意有小数的情况,decimal这个类型就可以定义变量,如:decimal(4,1)表示:保留一位小数,
精度不同,一个是小数点后面精确4位,一个是精确1位
注意的是decimal(18,4)总长18位,

--修改表-添加外键     alter table sc add FOREIGN key(sno,cno) REFERENCES  student(sno,cno);-- 创建表teacher    create table teacher(    tno char(3) not null PRIMARY key, -- 这个创建外键是列级约束    tname varchar(8) null,    tsex char(2) null,    tbirthday date null,    tdept char(16) null)-- 添加外键                                                     -- 这个是在定义级联操作    alter table sc add FOREIGN key(sno) REFERENCES student(sno) on DELETE CASCADE-- 向学生表添加数据    insert into student(sno,sname,ssex,sbirthday,saddress,sdept,speciality) values('20050101','李勇','男','1987-01-12','山东济南','计算机工程系','计算机应用')-- 更新数据:-- updateset 字段1=新值,字段2=新值,-- where  条件 (可选,不指定where,表中的每个记录都会被更新)    update person set age=15,name1='李明' where id=10-- 指定一个更新的范围    update person set info='student' where id>=4 and id<=9-- delete from 表 将表中的所有数据都删除-- 与drop的区别  关于表的结构,记录都删除了-- DELETE加上where条件,对特定的列进行删除,-- drop table 表 将表删除 然后表的数据和表的结构都不在了-- 在person表中删除ID为10的记录    delete from person where ID=10

– 单表查询

 -- 单表查询    -- select  目标    -- FROM    表    -- WHERE    条件    -- 查询指定列    -- 查询全体学生的学号姓名            select sno,sname            from student            select sno from student    -- 去掉查询结果的重复值(关键字是distinct)            select DISTINCT sno from         -- 2).查询所有列                select * from sc        -- 查询结果的N条记录                            -- 第0条记录后面的十条(是从0开始的,并且limit经常用与分页)        select * from student limit 0,10        -- 3).查询经过计算的列                -- (当前日期的年份-出生日期的年份)                        -- 表中的年份                                select year(sbirthday) from student                        -- 获取当前年份                                 select CURDATE()                        -- 合并执行                        select year(CURDATE())-year(sbirthday) from student                        -- 方便使用者使用 用as加一个别名                        select sname as '姓名',year(CURDATE()) - yesr(sbirthday) as '年龄'        -- 4). 别名 字段名 as 别名        --          字段名     别名(注:可以用“空格”作为as)        -- 5). 经过计算的列                select sno,cno,degree*1.2 as 增加后的成绩 from sc 

对表本身的增删改查

------------------老师讲的知识点(对表本身的增删改查):-------------------------------------- 修改表--   alter table 表 列 列的类型 列的参数    --------------------------     alter table demo_1 add user_name char(20) not null default ''  -- (2 alter tableadd  列 列参数 after 某列 (把新列加在某列后面afteralter table demo_1 add gender char(1) default '' after user_name-- (3 alter tableadd 列 列参数 first (把新列加在最前面)    alter table demo_1 add pid int not null first-- 删除列 drop 列名 -- 1)  alter tabledrop 列名    alter table demo_1 drop pid -- 修改列   modify 列名-- (1 alter table 表 modify 列 新类型 新参数 (修改列的类型)    alter table demo_1 modify gendr char(4) not null default ''-- (2 alter  table 表  change 旧列名 新列名 新类型 新参数    alter table demo_1 modify pid uid int unsigned not null default 0-- (3 改变一个字段的默认值 -- alter tablealter  字段 set defaultalter table demo_1 alter uid set default 2-- 改变字段的类型     alter table demo_1  change user_name varchar(20) -- 向一个表中增加一个列iid做主键    alter table demo_1 add iid int(10) primary key auto_increment -- 的意思是自动增长-- 查询列-- 1desc 表(查询所有列)查询表的字段信息    desc demo_1 -- 有点像右击表  设计表-- 2show COLUMNS from 表( 效果和desc是一样的)    show columns from demo_1 -- 3) show create table 表(查看表的创建    show create table demo_1-------------------------------------------------------------------------------------

– 查询 结果的输出

     -- 1)新建一个表,将查询的结果放在新建的表中                        create table student_new --这里不能添加分号';'                        select sno,sname                        from student;                        -- 查询上一个表的结果, 确定是否来把查询结果放在了一个新建的表中                        select sno,sname                        from student_new;    -- 2)将查询结果输出到文本文件中                        use gradem;                        select *                         from student                        LIMIT 4,3  -- 没有文件就创建一个新的                        into outfile 'f:/1.txt' fields terminated by ',';

– 单表的有条件查询

        -- 查询所有的男生信息                select * from student where ssex='男';        -- 大于,不小于        -- 比较运算符 > ,< , <=,>= !>,!<        -- 范围运算符 :and ,not,or         -- 列表运算符: in,not in (in相当于多个or语句连在一起,作用效果一样)        -- 空值:  is null,is not null          -- 字符匹配符:like,not like ,(通配符:%,_)(正则表达式)                select * from person where degree>=80;                select * from person where degree>=80 and degree>90;        -- 60到90分之间 between and             select * from person where degree between 60 and 90;        -- 1.同时满足要求 or 如下的语句的意思是:查找字段名为classno 满足20070101,20070201,20070301的记录                            select * from person where classno='20070101' or classno='20070201' or classno='20070301';        -- 与上面的1.等价,具有同样的效果 (注:not in 的意思就是排除在外的)                                select * from where in('20070101','20070201','20070301');        -- 判断字段是否为空!                        select * from where degree is not null;        -- 数字型的字符型                                      -- (注意:比较运算符几乎可以匹配所有的数据类型)                                -- 此sno数据类型是字符型  要加''括起来;                                select * from person  where sno>'20070103';             -- 模糊查询(要用like或者not like)                    -- 注:不要用等号不然就是精确查询了                    -- 转义字符:\  ( 注:有时候在有些数据中有_(下划线)的  在查询的时候就需要有到\,让他代表本来的含义)                                select * from student where sname not like '王%';                     -- 正则表示式                                select * from student where regexp '三$';

– 聚合函数的使用 count() sum(列) min(列) avg(l列)

            -- 查询学生表学生的人数                    select '学生的人数',count(*) as 人数                    from student            -- 查询学生所在的班级的个数                    select '班级个数',count(DISTINCT classno)                    from student             -- 查询选修了课程的人数                    select count(DISTINCT sno)                     from sc;            -- 计算机选修了c02课程的学生的平均成绩                    select max(degree) '最高分',min(degree) '最高分'                    from sc                    where cno='c02';            -- 查询指定学生的总分和平均分                    select sum(degree),avg(degree)                    from sc                    where sno='2007010104';            -- 查询没有分配班级的学生的人数                    select '学生人数',count(sno) as '人数'                    from student                    where classno is null;

– 分组和排序

            -- 分组(分类汇总) 查询结果1是分组字段 2是汇总(汇聚函数)            -- 统计男女的人数                    select ssex,count(*) as '人数'                    from student                     GROUP BY ssex            -- 两个表之间的查询                -- 查询各个班没门可的总成绩和平均成绩                        select student.classno,sc.cno,sum(degree),avg(degree)                        from student,sc                        where student.sno=sc.sno                        GROUP BY student.classno,sc.cno;
1 0
原创粉丝点击