当前位置:Gxlcms > 数据库问题 > oracle-2-sql数据操作和查询

oracle-2-sql数据操作和查询

时间:2021-07-01 10:21:17 帮助过:3人阅读

 

2》创建表和约束

代码演示

 SQL> create table yizheninfos
  2  (
  3  stuid varchar2(7) not null,
  4  stuname varchar2(10) not null,
  5  gender varchar2(2) not null,
  6  age number(2) not null,
  7  seat number(2) not null,
  8  enrolldate date,
  9  stuaddress varchar2(50) default ‘地址不详‘,
 10  classno varchar2(4) not null
 11  )
 12  /

Table created.

SQL> alter table yizheninfos add constraint PK_yizhen primary key(stuid)
  2  /

Table altered.----->这条sql语句的作用是创建一个主键约束,下面的其他sql语句的作用就是创建各种check 约束

SQL> alter table yizheninfos add constraint ck_yizhender check(gender = ‘男‘ or gender = ‘女‘)
  2  /

Table altered.

SQL> alter table yizheninfos add constraint ck_yizhent check(seat >= 0 and seat <= 50)
  2  /

Table altered.

SQL> alter table yizheninfos add constraint ck_yizhenssno check(classno > ‘1001‘ and classno <=‘1999‘ or
  2  (classno >=‘2001‘ and classno <=‘2999‘))
  3  /

Table altered.

SQL> alter table yizheninfos add constraints un_stuname unique(stuname)
  2  /

Table altered.----》这条sql语句的作用就是创建一个唯一约束,表示该列值是唯一的,列中的值不能重复。

代码解析:

在oracle中,“/” 代表的是执行缓存区中的语句,由于缓冲区中只存储一条刚刚保存过的语句,由于每条语句没有用分号结尾,只是保存在缓冲区,因此每条语句后面都有单独一行“/”

 

现在来举例展示一下外键的约束定义

SQL> create table scores
  2  (
  3  id number,                             ----》ID
  4  term varchar2(2),                   ----》学期为s1或s2
  5  stuid varchar2(7) not null,       ----》学号
  6  examno varchar2(7) not null,   ----》考号
  7  writtenscore number(4,1) not null,  ----》笔试成绩
  8  labscore number(4,1) not null         ----》机试成绩
  9  )
 10  /

Table created.

SQL> alter table scores
  2  add constraint ck_scores_term check(term = ‘s1‘ or term =‘s2‘)
  3  /

Table altered.

SQL> alter table scores
  2  add constraint fk_scores_yizheninfos_stuid foreign key(stuid) references yizheninfos(stuid)
  3  /

Table altered.---->这就是外键的约束定义

 

4.数据操作语言(DML)

DML用于对数据库的表中数据进行添加,修改,删除和select....for ,update等

 

》》4.1简单查询

数据查询是用select 命令从数据库的表中提取信息

select 列名  from  表名  where  条件  order by 列名

注:order by 要求在查询的结果中排序,默认是升序

 

》》4.2根据查询结果创建表

create table  表名  as  select 语句

代码演示

SQL> create table yizheninfosss as select * from yizheninfos ;

SQL> create table yizheninfossssss as select * from yizheninfos where 1=2;
第一条命令不存在认得的约束,并且把查询结果一起插入到新表中

第二条命令只是复制一个表结构,没有任何的数据(因为1=2的这个条件不成立)

 

》》4.3  数据的插入----insert

insert into 表名(列1,列2,列3......) values (值1,值2,值3.....)

语法解析:

(1)这里的列名是可以省略的,当省略列名的时候,默认是表中的所有列名,列名顺序为表定义中列的先后手顺序。

(2)这里的值的数量和顺序要和列名的数量和顺序一致,值的类型和列名的类型一致。

 

Oracle 中,日期是国际化的,不同的区域安装的数据库,默认的日期格式不同,

因此为了程序便于移植,日期的输入要使用 TO_DATE 函数对日期格式化后输入,采
用格式化字符串对日期进行格式化时,格式化字符串中字符不区分大小写,常见的
格式化字符如下:
1. yyyy 表示四位年份
2. mm 表示两位月份,比如 3 月表示为 03
3. dd 表示两位日期
4. hh24 表示小时从 0-23hh12 也表示小时从 0-11
5. mi 表示分钟
6. ss 表示秒
③ 在遇到存在默认值的列时,可以使用 default 值代替。
commit 是把用户操作(添加、删除、修改操作)提交,只有提交操作后,数据才
能真正更新到表中,否则其他用户无法查询到当前用户操作的结果。
Oracle 中,一个 INSERT 命令可以把一个结果集一次性插入到一张表中。使用的语句
是: INSERT INTO

比如说:

insert into info infos2 select * from yizheninfos;

这种情况,infos2和yizheninfos这两张表中的数据类型必须每一类的数据类型一致,这样就可以把yizheninfos表中的数据全部的插入到infos2这个表中

(这里也可以再后面的select 语句中添加条件,把符合一定条件的内同插入到infos2这个表中)

》》4.4更新数据

语法结构:

update  表名  set   列名1=值1,列名2=值2.....where  条件

这里执行完update,或者insert等之后需要执行commit 操作,具体原因参考:http://www.cnblogs.com/smail-bao/p/5431193.html

 

》》4.5 删除数据

删除数据的语法是:

delete  from 表名 where 条件

人气教程排行