1

enter image description here
(source: joescott.me)

I'm currently grappling with the following, as the title suggests i'm looking to reverse this table design into a valid create query for the table 'Book'

Am i right in thinking:

  • The title should be used as a primary key?
  • A foreign key reference publisherName on Publisher(name)
  • And another authorName on Author(name)

Which becomes:

CREATE TABLE Book
(
    Title varchar(30),
    ISBN INT UNIQUE,
    Cost dec(8,2),
    authorName varchar(30) REFERENCES Author(name),
    publisherName varchar(30) REFERENCES Publisher(name)
);

After mentioned alterations:

CREATE TABLE Book
(
    Title varchar(30) NOT NULL,
    ISBN INT UNIQUE PRIMARY KEY,
    Cost dec(8,2) NOT NULL,
    authorName varchar(30) REFERENCES Author(name),
    publisherName varchar(30) REFERENCES Publisher(name)
);
4
  • Wouldn't the ISBN be a better primary key for books? And where is your primary keys for the author and publisher tables? Aug 21, 2014 at 11:54
  • please clarify which DB you're planning to work on? The keywords vary across MySql, SQL Server and Oracle.
    – CodeNewbie
    Aug 21, 2014 at 11:55
  • @CodeNewbie Oracle! :)
    – Broak
    Aug 21, 2014 at 11:58
  • was an answer just deleted? it was a good one! I made the changes in the edit
    – Broak
    Aug 21, 2014 at 12:00

4 Answers 4

3

The title should be used as a primary key?

No. A primary key should be unique, and unchanging. There is no way to guarantee that there aren't two books with the same title. I believe ISBN is guaranteed unique and unchanging, although books exist without ISBNs (books that are not yet finished, books published before ISBNs became popular).

A foreign key reference publisherName on Publisher(name)

Again - you want the primary key for "publisher" to be unique, and unchanging. There's no guarantee that publisher names are unique, or unchanging. Typically, we create "publisherID" as primary keys, with either a GUID or incrementing integer.

And another authorName on Author(name)

As above

Also, I wouldn't include "numberOfTitles" in the publisher table - normalization suggests that we need to calculate this value, rather than store it.

1

If you are trying to make the best out of this bad design, you should go for your 2nd option:

  • Table [Publisher] with PK 'Name'.
  • Table [Author] with PK 'Name'.
  • Table [Book] with PK 'ISBN' and FK [Publisher].Name and another FK [Author].Name.

(PK should be standard UNIQUE and NOT NULL)

CREATE TABLE Book ( Title varchar(30) NOT NULL, ISBN INT PRIMARY KEY, Cost DECIMAL(8,2) NOT NULL, authorName varchar(30) REFERENCES Author(name), publisherName varchar(30) REFERENCES Publisher(name) );

Also with this dataset, your char lengths are fine. But in reality, INT will be too small to store 13 digit numbers for ISBN, and names can go up to 40+ chars easily, especially publishers.

1
  • All the answers are correct and very useful, i just found this answer the most approachable. thanks!
    – Broak
    Aug 21, 2014 at 14:09
0

You don't need to use duplicate columns (publisher name, author name) in your books table.

Just make a primary key column (Publisher_Id), (Author_Id) in both your Publisher & Authors table. Reference these columns as a foreign key in your books table & then you can get the publisher_name & author_name or any columns from both the tables using a SQL join.

So your books table would become

Book_Id(PrimaryKey)   Book_Name   Publisher_Id     Author_Id
     1             C# at a glance     100             21

You can use simple queries like

  select a.book_name,b.publisher_name,c.author_name from books a
  inner join publisher b on a.publisher_id=b.publisher_id
  inner join authors   c on a.author_id=c.author_id
  where a.book_id=1;

Don't set book_name as a primary field column. Instead you can use ISBN or make a separate book_id column. Also look into database normalization & perform the relevant forms upto 3NF minimum before you design your database depending upon your requirements. 1NF, 2NF, #NF, BCNF Normalization

1
  • Without altering the other two tables, is the revised query in my edit what we're looking for?
    – Broak
    Aug 21, 2014 at 12:02
-1

Data Structure Employee Details (Master Table) • Employee ID (Primary Key) (Alphanumeric) • Name (String) • Age(Integer) • Joining Date(Date) • Designation(String) Salary Details (Child Table) • Employee ID (Foreign Key) (Alphanumeric) • Salary ID (Primary Key) (Alphanumeric) • Year(Date) • Month(Date) • Gross Salary(Integer) • Deduction(Integer) • Net Salary(Integer) • Currency(String)

Prep pare a Master grid with sub grid- Master grid shows employee details and associated salary details shows in sub grid. Sub grid will have multiple rows like one employee have multiple salary based on year and month.

Top add & Delete icon for Adding or Deleting Employee Details Grid level add option for adding salary details in sub grid under each employee Pen icon for edit in raw level - edit employee or salary details Provide separate popup for adding / updating employee or salary details Add/Update pop should be with proper validations Use connection string in webconfig to connect to database All data fetching will be using on Views All data insertions, Update and delete will be using stored procedure

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 3, 2021 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.