Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better create new table or add columns in existing table

Tags:

sql

sql-server

In my database, i have payment table paymentid, amount, duedate, approvalcode, status, transactionid etc.

payment table has already 15 columns.

if i want to store tip, tax, disount against paymentid.

what is better

create new paymentdetails table with one to one relationship

or

add columns in same payment table?

pls help.

payment table has almost more than 50000 records. so what is really good for database.

like image 894
Sai Sherlekar Avatar asked Jun 03 '13 09:06

Sai Sherlekar


People also ask

What are the advantages of creating a table from existing table?

Here are two scenarios I see for creating a table based on an existing one: create a summary table from detail table - faster read of data based on precalculated results; secondly, reliability would be high as calculated fields in summary can be traced back to source.

Can we add column to the existing table?

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

Why is it better to have multiple separate tables?

It saves space and its flexible. Realistically, you cant really put everything in one table.

Can we create a table from an existing table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.


2 Answers

With such small tables I don't see a reason for not putting the new columns in the existing table.

You would do that if

  • you hit some kind of limits (number of columns, total size of table) of the RDBMS
  • the data in the columns is huge and only used in few queries
like image 124
Jens Schauder Avatar answered Sep 28 '22 16:09

Jens Schauder


Generally, I work on the premise that attributes of something/entity/object should be stored against the something/entity/object. Typically, I only split out into a new table when there is a one to many relationship - so an attribute that has multiple values (e.g. person and qualifications held)

I would add them to the payment table - although perhaps you have an order table they would more suitably relate to?

You will need to keep in mind whether you will need indexes over these columns though - if you add indexes, you could overburden your table and deteriorate performance but you would probably only add indexes in instances where you used the columns in joins and they don't sound like the sort of thing you would need to join on.

like image 45
Steph Locke Avatar answered Sep 28 '22 17:09

Steph Locke