考研 408 × 求职面试
知识点 · 数据库系统 · 基础
每日一题 · 数据库系统
解析
面试小贴士 · 数据库系统
代码实现 · C
#include <stdio.h>
#include <string.h>
/* 关系的每一列都有属性名和对应的域 */
typedef enum { MALE, FEMALE } Gender; /* 性别域 */
/* 元组:关系中的一行,字段即各属性的分量 */
typedef struct {
int id; /* 属性 学号, 域: 正整数 */
char name[20]; /* 属性 姓名, 域: 字符串 */
int age; /* 属性 年龄, 域: 0~150 */
Gender gender; /* 属性 性别, 域: {男,女} */
} Student;
/* 校验分量是否落在其域内,是则输出该元组 */
int insert(const Student *t) {
if (t->id <= 0 || t->age < 0 || t->age > 150) {
printf("元组超出属性域,拒绝插入\n");
return 0;
}
printf("元组: %d %s %d %s\n", t->id, t->name, t->age,
t->gender == MALE ? "男" : "女");
return 1;
}
int main(void) {
Student s1 = {1001, "张三", 20, MALE};
Student s2 = {1002, "李四", 200, FEMALE}; /* age 越界 */
insert(&s1);
insert(&s2);
return 0;
}