8月19日

use wy;
/*-----------其它转换函数-------------*/
/*类型转换:cast()和convert()*/
update T_Person set FBirthday=null where FName='Kerry';
update T_Person set FBirthday=null,FRegDay=null where FName='Smith';
/******空值处理*****/
/*coalesce()函数:coalesce(表达式,values1,values2,values3,....,valuen)*/
select FName,FBirthday,FRegDay ,coalesce(FBirthDay,FRegDay,'2008-08-08') from T_Person;
/*MySql:ifnull()函数,Sql Sever:isnull()函数*/
select FBirthDay,FRegDay,isnull(FBirthDay,FRegDay) from T_Person;
/*nullif()函数,nullif(表达式1,表达式2)*/
select FBirthDay,FRegDay,nullif(FBirthDay,FRegDay) from T_Person;

/****************case()函数**********************/
/*用法1:case 表达式
  when value1 then 返回值1
  when value2 then 返回值2
  when value3 then 返回值3
  when value4 then 返回值4
         ....
  else 默认值返回值
  end
*/
select FName,(case FName
  when 'Tom' then 'GoodBoy'
  when 'Lily' then 'GoodBoy'
  when 'Sam' then 'BadBoy'
  when 'Kerry' then 'BadBoy'
  else 'Normal'  
  end) as isgood
 from T_Person;


/*用法2:case
  when 条件1 then 返回值1
  when 条件2 then 返回值2
  when 条件3 then 返回值3
  when 条件4 then 返回值4
         ....
  else 默认值返回值
  end
*/

select FName,FWeight,(case
   when FWeight<40 then 'thin'
   when FWeight>50 then 'fat'
   else 'ok'
   end) as isnormal
  from T_Person;


/********************第六章、索引与约束*****************************/
create table T_Person(FNumber varchar(20),FName varchar(20),FAge int);
/*1、索引*/
/*创建索引的SQL语法:create inde 索引名 on 表名(字段1,字段2,....,字段n)
索引名必需唯一,字段允许一个到多个*/
create index idx_person_name on T_Person(FName);
create index idx_person_message on T_Person(FName,FAge);
/*删除索引:drop index.但是各个数据库的具体语法不一样*/
drop index T_Person.idx_person_name;
drop index T_Person.idx_person_message;
/*索引删除完才可以删除表*/
drop table T_Person;

/*2、约束*/
/*约束分类:非空约束、唯一约束、check约束、外键约束、主键约束*/
/*非空约束:not null*/
/*唯一约束:unique*/
/*在创建好的表上添加新的唯一约束,这时就需要使用alter table语句:
alter table 表名 add constraint 唯一约束名 unique(字段1,字段2,....,字段n)*/
/*check约束*/
create table T_Person(FNumber varchar(20),FName varchar(20),FAge int check(FAge>0),
FWorkYear int check(FWorkYear>0));
/*check约束中使用常量表达式外还可以在check约束中使用函数*/
create table T_Person(FNumber varchar(20) check(len(FNumber)>12),FName varchar(20),FAge int check(FAge>0),
FWorkYear int check(FWorkYear>0));/*注意这种方式中定义的check子句中是不能引用其它列的,如果希望在check子句的条件语句中
使用其它列,则必需在create table语句的末尾使用constraint关键词定义它,语法为constraint 约束名 check(约束条件)*/
create table T_Person(FNumber varchar(20),FName varchar(20),FAge int, FWorkYear int, constraint ck_1 check(FWorkYear>FAge));
/*alter table语句向已建好表上增加约束条件*/
alter table T_Person add constraint ck_2 check(FAge>24);
/*删除约束条件*/
alter table T_Person drop constraint ck_2;

/*主键约束:primary key*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值