select

--use School
--insert 往表里插入数据
--insert table_name(colum1 int, colum2) values(value1 1,2,3,value2)
--declare @i int--定义变量的关键字,在同一个作用域是不能重复的
/*select * from student
insert student(stu_no,stu_name,class_id,sex,liking)values(3,'王五',1,'男','篮球')
--加字段
alter table student
add remark varchar(8) constraint uq_liking unique(liking) */
declare @i int
select @i=6
--go--标识 go 上下属于不同代码区 把上下的代码分开
--变量 是临时保存数据用的 是在本代码块通过declare来定义,通过select来赋值的。
--insert student select @i,'赵六',1,'女','羽毛球'
insert student values (@i,'田七',1,'男','')
--select
--select 用来对变量来赋值
--是查询数据关键
--select 的语法
--select colum1,column2 from table_name where tiaojian1 and/or tiaojian2
select * from student where liking =null or stu_no=2--null不能用等号判断,要用 is
select * from student where liking is null or stu_no=2
--liking 字段 如何是null 或者是''
select * from student where isnull(liking,'')='' --isnull(参数1,参数2),判断参数1是否为NULL,如果是,返回参数2,否则返回参数1。如果有表中有null 则显示null和''的行、
select stu_no,liking, cast('' as varchar(200))as decration into liking1 from student--新建一个liking1的表,要用的student表中的的字段和数据,''表示liking1中新增的列的数据内容,并且以为括号外的as后的decration命名字段名。
select * from liking

cast() 函数用法
Cast(字段名 as 转换的类型 ),如 select *,cast(stu_no as date) as stu_no from student 是查询时希望把stu_no转换为date类型
其中类型可以为:
CHAR[(N)] 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int
TIME 时间型

select * cast(''as 字段类型)as 字段名称 into new_table from table
select *,cast(stu_no as date) as stu_no from student

select * from sysobjects where xtype='PK'--查当前数据库有哪些表。U是查询表 P 是存储过程 V是视图 F是外键 PK是主键
select object_id('liking') 查询表的ID,参数是字符串的,表要用引号引起来。它是唯一的在这个数据库里。
select * from syscolumns where ID=OBJECT_ID('liking')--表示查询表的字段信息 object_id 取表的id值 length表示字段长度, xtype表示字段类型的id int是56 varchar
in not
查询学号
select * from student where stu_no not in (1,2,4,6,10)

posted @ 2019-08-14 15:57  Abby_Bing  阅读(371)  评论(0编辑  收藏  举报