SQL 基础笔记

1、

/*将数据人一张表复制插入到另一张已存在的表*/  (说明:表在插入数据之前表必须要 已存在)  

insert into btable (id, name , age)
select id, name,age 
from atable

 

2、

/*从A (employee )表 复制数据到B (employee)表   */        (说明:在插入数据时会 新建表)

insert into   

e_employee(e_id,e_id_e,e_id_f )                                         //e_employee 为新建的一张空表 ,e_id,e_id_e,e_id_f为空表里的字段

select id,e_id8,e_id14                                                           // id , e_id8,e_id14为 employee表里的字段 

from employee                                                                     //从employee表里取数据

 

/*oracle自带的语法  as    插入并创建新表 */

create table  new_employee as select employee.e_name, employee.e_sex from employee   

3

/*sqlserver  Union合并数据进行插入 */    
create table atable (id int , name varchar(25), age int)       //创建一张表
select * from atable                                                             //查询新创建的表      

insert atable (id, name,age)                                                //插入想要插入的数据
select 1,'lijian',26 union
select 2,'JimLee',25 

select * from atable

select employee.id,employee.name,employee.pwd,employee.id_card,
score.id,score.name,score.score,score.emp_id,department.name

/*oracle  插入多行数据*/

insert into 表名(字段1,字段2) 

select '一','二' from dual         
union all                                 
select '三','四' from dual         
union all                                 
select '五','六' from dual         
union all                                 
select '七','八' from dual         
union all                                 
select '九','十' from dual


4

--增加表多虽然相等,两个相等的数查询出来查是丙个数都在相乘
from employee,score ,department
where
employee.name = score.name and
employee.id = department.id

 

5、

-- left outer join on   左表所有列都会显示,以及右表相等的也会重复列出,所匹配的后面的值跑到前面去了说明没顺序
-- right outer join on 结果发现, 最后只显示最右边表的信息 , 没有与右边表相等的则显示为null
-- join on                  只会显示多张表查询出来的数据完全对等的
-- full outer join on  查询了多张表相等的联合查询秀出来,不相等的也显示出来

-- 实验灵活用法
select distinct e.id, e.name,e.pwd,e.id_card,s.name,avg(s.score)as score,sum(s.emp_id)empid ,d.name
from employee as e
--right Outer
join  score as s on e.name = s.name
--right outer
join department as d on s.id = d.id
where e.id is not null
group by e.id,e.name,e.pwd,e.id_card,s.name,d.name

 

6、/*约束*/

/*随意新建两个实验表*/

create table table2(
id nummber ,
name varchar(25),
age int
)

--创建一个外键表
create table table3(
fkid number,
score varchar2(25)
)

--主键 (不能重复, 不能为空)
alter table table2
add constraint PK_ID PRIMARY KEY (ID)


--唯一约束 (不能重复,可以为空)
alter table table2
add constraint UQ_NAME UNIQUE (ID)


--sqlserver默认约束            
alter table table2                                                                                                  --sqlserver中的默认约束 在oracle不能这样使用
add constraint DF_AGE DEFAULT(18)FOR AGE

 

// ORACLE中的默认约束

alter table tableTest modify (AGE int default 18)                                                 --oracle默认约束

insert into tableTest (id,name,age,dateT)values (5,'JimLee',default,sysdate)    --插入默认值


--检查约束
alter table table2
add constraint CK_AGE CHECK (AGE BETWEEN 15 AND 90)

--添加外键约束
alter table table3
add constraint fk_id
foreign key (fkid) references table2(id)

 --SQLSERVER 查询存在的表
if exists (select * from sysobjects where name ='employee')

--case when 的用法 --下面的实例在oracle中效果不一样
update score
set name=case
when score =null then '缺考'
else convert(varchar(5),name)
end

 

-- sqlserver的变量查询

select * from score
declare @n int
while(1=1)--条件永远成立
begin
select @n=count(*) from score where score<60
if(@n>0)
update score set score=score+3
else
break
end
print '加分后成绩如下'
select * from score
   

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值