sql基本语句

本文档详细介绍了 Oracle 数据库中的 SQL 操作基础,包括用户创建、表的增删改查、字段操作、索引及序列使用、数据的插入与更新等。此外,还涉及了复杂的关联查询、视图创建以及不同类型的表连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[sql]  view plain  copy
  1. <div><p>--创建用户  
  2. create user han identified by han default tablespace  
  3. users Temporary TABLESPACE Temp;  
  4. grant connect,resource,dba to han; //授予用户han开发人员的权利</p><p>--------------------对表的操作--------------------------</p><p></p><p>创建表格语法:  
  5.      create table 表名(  
  6.        字段名1 字段类型(长度) 是否为空,  
  7.         字段名2 字段类型       是否为空  
  8. ); </p><p>-增加主键  
  9.      alter table 表名 add constraint 主键名 primary key (字段名1);</p><p>-增加外键:  
  10.      alter table 表名  
  11.        add constraint 外键名 foreign key (字段名1)  
  12.          references 关联表 (字段名2);</p><p>在建立表格时就指定主键和外键</p><p>    create table T_STU (  
  13.       STU_ID               char(5)                         not null,  
  14.        STU_NAME             varchar2(8)                     not null,  
  15.       constraint PK_T_STU primary key (STU_ID)  
  16. );</p><p>  
  17. 主键和外键一起建立:  
  18.      create table T_SCORE (  
  19.        EXAM_SCORE           number(5,2),  
  20.        EXAM_DATE            date,  
  21.         AUTOID               number(10)                      not null,  
  22.        STU_ID               char(5),  
  23.        SUB_ID               char(3),  
  24.        constraint PK_T_SCORE primary key (AUTOID),  
  25.        constraint FK_T_SCORE_REFE foreign key (STU_ID)  
  26.         references T_STU (STU_ID)  
  27. )</p><p></p><p></p><p>  
  28. --创建表  
  29. create table classes(  
  30.        id number(9) not null primary key,  
  31.        classname varchar2(40) not null  
  32. )        
  33. --查询表  
  34. select * from classes;</p><p>--删除表  
  35. drop table students;</p><p>--修改表的名称  
  36. rename alist_table_copy to alist_table;</p><p>--显示表结构  
  37. describe test --不对没查到</p><p>-----------------------对字段的操作-----------------------------------  
  38. --增加列  
  39. alter table test add address varchar2(40);</p><p>--删除列  
  40. alter table test drop column address;</p><p>--修改列的名称  
  41. alter table test modify address addresses varchar(40;</p><p>--修改列的属性  
  42. alter table test modi</p><p>create table test1(  
  43.        id number(9) primary key not null,  
  44.        name varchar2(34)  
  45.       )  
  46. rename test2 to test;</p><p>--创建自增的序列  
  47. create sequence class_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;</p><p>select class_seq.currval from dual</p><p>--插入数据  
  48. insert into classes values(class_seq.nextval,'软件一班')  
  49. commit;</p><p>--更新数据  
  50. update stu_account set username='aaa' where count_id=2;  
  51. commit;</p><p>--创建唯一索引  
  52. create unique index username on stu_account(username);   --唯一索引 不能插入相同的数据</p><p>--行锁 在新打开的对话中不能对此行进行操作  
  53. select * from stu_account t where t.count_id=2 for update--行锁</p><p>  
  54. --alter table stuinfo modify sty_id to stu_id;</p><p>alter table students drop constraint class_fk;  
  55. alter table students add constraint class_fk foreign key (class_id) references classes(id);--外键约束  
  56. alter table stuinfo add constraint stu_fk foreign key (stu_id) references students(id) ON DELETE CASCADE;--外键约束,级联删除</p><p>alter table stuinfo drop constant stu_fk;   </p><p>insert into students values(stu_seq.nextval,'张三',1,sysdate);</p><p>insert into stuinfo values(stu_seq.currval,'威海');</p><p>select * from stuinfo;</p><p>create table zhuce(  
  57.        zc_id number(9) not null primary key,  
  58.        stu_id number(9) not null,  
  59.        zhucetime date default sysdate</p><p>)</p><p>create table feiyong (  
  60.        fy_id number(9) not null primary key,  
  61.        stu_id number(9) not null,  
  62.        mx_id number(9) not null,  
  63.        yijiao number(7,2) not null default 0,  
  64.        qianfei number(7,2) not null  
  65.          
  66. )</p><p>  
  67. create talbe fymingxi(  
  68.        mx_id number(9) not null primary key,  
  69.        feiyong number(7,2) not null,     //共7位数字,小数后有两位  
  70.        class_id number(9) not null  
  71. }</p><p>create table card(  
  72.        card_id number(9) primary key,  
  73.        stu_id number(9) not null,  
  74.        money number(7,2) not null default 0,  
  75.        status number(1) not null default 0   --0表可用,1表挂失  
  76. )</p><p>--链表查询</p><p>select c.classname||'_'||s.stu_name as 班级_姓名,si.address from classes c,students s , stuinfo si where c.id=s.class_id and s.id=si.stu_id;   
  77. insert into students values(stu_seq.nextval,'李四',1,sysdate);  
  78. insert into stuinfo values(stu_seq.currval,'南京');</p><p>--函数  
  79. select rownum,id,stu_name from students t order by id asc;</p><p>  
  80. --中间表实现多对多关联  
  81. --(1   1, 1   n,n 1,n n )</p><p>  
  82. --1 n的描述   1的表不作处理   n的表有1表的字段  
  83. --1 1的描述   主外键关联  
  84. --n n的描述 中间表实现多对多关联</p><p>create table course(  
  85.          course_id number(9) not null,  
  86.          couser_name varchar2(40) not null  
  87. )  
  88. alter table course to couse;  
  89. create table stu_couse(  
  90.        stu_couse_id number(9) primary key,  
  91.        stu_id number(9) not null,  
  92.        couse_id number(9) not null</p><p>)</p><p>create unique index stu_couse_unq on stu_couse(stu_id,couse_id); --唯一学生  
  93. create sequence stu_couse_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;</p><p>  
  94. create sequence couses_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;  
  95. insert into course values(couses_seq.nextval,'计算机原理');  
  96. insert into course values(couses_seq.nextval,'编译原理');  
  97. insert into course values(couses_seq.nextval,'数据库原理');  
  98. insert into course values(couses_seq.nextval,'数据结构');  
  99. insert into course values(couses_seq.nextval,'计算机基础');  
  100. insert into course values(couses_seq.nextval,'C语言初步');  
  101. commit;</p><p>insert into stu_couse values(stu_couse_seq.nextval,1,1);  
  102. insert into stu_couse values(stu_couse_seq.nextval,1,3);  
  103. insert into stu_couse values(stu_couse_seq.nextval,1,5);  
  104. insert into stu_couse values(stu_couse_seq.nextval,1,5);</p><p>insert into stu_couse values(stu_couse_seq.nextval,2,1);  
  105. commit;  
  106. select * from stu_couse;  
  107. select * from course;</p><p>--select s.stu_name,sc.couse_id, c.couser_name from students s,course c,stu_couse sc where stu_id=1</p><p>--select couse_id from stu_couse where stu_id=1</p><p>select cl.classname,s.stu_name,c.couser_name from stu_couse sc, students s,course c,classes cl where s.id=sc.stu_id and sc.couse_id=c.course_id and s.class_id=cl.id and s.id=1;</p><p>--班级——姓名  
  108. select c.classname,s.stu_name from students s,classes c where s.class_id=c.id and s.id=2;</p><p>select * from students s where s.id=2  
  109. --班级——姓名——课程</p><p>select cl.classname,s.stu_name,c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.id=26;</p><p>  
  110. --sql 语句的写法,现写出关联到的表,然后写出要查找的字段,第三 写出关联条件   ,记住在写关联到的表时先写数据多的表,这样有助于提高sql的效率</p><p>select c.couser_name,s.stu_name from stu_couse sc,students s,course c where c.course_id=1 and c.course_id=sc.couse_id and sc.stu_id=s.id;</p><p>select s.stu_name from students s,stu_couse sc where s.id=sc.stu_id group by s.id,s.stu_name;</p><p>  
  111. select c.classname,count(sc.couse_id) from stu_couse sc,students s,classes c where s.class_id=c.id and s.id=sc.stu_id group by c.classname;</p><p>select s.stu_name, count(sc.couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id group by s.id,s.stu_name having count(sc.stu_couse_id)>3;  
  112. 班级 学生 选课数量  
  113. select cl.classname,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by cl.classname;</p><p>  
  114. --班级 学生 选课数量  
  115. select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by s.stu_name;</p><p>select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc ,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.id;</p><p>select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.stu_name;  
  116. --班级 学生 所选课程id 所选课程名称</p><p>  
  117. --创建试图 目的把表联合起来 然后看成一个表,在与其他的联合进行查询   
  118. create view xsxk as select cl.classname, s.stu_name,c.couse_id, c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.class_id=cl.id;</p><p>select * from xsxk</p><p>  
  119. create view classstu as select s.id,c.classname,s.stu_name from students s,classes c where c.id=s.class_id;  
  120. drop view classstu; --删除视图  
  121. select * from classstu;  
  122.   
  123. create view stu_couse_view as select s.id ,c.couse_name from stu_couse sc,students s,couse c where s.id=sc.stu_id and sc.couse_id=c.couse_id;  
  124. select * from stu_couse_view;  
  125.   
  126. create view csc as select cs.classname,cs.stu_name,scv.couse_name from classstu cs,stu_couse_view scv where cs.id=scv.id;  
  127. select * from csc;</p><p>  
  128. select * from classes cross join students; --全连接,相当于select * from classes,students;</p><p>select * from classes cl left join students s on cl.id=s.class_id; --左连接 不管左表有没有 都显示出来  
  129. select * from classes cl right join students s on cl.id=s.class_id; --右连接  
  130. select * from classes cl full join students s on cl.id=s.class_id; --全连接</p><p>  
  131. insert into classes values(class_seq.nextval,'软件四班');</p><p>create table sales(  
  132.        nian varchar2(4),  
  133.        yeji number(5)  
  134.          
  135. );  
  136. insert into sales values('2001',200);  
  137. insert into sales values('2002',300);  
  138. insert into sales values('2003',400);  
  139. insert into sale
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值