简体   繁体   中英

Creating SQL database. with multiple primary and foreign keys

I'm currently working through an assignment that is asking me to create a SQL database using SQL Server Express. This is what it's asking for: CREATE DATABASE USING SQL

And this is the code I have tried running;

CREATE DATABASE db_Library
Go

USE db_Library

CREATE TABLE tbl_library_branch 
(
    library_branch_branch_id INT PRIMARY KEY NOT NULL IDENTITY (1,1),
    library_branch_branch_name VARCHAR(50) NOT NULL,
    library_branch_address VARCHAR(50) NOT NULL
);

CREATE TABLE tbl_publisher 
(
    library_publisher_publisher_name VARCHAR(50) PRIMARY KEY NOT NULL,
    library_publisher_address VARCHAR(50) NOT NULL,
    library_publisher_phone INT NOT NULL
);

CREATE TABLE tbl_books 
(
    library_books_book_id INT PRIMARY KEY NOT NULL IDENTITY (1,1),
    library_books_title VARCHAR(50) NOT NULL,
    library_books_publisher_name VARCHAR(50) NOT NULL CONSTRAINT fk_library_books_publisher_name FOREIGN KEY REFERENCES tbl_publisher(library_publisher_publisher_name) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE TABLE tbl_book_authors 
(
    library_book_authors_book_id INT NOT NULL CONSTRAINT fk_library_book_authors_book_id FOREIGN KEY REFERENCES tbl_books(library_books_book_id) ON UPDATE CASCADE ON DELETE CASCADE,
    library_book_authors_author_name VARCHAR(50) NOT NULL
);

CREATE TABLE tbl_book_copies 
(
    library_book_copies_book_id INT NOT NULL CONSTRAINT fk_library_book_copies_book_id FOREIGN KEY REFERENCES tbl_books(library_books_title),
    library_book_copies_branch_id INT NOT NULL,
    library_book_copies_number_of_copies INT NOT NULL
);

CREATE TABLE tbl_book_loans 
(
    library_book_loans_book_id INT NOT NULL,
    library_book_loans_branch_id INT NOT NULL,
    library_book_loans_card_no INT NOT NULL,
    library_book_loans_date_out INT NOT NULL,
    library_book_loans_date_due INT NOT NULL
);

CREATE TABLE tbl_borrower 
(
    library_borrower_card_no INT PRIMARY KEY NOT NULL IDENTITY (1,1),
    library_borrower_name VARCHAR(50) NOT NULL,
    library_borrower_address VARCHAR(50) NOT NULL,
    library_borrower_phone VARCHAR(50) NOT NULL
);

Using the library "Books" as an example it looks like I need to have BoodID as a primary key and Title as a primary key, but you can't have more than one primary key per table..

Then I have to take BookID from the Book_Copies table and the Book_Loans table connect to the primary key of Title in Books?

I'm beyond stuck at this point and would appreciate any resources you think could help.

There is no need to be so verbose. I think you want something more like this (for two tables):

CREATE TABLE tbl_publisher (
    publisher_id int IDENTITY(1, 1) PRIMARY KEY,
    publisher_name VARCHAR(50) NOT NULL UNIQUE,
    address VARCHAR(50) NOT NULL,
    phone INT NOT NULL
);

CREATE TABLE books (
    book_id INT IDENTITY (1,1) PRIMARY KEY,
    title VARCHAR(50) NOT NULL UNIQUE,
    publisher_id INT NOT NULL CONSTRAINT fk_books_publisher__id FOREIGN KEY REFERENCES tbl_publisher(publisher_id) ON UPDATE CASCADE ON DELETE CASCADE
);

Notes:

  • Stick with synthetic primary keys for all the tables. That is, identity columns.
  • Other columns can be declared to be unique . That is fine.
  • I type quite fast. And yet I would quickly wary of typing library_book_ over and over. Such repetition just makes it harder to write and read queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM