Mysql database

i have a very basic database:
[COLOR=Red]CREATE TABLE shirts (
id int(6) unsigned NOT NULL auto_increment,
shirt type varchar(100) NOT NULL default ‘’,
price decimal(3,2) NOT NULL default ‘0.00’,
PRIMARY KEY (id)
) TYPE=MyISAM;

INSERT INTO shirts VALUES (1, ‘shirt type 1’, ‘17.99’);
INSERT INTO shirts VALUES (2, ‘shirt type 2’, ‘17.99’);
INSERT INTO shirts VALUES (3, ‘shirt type 3’, ‘17.99’);

[COLOR=Black]now i was wondering how i would add a shipping cost, transaction cost, taxes and all that would i just go and make a line like this [/COLOR][/COLOR][COLOR=Red]tax decimal(3,2) NOT NULL default ‘0.00’, [COLOR=Black]for each of them? and then just fill it in values accordingly? [/COLOR][/COLOR][COLOR=Red]
[/COLOR]

Depends - what exactly do you need to do? It sounds like you want a list of stock items (shirts) and a list of transactions (sales maybe) with a header/lines

If so do you need an explanation of how to set this all up in a database structure and how to retrieve/insert the data?

yes it sounds like you got it. i want a list of stock items which would be the different type of shirts. and then each shirt would have a price 17.99 but also i want to a tax cost, shipping cost, and transactiopn cost, added to the 17.99. but letting the person whos buiying the shirt now the costs involed.

Well I’d start by changing the name of the table to ‘items’ as all your items should go in there regardless of type - you can add columns to define the types of objects later if all you’ve got are shirts at the moment.

You need your ‘Items’ table and you will also need an ‘Order Header’ table and an ‘Order Lines’ table. The header will hold basic information about the purchase and serve to group the lines into one purchase.

Depending on the level of detail and how much customer information you capture your header should at least contain:

The header ID
Some buyer details
It’s also useful to have the order total here to save counting the lines

The line table should contain:

Line No
Header ID (which you will use to link to the header)
Item ID (links to the item table)
Selling figures (Qty/Cost per item/line total etc)

When you have created those tables filling them is just a case of either inserting the data with SQL queries or writing a stored procedure to handle adding the header/lines.

The data is now relational which means there should be one order in the header table and one or more lines in the lines table which relate to the order table

You can retrieve order information using queries such as

SELECT *
FROM order_header
inner join order_lines on order_header.orderid = order_lines.orderid
inner join items on order_lines.itemid = items.itemid

thank you for the information your gave ill try it out and see how it goes.

would this be a good example of a oder header table

[COLOR=Red]DROP TABLE IF EXISTS order header;
CREATE TABLE order header (
od_id int(10) unsigned NOT NULL auto_increment,
od_date datetime default NULL,
od_last_update datetime NOT NULL default ‘0000-00-00 00:00:00’,
od_status enum(‘New’, ‘Paid’, ‘Shipped’,‘Completed’,‘Cancelled’) NOT NULL default ‘New’,
od_memo varchar(255) NOT NULL default ‘’,
od_shipping_first_name varchar(50) NOT NULL default ‘’,
od_shipping_last_name varchar(50) NOT NULL default ‘’,
od_shipping_address1 varchar(100) NOT NULL default ‘’,
od_shipping_address2 varchar(100) NOT NULL default ‘’,
od_shipping_phone varchar(32) NOT NULL default ‘’,
od_shipping_city varchar(100) NOT NULL default ‘’,
od_shipping_state varchar(32) NOT NULL default ‘’,
od_shipping_postal_code varchar(10) NOT NULL default ‘’,
od_shipping_cost decimal(5,2) default ‘0.00’,
od_payment_first_name varchar(50) NOT NULL default ‘’,
od_payment_last_name varchar(50) NOT NULL default ‘’,
od_payment_address1 varchar(100) NOT NULL default ‘’,
od_payment_address2 varchar(100) NOT NULL default ‘’,
od_payment_phone varchar(32) NOT NULL default ‘’,
od_payment_city varchar(100) NOT NULL default ‘’,
od_payment_state varchar(32) NOT NULL default ‘’,
od_payment_postal_code varchar(10) NOT NULL default ‘’,
PRIMARY KEY (od_id)
) TYPE=MyISAM AUTO_INCREMENT=1001 ;

[COLOR=Black]if this is a good example than im having a little trouble with making the oder lines table how do i go about linking the order header table and the items table to it and make it counting the lines[/COLOR]
[/COLOR]

Is there a space in your table name? I don’t think that’s a good practice. :stuck_out_tongue:

Also, why do you have first_name through postal_code repeating twice? Are you really sure you’re going to have only 2 people or something?

Otherwise, it looks correct (right now).

why do you have first_name through postal_code repeating twice?

That’s because the person who pay maybe different from the person who will receive the product.
Making the difference between the shipping address and the billing address is quite common :wink:

Oh, duh, didn’t even notice that.

Assuming you aren’t storing customer address details this would be fine for an order header. If you do wish to store customer address information (and have customer logins) you will need more tables.

I will assume this is all you need so for now you just need to define the lines table and you can input some test data and run the queries to grab some relational data

Oh yeah and I wouldn’t put spaces in table names just in case - use the underscore _ instead

thanx for the tip, defining the lines table; ok hopefully i understood this one lol im assumming that it would be to the effect of this

[COLOR=Red]DROP TABLE IF EXISTS order_item;
CREATE TABLE order_item (
od_id int(10) unsigned NOT NULL default ‘0’,
it_id int(10) unsigned NOT NULL default ‘0’,
od_qty int(10) unsigned NOT NULL default ‘0’,
PRIMARY KEY (od_id,pd_id)
) TYPE=MyISAM;

[COLOR=Black]od_id= order header, it_id= items table. now if u don’t mind me asking what does TYPE=MyISAM mean

[/COLOR][/COLOR][COLOR=Red]DROP TABLE IF EXISTS items_table;[/COLOR]
[COLOR=Red]CREATE TABLE items_table (
it_id int(6) unsigned NOT NULL auto_increment,
shirt type varchar(100) NOT NULL default ‘’,
price decimal(3,2) NOT NULL default ‘0.00’,
PRIMARY KEY (id)
) TYPE=MyISAM;

INSERT INTO items_table VALUES (1, ‘shirt type 1’, ‘17.99’);
INSERT INTO [/COLOR][COLOR=Red]items_table[/COLOR][COLOR=Red] VALUES (2, ‘shirt type 2’, ‘17.99’);
INSERT INTO [/COLOR][COLOR=Red]items_table[/COLOR][COLOR=Red] VALUES (3, ‘shirt type 3’, ‘17.99’);

[COLOR=Black]items table. and yea i don’t plan on having a user login. just plan on selling t-shirts for now. [/COLOR]
[/COLOR]

Looks good so far…

ISAM is a type of database engine - Indexed Sequential Access Method

It’s just the type of storage. MySQL supports a couple of methods - this ones usually the best for smaller datasets and website style access which is why it’s the default

oh ok thanx, the tax shipping and transaction costs are they added to the lines table or items table?
and they are linked already to each other in the appropriate way. or is that something ill have to test?

For your first question, this depends on whether you calculate the costs per line or per order. Whichever one works for you.

The second question:

Database tables are not really ‘linked’ until you query them - granted you can put contraints on to stop lines being put in the lines table without a corresponding header, but still this is not really ‘linking’ the tables, it simply ensures data integrity.

You may want to use stored procedures for manipulating the data - if you don’t know what these are:

They are basically pre-compiled queries which more often than not contain a bunch of parameters that you pass in. Often they are a bit quicker than regular queries because of the pre-compiled nature (the SQL server does not need to figure out the execution plan for the query as it’s already worked one out)

You call them via the exec statement and then pass the parameters accordingly - I’m not 100% on the MySQL syntax as I’m more a microsoft SQL user but the details should be in the MySQL docs.

The advantage over a stored procedure is that once you’ve written it you’ve only got one place to maintain your SQL and you can run it from any place in your application which means that you don’t need to write an INSERT statement directly in your app and you won’t find yourself creating bugs in a piece of logic which is supposed to perform the same task as another piece.

Saying that however, most well written applications should seperate data, business and presentation into 3 distinct layers. Basically you would be writing some classes or modules for data access, some classes which will be your business objects and finally classes which will render the business data to the screen.

So your simple model will look like:

Data -> Business Layer -> Presentation Layer

To actually ‘link’ your data your SQL statements/stored procedures need to associate fields in each table with each other.

There are many ways to get data and the T-SQL language can be very frustrating if you aren’t exactly sure what you are doing with it.

You might want to read up on SQL - in your case you probably won’t need to write and difficult complex queries, a simple select and join should do

You will just need to select the data you want from the tables you want and join the linking fields together

SELECT
<distinct fieldname from either table>,
<tablename.non-distinct-fieldname>,
<tablename.some-other-field>
FROM
<tablename>
INNER JOIN – Two dashes in MSSQL are a comment, not sure about MySQL though, this inner join retrieves
– only data where the two or more specified fields match - if there is no match for a row
–in the second table the row will not be retrieved
<second-tablename>
ON <tablename.fieldname> = <second-tablename.fieldname> – The joining fields
WHERE – The where clause - remember this where happens ‘after’ the join
<tablename.fieldname> = ‘SomeValue’

So in your case something like

SELECT
order_header.od_id,
order_header.od_address1, – and address 2, address 3 etc etc
order_lines.quantity,
order_lines.linetotal,
items.description
INNER JOIN
order_header.od_id = order_lines.od_id
INNER JOIN
items on items.it_id = order_lines.it_id
WHERE order_header.od_id = 5

So this query will basically get the details of an order with an od_id of 5. It gets the line details and also the item description for each line item. Of course the lines MUST exist in the lines table or the order header won’t be returned. The item must also exist in the items table.

Read up on T-SQL and the different types of joins. I have to say that writing your own shopping cart with no real knowledge of SQL/PHP etc is no easy task. If you need any help I’ll help you out! :stuck_out_tongue:

thanx for your help i appreciate it, i probably will need some help with the shopping cart. so whats the better way to do it through the lines table or through the items table for the tax,shipping,etc… what i imagined would happen was that the cart would be linked to the items table allowing the person to drag an item that would be listed in the items table, drop it into the cart, and when u did that u saw a pic of the item with a drop down list for size, and quanity. and then they would press “proceed to checkout” button which would be linked to the order header table which a page would come up having all the same info thats on the order header table. and then checkout would be linked to the lines table. and each table would have a page. is that the common procedure on how its done?