ORACLE 基本建表语句

合集下载

plsql建表 基本语句

plsql建表 基本语句

plsql建表基本语句在PL/SQL中,创建表的基本语句是使用CREATE TABLE命令。

以下是创建表的基本语法:sqlCREATE TABLEtable_name (column1 datatype[constraint],column2 datatype[constraint],column3 datatype[constraint],...);其中,table_name是表的名称,column1, column2, column3等是表中的列名,datatype是列的数据类型,constraint是可选的约束条件。

以下是一个示例,展示如何在PL/SQL中创建一个简单的表:sqlCREATE TABLE employees (employee_id NUMBER PRIMARYKEY,first_name VARCHAR2(50),last_name VARCHAR2(50),hire_date DATE,salary NUMBER(8,2) CHECK(salary > 0));在上面的示例中,我们创建了一个名为employees的表,包含了五个列:employee_id、first_name、last_name、hire_date和salary。

每个列都有相应的数据类型,并且为employee_id列设置了主键约束,为salary列设置了检查约束,确保工资大于0。

请注意,PL/SQL通常用于Oracle数据库的存储过程和函数,而创建表的语句实际上是在SQL部分执行的。

在Oracle SQL Developer 等工具中,可以直接执行上述SQL语句来创建表。

如果你需要在PL/SQL块中执行DDL语句(如CREATE TABLE),你可以使用动态SQL (例如EXECUTE IMMEDIATE语句)来实现。

oracle数据库基本语句

oracle数据库基本语句

oracle数据库基本语句oracle 数据库是一种常用的关系型数据库管理系统,常用的oracle数据库包括oracle10g、oracle11g和oracle12c。

要掌握oracle的基本用法,必须掌握其基本的语句。

oracle的常用基本语句有:一、数据定义语言(DDL)1、创建数据表:CREATE TABLE 表名(字段名数据类型[完整性约束条件],字段名数据类型[完整性约束条件],……);2、修改数据表: ALTER TABLE 表名 ADD(字段名数据类型[完整性约束条件],字段名数据类型[完整性约束条件],……);3、删除数据表: DROP TABLE 表名;4、创建索引:CREATE [UNIQUE] INDEX 索引名ON 表名[字段名[,字段名];5、删除索引: DROP INDEX 索引名;三、数据控制语言(DCL)1、建立用户: CREATE USER 用户名 IDENTIFIED BY 密码;2、删除用户: DROP USER 用户名;3、授权:GRANT 权限 ON 对象 TO 用户[WITH GRANT OPTION];4、回收授权: REVOKR 权限 ON 对象 FROM 用户;5、控制事务: COMMIT/ROLLBACK;四、数据库控制语言(DBCL)1、创建数据库:CREATE DATABASE 数据库名;2、删除数据库: DROP DATABASE 数据库名;3、创建表空间:CREATE TABLESPACE 表空间名 SEGMENT SPACE MANAGEMENT 自动;4、删除表空间: DROP TABLESPACE 表空间名;5、管理会话: ALTER SYSTEM KILL SESSION ['会话号'];。

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全一、基础1、说明:创建数据库CREATE DATABASE database-name2、说明:删除数据库drop database dbname3、说明:备份sql server--- 创建备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat'--- 开始备份BACKUP DATABASE pubs TO testBack4、说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..)根据已有的表创建新表:A:select * into table_new from table_old (使用旧表创建新表)B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle>5、说明:删除表drop table tablename6、说明:增加一个列,删除一个列A:alter table tabname add column col typeB:alter table tabname drop column colname注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

7、添加主键:Alter table tabname add primary key(col)删除主键:Alter table tabname drop primary key(col)8、创建索引:create [unique] index idxname on tabname(col….)删除索引:drop index idxname注:索引是不可更改的,想更改必须删除重新建。

Oracle创建表结构

Oracle创建表结构

Oracle创建表结构⼀、Oracle序列创建和使⽤创建序列语法 CREATE SEQUENCE 序列名 [相关参数]参数说明INCREMENT BY :序列变化的步进,负值表⽰递减。

(默认1)START WITH:序列的初始值。

(默认1)MAXvalue:序列可⽣成的最⼤值。

(默认不限制最⼤值,NOMAXVALUE)MINVALUE:序列可⽣成的最⼩值。

(默认不限制最⼩值,NOMINVALUE)CYCLE:⽤于定义当序列产⽣的值达到限制值后是否循环(NOCYCLE:不循环,CYCLE:循环)。

CACHE:表⽰缓存序列的个数,数据库异常终⽌可能会导致序列中断不连续的情况,默认值为20,如果不使⽤缓存可设置NOCACHE 例CREATE SEQUENCE SEQU_R_FR_GLRQS_CHECK_RESULTINCREMENT BY 1START WITH 1NOMAXvalueNOCYCLENOCACHE;修改、删除序列使⽤alter命令进⾏修改使⽤drop命令删除⼆、Oracle主键的创建例如:alter table T_R_FR_GLRQS_CHECK_RESULTadd constraint PK_R_FR_GLRQS_CHECK_RESULT primary key (C_IDEN);三、创建索引例如:create index IDX_R_FR_GLRQS_CHECK_RESULT on T_R_FR_GLRQS_CHECK_RESULT (C_QSRQ)四、创建表结构例如:create table T_R_FR_GLRQS_CHECK_RESULT(c_iden VARCHAR2(30) not null,c_port_code VARCHAR2(30) not null,c_ywdm VARCHAR2(30) not null,c_xwdm VARCHAR2(20),c_jyrq VARCHAR2(10),c_qsrq VARCHAR2(10),c_fsrq VARCHAR2(10),c_zjrq VARCHAR2(10),n_zjje NUMBER(22,4),n_qsje NUMBER(22,4),n_ce NUMBER(22,4),c_hdjg VARCHAR2(10),c_hdzt VARCHAR2(10),n_check_state NUMBER(3) default 0 not null,c_update_by VARCHAR2(20) default ' ' not null,c_update_time VARCHAR2(20) default ' ' not null,c_check_by VARCHAR2(20),c_check_time VARCHAR2(20))。

创建oracle数据库表空间,角色,用户的sql语句

创建oracle数据库表空间,角色,用户的sql语句

创建oracle数据库表空间,角色,用户的sql语句创建oracle 数据库表空间,角色,用户的sql语句1.创建角色CREATE ROLE "QIUDINGROLE" NOT IDENTIFIED;GRANT "CONNECT" TO "QIUDINGROLE";GRANT "DBA" TO "QIUDINGROLE";GRANT "RESOURCE" TO "QIUDINGROLE";2.创建表空间create tablespace safetempdatafile'D:\oracle\product\10.1.0\oradata\localpower\safetemp01.dbf'size32m autoextend on next32m maxsize unlimited logging extent management local segment space management auto;CREATE SMALLFILE TABLESPACE "EXAM" DATAFILE'D:\Soft\oracle\product\10.2.0\oradata\qiuding\EXAM' SIZE 100M AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED LOGGING EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;BEGINDBMS_SERVER_ALERT.SET_THRESHOLD(9000,NULL,NULL,N ULL,NULL,1,1,NULL,5,' EXAM'); END;CREATE SMALLFILE TEMPORARY TABLESPACE "EXAM_TEMP" TEMPFILE'D:\Soft\oracle\product\10.2.0\oradata\qiuding\EXAM_tem p' SIZE 100M AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL UNIFORM SIZE 1M;BEGINDBMS_SERVER_ALERT.SET_THRESHOLD(9000,NULL,NULL,NULL,NULL,1,1,NULL,5,' EXAM_TEMP'); END;3.创建用户create userEXAMidentified by "PASSWORD123"default tablespace "EXAM"temporary tablespace "EXAM_TEMP"profile DEFAULTACCOUNT UNLOCK;grant QIUDINGROLE to EXAM;grant unlimited tablespace to EXAM;4.创建备份目录并且付相应权限create directory dump_dir as 'D:\Soft\oracle\backup';grant read,write on directory dump_dir to EXAM;。

oracle select create表格语句-概述说明以及解释

oracle select create表格语句-概述说明以及解释

oracle select create表格语句-范文模板及概述示例1:标题:Oracle SELECT语句创建表格的步骤和示例简介:Oracle的SELECT语句不仅可以查询现有的表格数据,还可以使用其结果集来创建新的表格。

本文将介绍如何使用Oracle的SELECT语句来创建表格,并提供一些示例以帮助您理解和应用这一功能。

内容:一、概述SELECT语句的表格创建功能在Oracle数据库中,SELECT语句可以用于创建新的表格。

它可以通过选择现有表格的特定列或者通过运算和处理现有表格数据的结果来创建新表格。

这是一个非常方便的功能,特别是在需要根据现有数据创建新表格进行分析和报表等应用时。

二、使用SELECT语句创建表格的步骤要使用SELECT语句创建表格,您需要遵循以下步骤:1. 编写合适的SELECT语句,以选择现有表格的特定列或者经过计算和处理的结果集。

2. 使用CREATE TABLE语句,将SELECT语句的结果作为新表格的数据进行存储。

3. 定义新表格的结构,包括列名、数据类型和约束等信息。

三、示例:使用SELECT语句创建表格以下是一个示例,演示了如何使用SELECT语句创建一个新的表格:sqlCREATE TABLE new_table ASSELECT column1, column2, column3FROM existing_tableWHERE condition;在上述示例中,我们从现有表格existing_table中选择特定列column1、column2和column3,并根据条件进行筛选,然后使用CREATE TABLE语句将查询结果存储到新表格new_table中。

注意,您可以根据实际需求自定义新表格的名称、列名和条件。

另外,如果需要对SELECT语句进行更复杂的操作,您还可以使用子查询、连接操作、函数和其他高级特性来创建新表格。

这些方法可以根据您的需求对数据进行进一步的处理和筛选。

Oracle_基本建表语句

Oracle_基本建表语句

Oracle_基本建表语句--创建⽤户create user han identified by han default tablespaceusers Temporary TABLESPACE Temp;grant connect,resource,dba to han; //授予⽤户han开发⼈员的权利--------------------对表的操作----------------------------创建表create table classes(id number(9) not null primary key,classname varchar2(40) not null)--查询表select * from classes;--删除表drop table students;--修改表的名称rename alist_table_copy to alist_table;--显⽰表结构describe test --不对没查到-----------------------对字段的操作-------------------------------------增加列alter table test add address varchar2(40);--删除列alter table test drop column address;--修改列的名称alter table test modify address addresses varchar(40;--修改列的属性alter table test modicreate table test1(id number(9) primary key not null,name varchar2(34))rename test2 to test;--创建⾃增的序列create sequence class_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;select class_seq.currval from dual--插⼊数据insert into classes values(class_seq.nextval,'软件⼀班')commit;--更新数据update stu_account set username='aaa' where count_id=2;commit;--创建唯⼀索引create unique index username on stu_account(username); --唯⼀索引不能插⼊相同的数据--⾏锁在新打开的对话中不能对此⾏进⾏操作select * from stu_account t where t.count_id=2 for update; --⾏锁--alter table stuinfo modify sty_id to stu_id;alter table students drop constraint class_fk;alter table students add constraint class_fk foreign key (class_id) references classes(id);--外键约束alter table stuinfo add constraint stu_fk foreign key (stu_id) references students(id) ON DELETE CASCADE;--外键约束,级联删除alter table stuinfo drop constant stu_fk;insert into students values(stu_seq.nextval,'张三',1,sysdate);insert into stuinfo values(stu_seq.currval,'威海');select * from stuinfo;create table zhuce(zc_id number(9) not null primary key,stu_id number(9) not null,zhucetime date default sysdate)create table feiyong (fy_id number(9) not null primary key,stu_id number(9) not null,mx_id number(9) not null,yijiao number(7,2) not null default 0,qianfei number(7,2) not null)create talbe fymingxi(mx_id number(9) not null primary key,feiyong number(7,2) not null, //共7位数字,⼩数后有两位 class_id number(9) not null}create table card(card_id number(9) primary key,stu_id number(9) not null,money number(7,2) not null default 0,status number(1) not null default 0 --0表可⽤,1表挂失)--链表查询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;insert into students values(stu_seq.nextval,'李四',1,sysdate); insert into stuinfo values(stu_seq.currval,'南京');--函数select rownum,id,stu_name from students t order by id asc;--中间表实现多对多关联--(1 1, 1 n,n 1,n n )--1 n的描述 1的表不作处理 n的表有1表的字段--1 1的描述主外键关联--n n的描述中间表实现多对多关联create table course(course_id number(9) not null,couser_name varchar2(40) not null)alter table course to couse;create table stu_couse(stu_couse_id number(9) primary key,stu_id number(9) not null,couse_id number(9) not null)create unique index stu_couse_unq on stu_couse(stu_id,couse_id); --唯⼀学⽣create sequence stu_couse_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;create sequence couses_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;insert into course values(couses_seq.nextval,'计算机原理');insert into course values(couses_seq.nextval,'编译原理');insert into course values(couses_seq.nextval,'数据库原理');insert into course values(couses_seq.nextval,'数据结构');insert into course values(couses_seq.nextval,'计算机基础');insert into course values(couses_seq.nextval,'C语⾔初步');commit;insert into stu_couse values(stu_couse_seq.nextval,1,1);insert into stu_couse values(stu_couse_seq.nextval,1,3);insert into stu_couse values(stu_couse_seq.nextval,1,5);insert into stu_couse values(stu_couse_seq.nextval,1,5);insert into stu_couse values(stu_couse_seq.nextval,2,1);commit;select * from stu_couse;select * from course;--select s.stu_name,sc.couse_id, c.couser_name from students s,course c,stu_couse sc where stu_id=1--select couse_id from stu_couse where stu_id=1select 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;--班级——姓名select c.classname,s.stu_name from students s,classes c wheres.class_id=c.id and s.id=2;select * from students s where s.id=2--班级——姓名——课程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;--sql 语句的写法,现写出关联到的表,然后写出要查找的字段,第三写出关联条件,记住在写关联到的表时先写数据多的表,这样有助于提⾼sql的效率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;select s.stu_name from students s,stu_couse sc where s.id=sc.stu_id group by s.id,s.stu_name;select c.classname,count(sc.couse_id) from stu_couse sc,studentss,classes c where s.class_id=c.id and s.id=sc.stu_id group byc.classname;select s.stu_name, count(sc.couse_id) from stu_couse sc,studentss,classes cl where s.id=sc.stu_id group by s.id,s.stu_name having count(sc.stu_couse_id)>3;班级学⽣选课数量select cl.classname,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id ands.class_id=cl.id group bycl.classname;--班级学⽣选课数量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;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;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;--班级学⽣所选课程id 所选课程名称--创建试图⽬的把表联合起来然后看成⼀个表,在与其他的联合进⾏查询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;select * from xsxkcreate view classstu as select s.id,c.classname,s.stu_name from students s,classes c where c.id=s.class_id;drop view classstu; --删除视图select * from classstu;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; select * from stu_couse_view;create view csc as select cs.classname,cs.stu_name,scv.couse_name from classstu cs,stu_couse_view scv wherecs.id=scv.id;select * from csc;select * from classes cross join students; --全连接,相当于select * from classes,students;select * from classes cl left join students s on cl.id=s.class_id; --左连接不管左表有没有都显⽰出来select * from classes cl right join students s on cl.id=s.class_id; --右连接select * from classes cl full join students s on cl.id=s.class_id; --全连接insert into classes values(class_seq.nextval,'软件四班');create table sales(nian varchar2(4),yeji number(5));insert into sales values('2001',200);insert into sales values('2002',300);insert into sales values('2003',400);insert into sales values('2004',500);commit;select * from sales;drop table sale;select s1.nian,sum(s2.yeji) from sales s1,sales s2 wheres1.nian>=s2.nian group by s1.nian order by s1.nian desc;select s1.nian,sum(s2.yeji) from sales s1,sales s2 wheres1.nian>=s2.nian group by s1.nian;s年年业绩总和2001 2002002 5002003 9002004 1400create table test1(t_id number(4));create table org(org_id number(9) not null primary key,org_name varchar2(40) not null,parent_id number(9));create sequence org_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE; drop sequence org_seq;insert into org values(1,'华建集团',0);insert into org values(2,'华建集团⼀分公司',1);insert into org values(3,'华建集团⼆分公司',1);insert into org values(4,'华建集团财务部',1);insert into org values(5,'华建集团⼯程部',1);insert into org values(6,'华建集团⼀分公司财务处',2);insert into org values(7,'华建集团⼀分公司⼯程处',2);select * from org;--不正确不能实现循环select /doc/5e8716366.html_id , /doc/5e8716366.html_name ,b.parent_id from org a,org b where/doc/5e8716366.html_id=7 and a.parent_id=/doc/5e8716366.html_id;select * from org connect by prior parent_id=org_id start with org_id=7 order by org_id;select * from org connect by prior org_id=parent_id start with org_id=1 order by org_id;create table chengji(cj_id number(9) not null primary key,stu_cou_id number(9) not null,fen number(4,1));insert into chengji values(1,1,62);insert into chengji values(2,2,90);insert into chengji values(3,3,85);insert into chengji values(4,4,45);insert into chengji values(5,5,68);insert into chengji values(6,6,87);commit;select * from chengji;select * from stu_couse;--在oracle 中好像不适⽤ alter table chengji change stu_cou_idstu_couse_id;alter table shop_jb change price1 price double;学⽣姓名平均分select s.stu_name,avg(cj.fen) from stu_couse sc,chengji cj,students s where s.id=sc.stu_id andsc.stu_couse_id=cj.stu_couse_id group bys.id,s.stu_name;select s.stu_name from students s,stu_couse sc,chengji cj wheres.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group bys.id,s.stu_name;select s.stu_name,cj.fen from students s,stu_couse sc,chengji cj where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60;学⽣姓名科⽬成绩select s.stu_name,c.couse_name,cj.fen from stu_couse sc,studentss,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60 order by=;select * from stu_couse;--集合运算--选择了课程3的学⽣ union 选择了课程5的学⽣并集--选择了课程3 或者选择了课程5的学⽣select s.stu_name from students s,couse c,stu_couse sc wheres.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3unionselect s.stu_name from students s,couse c,stu_couse sc wheres.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=5--选择了课程3,5,2 的学⽣ intersect 选择课程1,2,4的学⽣交集--求选择了课程 2 并且选择了课程 3 的学⽣交集select s.stu_name from students s,couse c,stu_couse sc wheres.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=2intersectselect s.stu_name from students s,couse c,stu_couse sc wheres.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3;--选择了课程3,5,8的学⽣ minus 选择了课程1,7,8的学⽣ --差集-- 求所有课程的成绩都⼤于 60 的学⽣差集select distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id andsc.couse_id=c.couse_id andsc.stu_couse_id=cj.stu_couse_id and cj.fen>60minusselect distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id andsc.couse_id=c.couse_id andsc.stu_couse_id=cj.stu_couse_id and cj.fen<60;⼀、何謂分區表(索引)?分區表是⼀種數據庫的物理存儲機制,它將⼀個表的數據存儲在不同的存儲⽚段中,這些不同的存儲⽚稱為區(partition)。

oracle数据库常用语句大全

oracle数据库常用语句大全

Oracle数据库是甲骨文公司的一款关系数据库管理系统,它在数据库领域一直处于领先地位。

以下是Oracle数据库常用的一些SQL语句:SELECT:查询语句,用于从一个或多个表中检索数据。

例如:sqlSELECT column1, column2 FROM table_name;INSERT:插入语句,用于向表中插入新记录。

例如:sqlINSERT INTO table_name (column1, column2) VALUES (value1, value2);UPDATE:更新语句,用于修改表中的数据。

例如:sqlUPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;DELETE:删除语句,用于从表中删除记录。

例如:sqlDELETE FROM table_name WHERE condition;CREATE TABLE:创建表语句,用于创建新的数据库表。

例如:scssCREATE TABLE table_name (column1 datatype, column2 datatype, ...);ALTER TABLE:修改表语句,用于添加、删除或修改表中的列。

例如:sqlALTER TABLE table_name ADD column_name datatype;ALTER TABLE table_name DROP COLUMN column_name;ALTER TABLE table_name MODIFY COLUMN column_name datatype; WHERE:条件语句,用于在查询中过滤数据。

例如:sqlSELECT * FROM table_name WHERE condition;GROUP BY:分组语句,用于将查询结果分组。

在使用GROUP BY时,SELECT子句中的列表中的所有个体值(除聚组函数avg、count等外)必须是GROUP BY子句中的表达式或常量。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
--删除列 alter table test drop column address;
--修改列的名称 alter table test modify address addresses varchar(40;
--修改列的属性 alter table test modi
create table test1( id number(9) primary key not null, name varchar2(34)
失 )
--0 表可用,1 表挂
--链表查询
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; insert into students values(stu_seq.nextval,'李四',1,sysdate); insert into stuinfo values(stu_seq.currval,'南京');
--select couse_id from stu_couse where stu_id=1
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;
alter table stuinfo drop constant stu_fk;
insert into students values(stu_seq.nextval,'张三',1,sysdate);
insert into stuinfo values(stu_seq.currval,'威海');
insert into stu_couse values(stu_couse_seq.nextval,2,1); commit; select * from stu_couse; select * from course;
--select s.stu_name,sc.couse_id, c.couser_name from students s,course c,stu_couse sc where stu_id=1
create table course( course_id number(9) not null, couser_name varchar2(40) not null
) alter table course to couse; create table stu_couse(
stu_couse_id number(9) primary key, stu_id number(9) not null, couse_id number(9) not null
create sequence couses_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE; insert into course values(couses_seq.nextval,'计算机原理'); insert into course values(couses_seq.nextval,'编译原理'); insert into course values(couses_seq.nextval,'数据库原理'); insert into course values(couses_seq.nextval,'数据结构'); insert into course values(couses_seq.nextval,'计算机基础'); insert into course values(couses_seq.nextval,'C 语言初步'); commit;
--删除表 drop table students;
--修改表的名称 rename alist_table_copy to alist_table;
--显示表结构 describe test --不对没查到
-----------------------对字段的操作 ------------------------------------增加列 alter table test add address varchar2(40);
--函数 select rownum,id,stu_name from students t order by id asc;
--中间表实现多对多关联 --(1 1, 1 n,n 1,n n )
--1 n 的描述 1 的表不作处理 n 的表有 1 表的字段
--1 1 的描述 主外键关联 --n n 的描述 中间表实现多对多关联
select * from stuinfo;
create table zhuce( zc_id number(9) not null primary key, stu_id number(9) not null, zhucetime date default sysdate
)
create table feiyong ( fy_id number(9) not null primary key, stu_id number(9) not null, mx_id number(9) not null, yijiao number(7,2) not null default 0, qianfei number(7,2) not null
)
create talbe fymingxi(
mx_id number(9) not null primary key,
feiyong number(7,2) not null,
//共 7 位数字,小数
后有两位
class_id number(9) not null
}
create table card( card_id number(9) primary key, stu_id number(9) not null, money number(7,2) not null default 0, status number(1) not null default 0
references T_STU (STU_ID)
)
--创建表 create table classes(
id number(9) not null primary key, classname varchar2(40) not null ) --查询表 select * from classes;
在建立表格时就指定主键和外键
create table T_STU (
STU_ID
r(5)
not null,
STU_NAME
varchar2(8)
not null,
constraint PK_T_STU primary key (STU_ID)
);
主键和外键一起建立:
create table T_SCORE (
--班级——姓名 select c.classname,s.stu_name from students s,classes c where s.class_id=c.id and s.id=2;
select * from students s where s.id=2 --班级——姓名——课程
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;
)
create unique index stu_couse_unq on stu_couse(stu_id,couse_id); --唯 一学生 create sequence stu_couse_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;
--创建用户 create user han identified by han default tablespace users Temporary TABLESPACE Temp; grant connect,resource,dba to han; //授予用户 han 开发人员的权利
--------------------对表的操作--------------------------
insert into stu_couse values(stu_couse_seq.nextval,1,1); insert into stu_couse values(stu_couse_seq.nextval,1,3); insert into stu_couse values(stu_couse_seq.nextval,1,5); insert into stu_couse values(stu_couse_seq.nextval,1,5);
--插入数据 insert into classes values(class_seq.nextval,'软件一班') commit;
--更新数据 update stu_account set username='aaa' where count_id=2; commit;
相关文档
最新文档