按本linkBerkeley DB中是否存在/需要'主鍵'和'外鍵'概念?如下所示
甲表與在SQL主鍵(student_id
)創建的:
CREATE TABLE students(student_id CHAR(4) NOT NULL,
lastname CHAR(15), firstname CHAR(15), PRIMARY KEY(student_id));
在Berkeley DB,我們可以這樣做,使用以下代碼:
struct student_record {
char student_id[4];
char last_name[15];
char first_name[15];
};
DB *dbp, *sdbp;
int ret;
/* Open/create primary */
if ((ret = db_create(&dbp, dbenv, 0)) != 0)
handle_error();
if ((ret = dbp->open(dbp,
"students.db", NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
handle_error();
從應用程序的角度來看,將事物放入數據庫的工作方式與沒有輔助索引時完全相同;可以簡單地將記錄插入到主數據庫中。在SQL一會做到以下幾點:
INSERT INTO student
VALUES ("WC42", "Churchill ", "Winston ");
,並在Berkeley DB,一個做:
struct student_record s;
DBT data, key;
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
memset(&s, 0, sizeof(struct student_record));
key.data = "WC42";
key.size = 4;
memcpy(&s.student_id, "WC42", sizeof(s.student_id));
memcpy(&s.last_name, "Churchill ", sizeof(s.last_name));
memcpy(&s.first_name, "Winston ", sizeof(s.first_name));
data.data = &s;
data.size = sizeof(s);
if ((ret = dbp->put(dbp, txn, &key, &data, 0)) != 0)
handle_error(ret);
我的問題:
是我們在設置主鍵(student_id
)當我們調用db_create()
?如果不是,基於student_id
字段的行如何是唯一的?
這是C/C++嗎? – 2014-09-23 13:44:14
@PraveenKumar:什麼是「C/C++」? – 2014-10-03 07:34:16