Tag: Table

Selecting From One Table And Placing in Another in Mysql

How To Learn In Your Sleep
This System Not Only Gives You Back Some of Those Lost Hours it Will Also Have You Learning New Material Faster and More Accurately in Seven Days or Less – 100% Guaranteed! An in depth course of the Sleep-Learning method. Gain extra hours every day!
How To Learn In Your Sleep

Handling MySQL Events with Triggers and Procedures Using SQL – Part 10

Introduction
This is part 10 of my series, Handling MySQL Events with Triggers and Stored Procedures Using SQL. You must have read all the different parts of the series (or equivalent tutorials from elsewhere) up to this point. In this part of the series, we learn how to select data from one table row and place (insert or update) in a row of another table in MySQL. We are dealing with MySQL 5.1.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Project Description
We already have an Invoice table whose notation is:

    Invoice (InvoiceID, DateAndTime);

Let us create an InvoiceDetails table. An Invoice table is like a sales table. An InvoiceDetails table is like a SaleDetails table.

– Start the server and connect to it; choose the database, wholesale.
– Type and execute the following statement to create the InvoiceDetails table:

CREATE TABLE InvoiceDetails
(
    InvoiceID INTEGER NOT NULL,
    ProductID INTEGER NOT NULL
);

You should have a positive feedback. There are only two columns in the table.

So now we have an InvoiceDetails table whose notation is:

    InvoiceDetails(InvoiceID, ProductID)

For one invoice a customer may require three products. We shall write a short program of SQL statements that will insert the row information for the invoice table. Since the InvoiceID is autonumber, the program will read the number given by the DBMS and then use it to insert the InvoiceIDs (which are the same) for corresponding three products in the InvoiceDetails table.

The Code
Here is the program. Try it:

INSERT INTO Invoice () VALUES ();
SET @var = NULL;
SELECT MAX(InvoiceID)
FROM Invoice
INTO @var
INSERT INTO InvoiceDetails (InvoiceID, ProductID) VALUES (@var, 13);
INSERT INTO InvoiceDetails (InvoiceID, ProductID) VALUES (@var, 5);
INSERT INTO InvoiceDetails (InvoiceID, ProductID) VALUES (@var, 12);

Explanation of Code
The first line in the SQL code inserts a new row into the Invoice table. The parentheses are empty because for the two columns in the Invoice table, it is the DBMS that gives the values. The next statement creates the variable that will hold the most recently inserted InvoiceID in the Invoice table.

Next you have the SELECT statement. Since an InvoiceID value in the Invoice table is AUTO_INCREMENT, the most recently added InvoiceID is the maximum number in the column, everything being equal. So the SELECT statement reads this maximum number. The INTO clause of the SELECT statement copies the value read from the Invoice table to the variable of the SET statement.

The last three statements insert corresponding rows into the InvoiceDetails table. The three products are requested in one invoice, so the InvoiceID in this table for the three products has to the last one in the Invoice table. At the moment, this InvoiceID is held by the variable, @var. So this variable is in the last three statements.

The project has been accomplished.

– Stop the database, drop the connection and stop the server in one command.

I hope you are appreciating the series. We take a break here and continue in the next part.

Chrys

To arrive at any of the parts of this division, type the corresponding title below in the search box of this page and click Search (use menu if available).

Handling MySQL Events with Triggers and Procedures Using SQL – Part 1
Handling MySQL Events with Triggers and Procedures Using SQL – Part 2
Handling MySQL Events with Triggers and Procedures Using SQL – Part 3
Handling MySQL Events with Triggers and Procedures Using SQL – Part 4
Handling MySQL Events with Triggers and Procedures Using SQL – Part 5
Handling MySQL Events with Triggers and Procedures Using SQL – Part 6
Handling MySQL Events with Triggers and Procedures Using SQL – Part 7
Handling MySQL Events with Triggers and Procedures Using SQL – Part 8
Handling MySQL Events with Triggers and Procedures Using SQL – Part 9
Handling MySQL Events with Triggers and Procedures Using SQL – Part 10
Handling MySQL Events with Triggers and Procedures Using SQL – Part 11
Handling MySQL Events with Triggers and Procedures Using SQL – Part 12
Handling MySQL Events with Triggers and Procedures Using SQL – Part 13
Handling MySQL Events with Triggers and Procedures Using SQL – Part 14
Handling MySQL Events with Triggers and Procedures Using SQL – Part 15
 

Written by Chrys

Learn to speak Korean – audio and text
Learn to speak the Korean language using audio and ebook.
Learn to speak Korean – audio and text

Learn Web Designing – Joomla Made Easy

This book is intended to expose the fundamental know-how of Joomla. Though, it may not be possible to cover everything but what is provided in this book is sufficient in making one to cover as much as one can and enough to make one an expert as long as one never stops learning and researching.

Almost every explanation is accompanied with illustrative images. The secret to this is to guide the learner to get it right quickly and easily. Once you get it right, try repeating the examples

List Price: $ 31.96

Price:


Database Table Data Types

Database Table Data Types
Database Essentials – Part 3
Division 1

Introduction
This is part 3 of my series, Database Essentials. Database Essentials is division 1 of a set of tutorials I have on Database. Data Types also known as Data Domains refers to the kind of data that goes into a column of a table. In this part of the series we look at database Data Types. If the values of a column are whole numbers then the data type of that column is integer. If the values of a column are strings then the data type for that column is text. There are other data types; we look at them in this part of the series.

Byte and Kilo
The length of a message is measured in bytes. One byte is one character. Kilo abbreviated K in computer software is 2 raise to the power 10.

Implementation
After designing the tables for a database, you need to code it (implement it) in a database management system. There are many DBMS. They come in different software packages such as Microsoft Access, Oracle, MSSQL, MySQL, Sybase, etc. The data types I describe to you in this tutorial are those of Microsoft Access. Those for the other packages are similar. When you are creating a table in a DBMS you have to specify the data type for each column in the table.

Text
This refers to phrase composed of letters and/numbers. This data type can be used for an address column for example. It can also be used for a column that has numbers that do not require calculations. Examples of sets of numbers that are not used for calculation are phone numbers and postal code. If you have a column in a table for phone numbers or postal code, its data type should be text. An example of a set of numbers that is used in calculation is money figures. The maximum size of the text data type is 255 bytes. So, if you declare a column as having a data type of text, none of its cell content should be longer than 255 bytes.

Memo
This data type is used for lengthy text (which may include numbers). You can use this data type for columns that have notes or descriptions. The maximum size for this data type is Ƞ,000 characters.

Integer
This is a whole number data type. You use this data type for a column that has whole numbers. If you have a drinking bar for example, you may be interested in the number of bottles of drinks bought each day. You might have a database table, which has a column with the name, noOfDrinks; each row in such a table can represent a day’s information. So each table cell for the noOfDrinks column will have a whole number, for number of drinks sold on a day.  An integer can be up to 2 bytes long.

Floating
The integer data type takes only whole numbers. A whole number does not have a fractional part. If you would use numbers with fractional parts then you need the Floating data type. The floating data type accepts numbers with fractional parts. However, the fractional part has to be in decimal. Examples of floating numbers are: 23.758, ጤ.4, and 589652.7454. This kind of numbers cannot be put in table cells whose column has been declared to have integers; you have to put them in cells whose column has been declared to have floating numbers.

Currency
Currency is a data type. This is for money. You can have a table column for money. There are many currencies in the world. Dollar and Euro are examples. The currency data type can be up to 8 bytes long. You may wonder why the floating data type is not used for currency. The reason is that money values are usually large (large whole number part) and need more precision. For example, accountants in the bank work with a lot of money number precision. They do not want any one penny to be unaccounted for.

Date/Time
Date/Time is a data type. An example of a date is 02-04-2009. There is more to the Date/Time data type. Read the documentation of the particular DBMS to know exactly how you have to type or input the date and/or time. The Date/Time data type can be up to 8 bytes long.

Boolean
With this data type a cell of a column can have either “true” or “false”. A data type is specified for a column (not for a row). Some DBMS may take “yes” for true and “no” for false or “on” for true and “off” for false. The Boolean data type is known to have a size of one bit.

AutoNumber
This data type is typically used for a surrogate key that we saw in the previous part of the series. The difference between consecutive numbers in the cells of the column of this data type is 1 unit. Any of these numbers can be up to the size of 16 bytes.

BLOB
This data type is used for files. Have you ever imaging that files can be stored in a database? You can have one column in a table and each cell in the column will hold a file. What are files? You already know them. When you type a document in a word processor or text editor and save the document, the saved document is a file. Each image you see on a web page on the Internet is stored as a file in the hard disk of an Internet server. There are all kinds of files. Some files are executable files, in the sense that when you double click their icons within the operating system, they perform some task.

The BLOB data type is used to store any of these files; that is the BLOB data type is meant for storing files. It is the code of the file that is stored in a database table cell. The database itself cannot use the file. If the file is to be used, a copy of it has to be sent to some application, such as the one (word processor) that was used to create the file.

BLOB stands for Binary Large OBjects. This data type is used for storing files. If you declare a column of a database table to have the data type BLOB, then each cell of the column will be able to hold the code of a file.

Actually the BLOB data type is used for holding anything that is large in code form, which the DBMS does not really use. The DBMS can use numbers in a number column to find averages and totals, but is not expected to use the cell contents of the BLOB types to do anything.

Table Column Design Worksheet
For every table in the database there has to be a Table Column Design Worksheet. The Table Column Design Worksheet is not a table that holds data. It holds information about the data table itself. You will have to open the following link in your browser window tab to see an example.

        http://www.cool-mathematics.com/diagrams/design-worksheet-table.htm

The Table Column Design Worksheet has rows. Each of the rows gives information about a column in the data table. In the browser window tab you have opened, Table 3.1 shows an employee table. The diagram below shows the Table Column Design Worksheet for the employee table. Employee table is the data table.

In the Table Column Design Worksheet there should be at least three columns. There is first off all the entity name and the table name. The entity name is just the name the users have for the table. The table name is the database table name, which is the name you will use in the implementation (coding). Both the entity and table name can be the same.

The first column in our Table Column Design Worksheet lists the data table column names. The second column gives the data type for each column in the data table. The third column gives the description of each column in the data table. For this third column, where the name of the column of the data table is explicit (e.g. LastName), the description is not given.

Table Column Design Worksheet does not have to be coded with the database during implementation. You can have it in a notebook (paper book) in your office. However, some people create separate database tables during the implementation phase in the DBMS for the Table Column Design Worksheets for each of the data tables of the database. Remember, the data tables, which are the database tables (e.g. employee) will be coded in the DBMS during implementation.

For the Table Column Design Worksheet of the employee table, only the AutoNumber and the Text data types feature. This is alright; all the data types do not have to feature in a Table Column Design Worksheet. The data types that feature, depend on the actual data table columns data. If we had something like a Date_Hired column in the employee table, then the Date/Time data type would have featured in the Table Column Design Worksheet. Pictures are stored in the computer in files: one picture as a file. If we had a column for employee pictures in the employee table, then the BLOB data type would have been used in the employee table. The employee table would have a column whose cells have the code files for each of the employee picture. The Table Column Design Worksheet would also have a corresponding row with the data type, BLOB. In the case of pictures, the user would need a program that would decode the code files for the pictures in order to see the pictures.

That is what I have for you for this tutorial. Let us end here and continue in the next part of the series.

Chrys

To arrive at any of the parts of this series, just type the corresponding title below and my name, Chrys, in the Search Box of this page and click Search (use menu if available):

Database
Database Table Keys
Database Table Data Types
Database Associations Overview
Database N-to-N Relationships
Database N-ary Association
Aggregation Association
Composition Association
Generalization Association
Reflexive Association
Computed Values
Database Events
 

Written by Chrys

Learning Oracle Reports 2.5: A Tutorial for Report Designers

List Price: $ 29.95

Price:

More Oracle Tutorial Products

Comments Off more...

Rectifying Error Message “Unsupported extension used for table” in MySQL database

Jee Juh – Hip Hop Beats, Rap Instrumentals, Royalty Free Music
Rap Beats for Sale. Royalty free instrumental music for hip hop artists, movie soundtracks, and multimedia developers. Users can Preview, purchase, and download.
Jee Juh – Hip Hop Beats, Rap Instrumentals, Royalty Free Music

Article by Naveen Kadian

Naveen Kadian is a self employed Internet entrepreneur and product reviewer Stellar Phoenix Database Recovery For MySQL is the premier MySQL Repair tool that can instantly repair and restore corrupt MySQL databases. It supports InnoDB (.ibdata,.ibd and.frm) and MyISAM (.myd,.myi and.frm) files.










IM Video Vault
Grab private label rights to 30 of the most popular how-to videos in the internet marketing niche.
IM Video Vault


  • Copyright © 1996-2010 sql tutorial for beginners,sql server tutorial,sql tutorial pdf,sql tutorials. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress