当前位置:Gxlcms > 数据库问题 > 【12】简单SQL语句

【12】简单SQL语句

时间:2021-07-01 10:21:17 帮助过:3人阅读

------------------------------------------ -- SQL语句 -------------------------------------------- -- 数据定义语言(DDL) -- 数据操作语言(DML) -- 数据控制语言(DCL) -- 简单的增删改查(CRUD) -- 增加数据 -- insert into 表名(字段名, 字段名, ...) values(值1, 值2, ...); insert into ConstraintExercise.StuInfo2 (stuId, stuName, stuAge, stuSex, courseId) values -- (1, N‘赵晓虎‘, 30, ‘m‘, null); (1, N赵晓虎, 30, m, NULL); -- 查询数据(简写) -- select 字段1, 字段2, ... from 表名; select * from ConstraintExercise.StuInfo2; insert into [ConstraintExercise].[StuInfo2] (stuId, stuName, stuAge, stuSex, courseId) values (2, 牛亮亮, 35, f, NULL); -- 修改数据 -- update 表名 set 字段=值,字段=值,... where 条件; update ConstraintExercise.StuInfo2 set stuSex=m where stuId=2; update ConstraintExercise.StuInfo2 set stuName=牛亮亮 where stuId=2; update ConstraintExercise.StuInfo2 set stuName=NNULL; update ConstraintExercise.StuInfo2 set stuName=NULL; alter table ConstraintExercise.StuInfo2 drop constraint UQ_StuInfo2_stuName; -- 删除数据 -- delete from 表 where 条件; delete from ConstraintExercise.StuInfo2; -- 二进制截断 insert into ConstraintExercise.StuInfo2(stuId, stuName) values(3, N0123456789); select * from ConstraintExercise.StuInfo2 -- 删除 -- delete from 表 where 。。。 -- 删除数据库对象 --drop table 表 --drop database 数据库 --drop view 视图 --drop schema 架构 ------------------------------------------- -- 查询 ------------------------------------------- -- 查询的基本结构(烂熟) -- 查询的详细步骤(烂熟) -- 案例(举一反三) -- 标准的SQL语句 select top | distinct 字段 , 表达式 , 函数 , 标量子查询 , 常量 from 数据源 where 基本筛选 group by 分组字段 having 二次筛选 order by 排序字段依据; -- SQL语句的执行顺序 -- -- 获得数据源(from) -- 进行第一次筛选(where) -- 对筛选得到的结果进行分组(group by) -- 可以对分组后的结果进行再删选一次(having) -- 对所有数据进行整理(select) -- 对结果进行排序(order by) -- 添加一个分数表 go create schema Exercise authorization dbo; go create table Exercise.ScoreTbl ( scoreId int identity(1,1) not null primary key, stuId int not null, scoreNum int check(scoreNum>=0 and scoreNum<=100), scoreLast int check(scoreLast>=0 and scoreLast<=100), -- foreign key(stuId) references Exercise.StudentTbl(stuId) ); -- 添加分数数据 2008+ insert into Exercise.ScoreTbl(stuId, scoreNum, scoreLast) values(1,60,55),(2,75,40),(3,95,85),(5,86,75),(6,90,95); insert into Exercise.ScoreTbl(stuId, scoreNum, scoreLast) values(7, 45, 99) select * from Exercise.ScoreTbl; select * , scoreNum * .3 + scoreLast * .7 from Exercise.ScoreTbl where scoreNum * .3 + scoreLast * .7 >= 60 and scoreNum < 60; -- 找姓牛的人 -- 单字符匹配 _ -- 多字符匹配 % -- stuName like ‘牛%‘ -- 引用对象的方式可以是 -- 服务器.数据库.架构.表 select * from TestDataBase..Student; select * from TestDataBase..Course; select * from TestDataBase..Score; -- 叫纪明X select * from TestDataBase..Student where stuName like 纪明%; insert into TestDataBase..Student (stuName, stuSex, stuBirthdate, stuStudydate, stuAddress, stuEmail, stuPhone, classId) values (纪明闪闪, m, 1990-1-1 00:00:00, 2014-7-7 17:04:52.123, N上帝细节128号, jmss@jmss.com, 12345678909, 2); ----------------------- -- [] [a-z] [^a-z] -- stuName like ‘杨[中重]科‘ -- 如果要匹配 asp_net -- bookName like ‘asp[_]net‘ -- stuName like ‘%虎%‘ -- age 在 19 到 26 岁 -- datediff(year, 开始的时间, 结束的时间) select datediff(YEAR, stuBirthdate, CURRENT_TIMESTAMP),* from TestDataBase..Student where stuSex = f and -- datediff(YEAR, stuBirthdate, CURRENT_TIMESTAMP) between 19 and 26; datediff(YEAR, stuBirthdate, CURRENT_TIMESTAMP) in (19,26,23) ------------------------- -- 空值处理 ------------------------- select * from ConstraintExercise.StuInfo2; select * from ConstraintExercise.StuInfo2 where stuAge <> null; -- SQL Server 采用三值逻辑 真 假 不知道 -- 判断为空使用 is null 或 is not null 再或 not(... is null) select * from ConstraintExercise.StuInfo2 where stuAge is not null; -- isnull(字段, 数据) select *, isnull(stuAge, -1) from ConstraintExercise.StuInfo2;

 

【12】简单SQL语句

标签:group   sql   执行顺序   com   ima   net   set   let   sch   

人气教程排行