oracle数据库表约束条件使用示例
时间: 2024-02-05 19:02:52 浏览: 87
Oracle数据库表约束条件可以分为列级别约束条件和表级别约束条件。列级别约束条件是指对表中某一列的约束条件,而表级别约束条件是指对表中多列的约束条件。下面是Oracle数据库表约束条件的使用示例:
1. 创建一个包含列级别约束条件的表:
```sql
CREATE TABLE student (
id NUMBER(10) PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
age NUMBER(3) CHECK(age >= 0 AND age <= 150),
gender CHAR(1) CHECK(gender IN ('M', 'F'))
);
```
2. 创建一个包含表级别约束条件的表:
```sql
CREATE TABLE course (
id NUMBER(10),
name VARCHAR2(20),
teacher VARCHAR2(20),
PRIMARY KEY (id, name),
FOREIGN KEY (teacher) REFERENCES teacher(name)
);
```
3. 添加列级别约束条件:
```sql
ALTER TABLE student ADD CONSTRAINT student_age CHECK(age >= 0 AND age <= 150);
```
4. 添加表级别约束条件:
```sql
ALTER TABLE course ADD CONSTRAINT course_teacher FOREIGN KEY (teacher) REFERENCES teacher(name);
```
阅读全文