473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is the structure of this example database ok?


I have a question about creating a simple database. Here are the 2 tables.
They are linked by the primary/foreign key 'client_id'. I think that I
understand that part.

I have 'KEY birthdate(birthdate)' in the clients table definition. I'm not
sure when I should have that. When is it desirable to use the 'KEY' in the
definition? If I wanted to be able to often and quickly sort the clients table
by lastname, firstname, or birthdate, should I use the 'key' phrase? Ie, would
the clients table sort faster on birthdate than on lastname in this example?
If I wanted to be able to efficiently sort on all columns then should I have a
'key' phrase for each column in the table?

CREATE table clients (
client_id int(10) unsigned NOT NULL auto_increment,
birthdate date NOT NULL default '0000-00-00',
lastname char(15) NOT NULL,
firstname char(15) NOT NULL,
PRIMARY KEY (client_id),
KEY birthdate(birthdate)
);

CREATE table sales (
saleno int(10) unsigned NOT NULL auto_increment,
client_id int(10) unsigned NOT NULL,
description char(15) not null,
amount float(10,2) NOT NULL,
PRIMARY KEY (saleno),
KEY amount(amount),
constraint foreign key (client_id) references clients (client_id)
);

thanks,
Jim

Jul 20 '05 #1
2 1518

"James Johnson" <jj@yaaho.com> wrote in message
news:5h********************************@4ax.com...

I have a question about creating a simple database. Here are the 2 tables.
They are linked by the primary/foreign key 'client_id'. I think that I
understand that part.

I have 'KEY birthdate(birthdate)' in the clients table definition. I'm not
sure when I should have that. When is it desirable to use the 'KEY' in the
definition? If I wanted to be able to often and quickly sort the clients table by lastname, firstname, or birthdate, should I use the 'key' phrase? Ie, would the clients table sort faster on birthdate than on lastname in this example? If I wanted to be able to efficiently sort on all columns then should I have a 'key' phrase for each column in the table?

CREATE table clients (
client_id int(10) unsigned NOT NULL auto_increment,
birthdate date NOT NULL default '0000-00-00',
lastname char(15) NOT NULL,
firstname char(15) NOT NULL,
PRIMARY KEY (client_id),
KEY birthdate(birthdate)
);

CREATE table sales (
saleno int(10) unsigned NOT NULL auto_increment,
client_id int(10) unsigned NOT NULL,
description char(15) not null,
amount float(10,2) NOT NULL,
PRIMARY KEY (saleno),
KEY amount(amount),
constraint foreign key (client_id) references clients (client_id)
);

thanks,
Jim


Hi Jim,

This is a big issue. In general indexes make finding rows in your database
faster. So if you search on birthdate often you 'may' find performance
benefits. Always use EXPLAIN to see just what's going on. But you must read
chapter 7 of the manual to really understand what's happening.

BTW, the way you have your database structured a salesperson can only have
one client. You made it a primary key. Is this what you meant? Or is this
table a row for each client with sales info for each client?

Regards,
Rich
Jul 20 '05 #2
On Thu, 23 Dec 2004 14:14:04 GMT, "Rich R" <rr***@cshore.com> wrote:

"James Johnson" <jj@yaaho.com> wrote in message
news:5h********************************@4ax.com.. .

I have a question about creating a simple database. Here are the 2 tables.
They are linked by the primary/foreign key 'client_id'. I think that I
understand that part.

I have 'KEY birthdate(birthdate)' in the clients table definition. I'm not
sure when I should have that. When is it desirable to use the 'KEY' in the
definition? If I wanted to be able to often and quickly sort the clients

table
by lastname, firstname, or birthdate, should I use the 'key' phrase? Ie,

would
the clients table sort faster on birthdate than on lastname in this

example?
If I wanted to be able to efficiently sort on all columns then should I

have a
'key' phrase for each column in the table?

CREATE table clients (
client_id int(10) unsigned NOT NULL auto_increment,
birthdate date NOT NULL default '0000-00-00',
lastname char(15) NOT NULL,
firstname char(15) NOT NULL,
PRIMARY KEY (client_id),
KEY birthdate(birthdate)
);

CREATE table sales (
saleno int(10) unsigned NOT NULL auto_increment,
client_id int(10) unsigned NOT NULL,
description char(15) not null,
amount float(10,2) NOT NULL,
PRIMARY KEY (saleno),
KEY amount(amount),
constraint foreign key (client_id) references clients (client_id)
);

thanks,
Jim


Hi Jim,

This is a big issue. In general indexes make finding rows in your database
faster. So if you search on birthdate often you 'may' find performance
benefits. Always use EXPLAIN to see just what's going on. But you must read
chapter 7 of the manual to really understand what's happening.

BTW, the way you have your database structured a salesperson can only have
one client. You made it a primary key. Is this what you meant? Or is this
table a row for each client with sales info for each client?

Regards,
Rich

I thought that I had a sales table that can only have one client. Ie,
one sale to one client at a time? Isn't that correct? That's what I'm
trying to structure. I can't think of a need for a sale to have more
than one client.

Jim

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Douglas Buchanan | last post by:
Newbie to donnet This is an example of a structure given in vs.net help ============================ Private Structure Employee Public GivenName As String ' This employee's given name....
5
by: brett valjalo | last post by:
Hey Gang! SORRY ABOUT THE LENGTH! Nice to see some of the same faces around this place from way back when ... Whatta buncha CDMA addicts some o' y'all are ;) Don't get me wrong, I...
2
by: Maileen | last post by:
Hi, In my application, I want to load a MS Access database, open it and get the database structure (number of tables, name of tables and their structure - fields). how can i do it ? does...
2
by: David | last post by:
Hi all, using 1.1 I am creating a dynamic menu structure for my site, however, I may refer to the structure many times within a page. This would be fine if I was doing it all in the...
4
by: Sergei Minayev | last post by:
Hi All! Can you please help me with the following problem: I need to store a copy of local folders structure in MySQL database. I have chosen the following table structure for that:...
2
by: Thomas Kehl | last post by:
Hello. I am searching for a class which is able to update a sql-server db-structure about definitions for example in a xml-file. I have a database which structure have to be updated after a...
12
by: Steve | last post by:
I have been studying the Adjacency List Model as a means of achieving a folder structure in a project I am working on. Started with the excellent article by Gijs Van Tulder ...
5
by: Sergio Montero | last post by:
I have a MustInherits Base class that implements a custom IDataLayer interfase. IDataLayer expose CRUD methods. Base class constructor requires two parameters: ConnectionString TableName ...
0
by: remya1000 | last post by:
I have a field called Departments in my database. and I have 3 monitors. So when Page_Load, I need to check number of departments I have in database. And depends upon that number of departments I...
7
by: billelev | last post by:
I'm building a database and am a bit stumped about how to construct/link tables. I will describe the current configuration, then present the problem I am trying to solve. Currently: I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.