oracle DML語句的學習(1 )

DML語句
(insert,update ,delete,select)

--插入語句
select * from student;
--單獨的插入一條語句
insert into student(sno,sname,sex,address,cardid)
values(2,'張三','男','長沙','12313')

update student set sex='女'

rollback;

--插入的時候要注意的地方
(1)要插入的列名的個數必須和值的個數匹配
(2)當有約束的情況下面要考慮約束(check,default,foreign key,unique)
 (3) 數據類型(考慮完整性約束(實體完整性,域完整性,引用完整性,自定義完整性))
(4)在SQL server裏面,當有標識列存在的情況下,我們不能顯示的給這個標識列插入值
 
--考慮默認約束的情況下面
insert into student(sno,sname,sex,cardid)
values(2,'張三','男','12322')

insert into student
values(3,'李四','女',default,'23')

--一次性插入多行數據
--在SQL SERVER  裏面
/*
  --自己輸入的值
insert into student
select 110,'張三','男',getdate(),95031 union
select 111,'張2','男',getdate(),95031 union
select 112,'張3','男',getdate(),95031 union
select 113,'張4','男',getdate(),95031

--創建表的同時插入另外一張表裏面的數據(表不存在的情況下)
select * into newStudent from student;--同時只能執行一次
select * from newStudent;
truncate table newStudent;
drop table newStudent;

--表已經存在的情況下
insert into newStudent select * from student;

--只複製表結構不要複製數據
select * into newStudent from student where 1=0;

--我們在創建表的同時添加一個字段,在這個字段上面添加標識列
select identity(int,1,1) as sno,sname,ssex into newStudent  from student;
select * from newStudent;
*/
select * from student;
--在oracle裏面一次性插入多行記錄
insert into student
select 110,'張三','男','長沙','95031' from dual union
select 111,'張2','男','長沙','95032'  from dual union
select 112,'張3','男','長沙','95033'  from dual union
select 113,'張4','男','長沙','95034'  from dual


select * into newStudent from student; --在oracle裏面錯誤

create table newStudent
(
       sno int primary key,
       sname varchar2(20)
)
select * from newStudent;
--往已經存在的表中插入另外一張表中的數據
insert into newStudent select sno,sname from student;

commit;

select * from student;
--更新語句
update 表名 set 字段名=值 where 條件

update student set sname = '王五' where sno =111;

rollback;

--刪除語句
delete from student where sno =111;

delete cardid from student;--不能刪除指定列的值

--刪除表中的所有記錄
truncate table student;
--相當於
delete from student;

truncate table tt1;

select * from student;

insert into student
values(1,'張三','男',default,'111')

create table tt
(sno int primary key,
sname varchar2(20))

insert into tt select sno,sname from student;

select * from tt;

truncate table tt;

--查詢select
select 12+12 from dual;
--select  執行順序我們不能隨意更改
select
from
where
group by
having
order by

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章