Anda di halaman 1dari 20

Creating Tables

You must create your tables before you can enter data into them. Use the Create Table command. Syntax:

Create table tablename using filename (fieldname fieldtype(length), fieldname fieldtype(length), fieldname fieldtype(length));

Explanation:

Table names cannot exceed 20 characters. Table names must be unique within a database. Field names must be unique within a table. You may specify the data file to use. If you do not specify a data file, Scalable SQL will create one, using a .dat extension. The list of fields must be enclosed in parentheses. You must specify the field type.

Examples: Char -- a character string Float -- a number Date -- a date field Logical -- a logical field


Example:

You must specify the field length. The field length must be enclosed in parentheses. You must separate field definitions with commas. You must end each SQL statement with a semicolon.

Create tables TrnVendor and TrnAPDoc. We will use these tables in the exercises that follow:

1.

In the SQL text box, type:

Create table TrnVendor using 'TVendor.dat' (VendId char(10), Name char(30), Add1 char(30), City char(30), State char(3), CurrBal float(8), ExpAcct char(10));

Create table TrnAPDoc using 'TAPDoc.dat' ( VendId char(10), RefNbr char(10), DocDate date(4), OrigDocAmt float(8), CuryDocBal float(8), OpenDoc logical(2));

2. 3.

Execute the commands. To create the first table, click on First, the Run button located to the right of the SQL text box. You should receive the following message:

Scalable SQL informative status = -105. The CREATE statement completed successfully.

4. 5.

Click on Next to create the second table. You should receive the following message:

Scalable SQL informative status = -105. The CREATE statement completed successfully.

Importing Data

You can import data into SQLScope from another data source by using the Insert command. Before completing the following exercises, you must import data into the tables you created. The example given in this section will step you through the process.

Syntax:

Insert into tablename (fieldname, fieldname, fieldname) Values (@fieldname, @fieldname, @fieldname);

Explanation:

Before you can import, you must create a data file in one of the following formats:

Btreive unformatted (.unf) -- File layout.


ASCII

Logical, date, and time fields are in binary format. Each row is preceded by its length, followed by a comma. Each row is terminated with a carriage return. All data is represented in ASCII characters. A comma is used to separate fields. Character fields are enclosed in double quotes. Each row is terminated with a carriage return. Each row is preceded by a comma or a space. Each field and each row is a specified length. Each row is terminated with a carriage return. Each file is made up of two sections: header and data. Each row has a standard length. Each field has a standard length.

Standard Data Format (.sdf) -- File layout.


DIF (.dif)

The Values clause is used to specify substitution variables. Substitution variables must be preceded by an @ sign. Each field in the Insert statement must be in the same order as the corresponding field in the data file.

To import data:

Choose Import from the File menu to open the Import dialog box.

1. 2. 3. 4. 5. 6.

In the File Type box, specify the type of file you want to import. In the Drive box, specify the appropriate drive. In the Directories box, specify the appropriate directory. In the File Name box, specify the name of the file from which to import. If you would like the process to run in the background while you continue working, click on the Background field. Click on Import to begin the import process.

Example:

Import data into the TrnVendor and TrnAPDoc files.

1.

Download the files:

Click here to download a zip.file

2.

Type the following in the SQL text box:

Insert into TrnVendor (Vendid, Name, Add1, City, State, CurrBal, ExpAcct ) Values (@Vendid, @Name, @Add1, @City, @State, @CurrBal, @ExpAcct)

3. 4. 5. 6. 7. 8. 9.

Click on File on the Menu bar. Click on Import on the drop-down menu to open the Import dialog box. In the Drive box, specify the drive to which you downloaded the files in step #1. In the Directories box, specify the directory to which you downloaded the files in step #1. In the File Name field, type imvendor.sdf. Click on Import. You should receive the following message:

Importing has completed. Records: 30

10.

Type the following in the SQL Text box:

Insert into TrnAPDoc (Vendid, RefNbr, DocDate, OrigDocAmt, CuryDocBal, OpenDoc) Values (@Vendid, @RefNbr, @DocDate, @OrigDocAmt, @CuryDocBal, @OpenDoc)

11. 12. 13. 14. 15. 16. 17.

Click on File on the Menu bar. Click on Import on the drop-down menu to open the Import dialog box. In the Drive box, specify the drive to which you downloaded the files in step #1. In the Directories box, specify the directory to which you downloaded the files in step #1. In the File Name field, type imapdoc.sdf. Click on Import. You should receive the following message:

Importing has completed. Records: 50

Retrieving All Data


Select statements are used to retrieve data from SQL tables. The Select statement illustrated below retrieves all of the columns and rows from the named table. Syntax:

Select * from tablename;

Explanation:


Example:

A Select statement is a SQL statement that begins with the word "select." Select statements are used to retrieve data from SQL tables. An asterisk after the word "select" means retrieve all fields (columns). The name of the table from which you are retrieving data is specified in the From clause. Use a semicolon to signify the end of a SQL statement.

Retrieve all of the data from the TrnVendor table.

1.

In the SQL text box, type:

Select * from TrnVendor;

2. 3. 4.

Execute the command by clicking on First, the Run button located to the right of the SQL text box. The results will display on the screen. Use the horizontal scroll bar to move across the screen. The fields in the Vendor table will display alphabetically. Use the vertical scroll bar to move up and down the screen. Click on Stop to return to the original screen.

Results: VendId Name Add1 City State CurrBal ExpAcct

TV001

Wet Off Towels The Games All Here BedMakers Linen Bed Room Furniture, Inc.

66 Keep Dry Way 85 Olympic Drive 97 Comfort Zone 4 Hide A Way

Toledo

OH

1.31300E+001

7230

TV002

Warren

NY

4.53170E+002

4110

TV003

Chicago

IL

5.00000E+002

4120

TV004

Mercer

NJ

3.21700E+002

6080

TV005

Ray Block

90 Sunscreen Blvd. 99 Repellent Way 77 Sunset Strip 44 Broad Street 42 Deck Lane 101 Fizz Up Blvd. 1 Trim Way 80 Pulp Fiction Drive 44 User Friendly Dr. 26 Shelf Street 5819 Street Road 54 Note Street 67 Janice Blvd. 10 Electric Dog Street 6479 Bay Drive 1 Tutorial Lane 101 Systems Go 77 Service Blvd. 11 Boyardee Drive 98 New Worlds Blvd. 54 All Detergent Way 4268 Jethro Blvd. 23 Georgia Drive 1917 Douglas Way 1818 Johnson Blvd. 4 Holiday Way

Detroit

MI

1.00123E+003

7230

TV006

Mosquito No Bite Spot Out

Park Ridge

NJ

9.67240E+002

6510

TV007

Detroit

MI

1.60654E+003

6500

TV008

Big Tree Landscaping Hit the Deck

Monroe

NY

3.42980E+002

4110

TV009

Toledo

OH

2.34500E+001

4030

TV010

The Soda Factory The Freelance Paper People Softer Software Counter Productive No Waste Disposal Music Maker

Orlando

FL

0.00000E+000

4110

TV011

Mercer

NJ

-1.09230E+002

7230

TV012

New York

NY

6.17000E+002

6080

TV013

Monroe

NY

9.87367E+003

7230

TV014

Toledo

OH

4.28900E+001

6510

TV015

Park Ridge

NY

1.72098E+003

6500

TV016

Chicago

IL

4.10914E+003

4110

TV017

Food Four

Freeport

MA

6.42980E+002

4110

TV018

Computer Bytes Bayshore Consulting BayCon Group Cooperative Operatives Legal Lookup World Wide Learning U Enterprise Transport Against the Tide Clampett Oil

Youngstown

WA

3.45090E+002

7230

TV019

Bayshore

CA

4.98050E+002

4110

TV020

Burlington

NJ

5.10000E+001

4120

TV021

Dallas

TX

2.00000E+001

6080

TV022

Houston

TX

3.21516E+003

7230

TV023

Freeport

MA

7.71060E+002

6510

TV024

Freeport

MA

1.23498E+003

6500

TV025

Orlando

FL

6.47200E+001

4110

TV026

Dallas

TX

0.00000E+000

4030

TV027

Narrow Nest

Toledo

OH

1.60900E+003

4110

TV028

Distant Horizons Paltry Play

Chicago

IL

2.69800E+001

7230

TV029

Chicago

IL

1.00000E+003

6080

TV030

Make Shift Tilts

Chicago

IL

1.79323E+003

7230

Retrieving a Single Column


You can use SQL to retrieve a single column of data. Syntax:

Select fieldname from tablename;

Explanation:


Example:

Instead of an asterisk, the name of the field is specified in the Select clause. Field names are not case-sensitive. You can type field names in uppercase or lowercase letters.

Retrieve the vendor name from each record:

1.

In the SQL text box, type:

Select Name from TrnVendor;

2. 3. 4.

Click on First, the Run button located to the right of the SQL text box. The results will display on the screen. Use the vertical scroll bar to move up and down the screen. Click on Stop to return to the original screen.

Results:

Name

Wet Off Towels

The Games All Here

BedMakers Linen

Bed Room Furniture, Inc.

Ray Block

Mosquito No Bite

Spot Out

Big Tree Landscaping

Hit the Deck

The Soda Factory

The Freelance

Paper People

Softer Software

Counter Productive

No Waste Disposal

Music Maker

Food Four

Computer Bytes

Bayshore Consulting

BayCon Group

Cooperative Operatives

Legal Lookup

World Wide Learning U

Enterprise Transport

Against the Tide

Clampett Oil

Narrow Nest

Distant Horizons

Paltry Play

Make Shift Tilts

Retrieving Multiple Columns


You can use SQL to retrieve multiple columns. Syntax: Select fieldname, fieldname, fieldname from tablename; Explanation:

When retrieving multiple columns, specify each field name. A comma must separate field names. The columns will display in the order you select them. Numbers display in scientific notation.

Example: Retrieve the vendor ID, name, and current balance for each record: 1. In the SQL text box, type: Select VendId, Name, CurrBal from TrnVendor; 2. Execute the command by clicking on First, the Run button located to the right of the SQL text box. The results will display on the screen. 3. Use the vertical scroll bar to move up and down the screen. 4. Click on Stop to return to the original screen Results: VendId TV001 TV002 TV003 TV004 TV005 TV006 TV007 TV008 TV009 TV010 TV011 TV012 TV013 TV014 TV015 TV016 TV017 Name Wet Off Towels The Games All Here BedMakers Linen Bed Room Furniture, Inc. Ray Block Mosquito No Bite Spot Out Big Tree Landscaping Hit the Deck The Soda Factory The Freelance Paper People Softer Software Counter Productive No Waste Disposal Music Maker Food Four CurrBal 1.31300E+001 4.53170E+002 5.00000E+002 3.21700E+002 1.00123E+003 9.67240E+002 1.60654E+003 3.42980E+002 2.34500E+001 0.00000E+000 -1.09230E+002 6.17000E+002 9.87367E+003 4.28900E+001 1.72098E+003 4.10914E+003 6.42980E+002

TV018 TV019 TV020 TV021 TV022 TV023 TV024 TV025 TV026 TV027 TV028 TV029 TV030

Computer Bytes Bayshore Consulting BayCon Group Cooperative Operatives Legal Lookup World Wide Learning U Enterprise Transport Against the Tide Clampet Oil Narrow Nest Distant Horizons Paltry Play Make Shift Tilts

3.45090E+002 4.98050E+002 5.10000E+001 2.00000E+001 3.21516E+003 7.71060E+002 1.23498E+003 6.47200E+001 0.00000E+000 1.60900E+003 2.69800E+001 1.00000E+003 1.79323E+003

Formatting Numbers
By default, Scalable SQL displays numbers using scientific notation. To change the format, you must add a mask. Syntax:

Select fieldname<mask>, fieldname<mask>, fieldname<mask> from tablename;

Explanation:

Number masks tell SQL how to display numbers. The symbols in the table below define the format mask. Enclose number masks in brackets.

Number Masks

Display number.

Display number, drop leading zeros.

Display a plus in front of positive numbers. Display a negative in front of negative numbers.

Display a negative in front of negative numbers. Display nothing in front of positive numbers.

()

Display negative numbers enclosed in parentheses.

Display decimal point.

Display dollar sign.

Use comma to separate thousands.

Example:

Retrieve the vendor ID, name, and current balance for all vendors. Format the current balance:

1.

In the SQL text box, type:

Select VendId, Name, CurrBal [($Z,ZZZ,ZZZ.99)] from TrnVendor;

2. 3.

Execute the statement by clicking on First, the Run button located to the right of the SQL text box. The results will display on the screen. Click on Stop to return to the original screen.

Results: VendId Name CurrBal

TV001

Wet Off Towels

$13.13

TV002

The Games All Here

$453.17

TV003

BedMakers Linen

$500.00

TV004

Bed Room Furniture, Inc.

$321.70

TV005

Ray Block

$1,001.23

TV006

Mosquito No Bite

$967.24

TV007

Spot Out

$1,606.54

TV008

Big Tree Landscaping

$342.98

TV009

Hit the Deck

$23.45

TV010

The Soda Factory

$0.00

TV011

The Freelance

($109.23)

TV012

Paper People

$617.00

TV013

Softer Software

$9,873.67

TV014

Counter Productive

$42.89

TV015

No Waste Disposal

$1,720.98

TV016

Music Maker

$4,109.14

TV017

Food Four

$642.98

TV018

Computer Bytes

$345.09

TV019

Bayshore Consulting

$498.05

TV020

BayCon Group

$51.00

TV021

Cooperative Operatives

$20.00

TV022

Legal Lookup

$3,215.16

TV023

World Wide Learning U

$771.06

TV024

Enterprise Transport

$1,234.98

TV025

Against the Tide

$64.72

TV026

Clampett Oil

$0.00

TV027

Narrow Nest

$1,609.00

TV028

Distant Horizons

$26.98

TV029

Paltry Play

$1,000.00

TV030

Make Shift Tilts

$1,793.23

Ordering Rows
Data is stored in Scalable SQL in no particular sequence. If you want to see your data displayed in sequence, you must add an Order By clause to your Select statement. Syntax:

Select */fieldname<mask> ... from tablename order by fieldname;

Explanation:

Example:

The Order By clause tells SQL you want the specified fields displayed in ascending order (ordered from A to Z, 1 to 100).

Retrieve the city, vendor Id, and name from the TrnVendor table. Order your data by city:

1.

In the SQL text box type:

Select City, VendId, Name from TrnVendor order by City;

2.

Execute the SQL statement.

Results: City VendId Name

Bayshore

TV019

Bayshore Consulting

Burlington

TV020

BayCon Group

Chicago

TV003

BedMakers Linen

Chicago

TV016

Music Maker

Chicago

TV028

Distant Horizons

Chicago

TV029

Paltry Play

Chicago

TV030

Make Shift Tilts

Dallas

TV021

Cooperative Operatives

Dallas

TV026

Clampett Oil

Detroit

TV005

Ray Block

Detroit

TV007

Spot Out

Freeport

TV017

Food Four

Freeport

TV023

World Wide Learning U

Freeport

TV024

Enterprise Transport

Houston

TV022

Legal Lookup

Mercer

TV004

Bed Room Furniture, Inc.

Mercer

TV011

The Freelance

Monroe

TV008

Big Tree Landscaping

Monroe

TV013

Softer Software

New York

TV012

Paper People

Orlando

TV010

The Soda Factory

Orlando

TV025

Against the Tide

Park Ridge

TV006

Mosquito No Bite

Park Ridge

TV015

No Waste Disposal

Toledo

TV001

Wet Off Towels

Toledo

TV009

Hit the Deck

Toledo

TV014

Counter Productive

Toledo

TV027

Narrow Nest

Warren

TV002

The Games All Here

Youngstown

TV018

Computer Bytes

Displaying Rows in Descending Order


If you would like to see fields displayed in descending order, follow the field name with "desc" in the Order By clause. Syntax: Select */fieldname ... from tablename order by fieldname <desc> ... Explanation:

By default, the Order By clause tells SQL you want the field displayed in ascending order. Typing "desc" after the field name in the Order By clause tells SQL you want the data in the field displayed in descending order (Z to A, 100 to 1).

Example: Retrieve the city, vendor ID, and name from the TrnVendor table. Order your data by city in descending order: 1. In the SQL text box, type:

Select City, VendId, Name from TrnVendor order by City desc; 2. Results City Youngstown Warren Toledo VendId TV018 TV002 TV027 Name Computer Bytes The Games All Here Narrow Nest Execute the SQL statement.

Toledo Toledo Toledo Park Ridge Park Ridge Orlando Orlando New York Monroe Monroe Mercer Mercer Houston Freeport Freeport Freeport Detroit Detroit Dallas Dallas Chicago Chicago Chicago Chicago Chicago Burlington Bayshore

TV014 TV009 TV001 TV015 TV006 TV025 TV010 TV012 TV013 TV008 TV011 TV004 TV022 TV024 TV023 TV017 TV007 TV005 TV026 TV021 TV030 TV029 TV028 TV016 TV003 TV020 TV019

Counter Productive Hit the Deck Wet Off Towels No Waste Disposal Mosquito No Bite Against the Tide The Soda Factory Paper People Softer Software Big Tree Landscaping The Freelance Bed Room Furniture, Inc. Legal Lookup Enterprise Transport World Wide Learning U Food Four Spot Out Ray Block Clampett Oil Cooperative Operatives Make Shift Tilts Paltry Play Distant Horizons Music Maker BedMakers Linen BayCon Group Bayshore Consulting

Ordering Multiple Columns


When ordering your data, you can have multiple sort levels. For example, you can order your data by city and then by name within the city. Syntax:

Select fieldname, fieldname, fieldname from tablename order by fieldname <desc>, fieldname <desc>, fieldname <desc>

Explanation:


Example:

By default, the Order By clause orders the specified fields in ascending order. Typing "desc" after a field name in the Order By clause tells SQL you want the data in the specified field displayed in descending order (Z to A, 100 to 1). The first field name specified is the primary sort order, the second field name specified is the secondary sort order, and so on ...

Retrieve the city, name, and vendor ID from the TrnVendor table. Order your data by city and then by name within city:

1.

In the SQL text box, type:

Select City, Name, VendId from TrnVendor order by City, Name;

2.

Execute the SQL statement.

Results City Name VendId

Bayshore

Bayshore Consulting

TV019

Burlington

BayCon Group

TV020

Chicago

BedMakers Linen

TV003

Chicago

Distant Horizons

TV028

Chicago

Make Shift Tilts

TV030

Chicago

Music Maker

TV016

Chicago

Paltry Play

TV029

Dallas

Clampett Oil

TV026

Dallas

Cooperative Operatives

TV021

Detroit

Ray Block

TV005

Detroit

Spot Out

TV007

Freeport

Enterprise Transport

TV024

Freeport

Food Four

TV017

Freeport

World Wide Learning U

TV023

Houston

Legal Lookup

TV022

Mercer

Bed Room Furniture, Inc.

TV004

Mercer

The Freelance

TV011

Monroe

Big Tree Landscaping

TV008

Monroe

Softer Software

TV013

New York

Paper People

TV012

Orlando

Against the Tide

TV025

Orlando

The Soda Factory

TV010

Park Ridge

Mosquito No Bite

TV006

Park Ridge

No Waste Disposal

TV015

Toledo

Counter Productive

TV014

Toledo

Hit the Deck

TV009

Toledo

Narrow Nest

TV027

Toledo

Wet Off Towels

TV001

Warren

The Games All Here

TV002

Youngstown

Computer Bytes

TV018

Retrieving Specific Rows


So far, you have been retrieving all of the rows in the table. You can, however, specify which rows you wish to retrieve. For example, you could retrieve only those vendors who are in Chicago. Syntax:

Select */fieldname ... from tablename where fieldname =/!=/<>/>/>=/</<=/in/not in/between/not between/begins with/contains/not contains/ is null/is not null/like/not/like value order by fieldname <desc>...

Explanation:

You can use any of the following logical operators in your Where clause to restrict the rows you retrieve.

Logical Operators

Equal to

!= or <>

Not equal to

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

in

Equal to any item in a list

not in

Not equal to any item in a list

between

Between two values, greater than or equal to one and less than or equal to the other

not between

Not between two values

begins with

Begins with specified value

contains

Contains specified value

not contains

Does not contain specified value

is null

Is blank

is not null

Is not blank

like

Like a specified pattern. % means any series of characters. _ means any single character.

not like

Not like a specified pattern. % means any series of characters. _ means many single character.

In the Where clause, when referring to variables in character fields, you must enclose the values in single quotes.

Example: where City = 'Chicago'

Variables that refer to numeric fields should not be enclosed in quotes.

Example: where CurrBal > 1200

Examples:

Retrieve all vendors located in Chicago.

1.

In the SQL text box, type:

Select City, Name, VendId from TrnVendor where City = 'Chicago';'

2.

Execute the SQL statement.

Results: City Name VendId

Chicago

BedMakers Linen

TV003

Chicago

Music Maker

TV016

Chicago

Distant Horizons

TV028

Chicago

Paltry Play

TV029

Chicago

Make Shift Tilts

TV030

Retrieve all vendors who are not located in Chicago. Order the results by city.

1.

In the SQL text box, type:

Select City, Name, VendId from TrnVendor where City <> 'Chicago' order by City;

2.

Execute the SQL statement.

Results: City Bayshore Burlington Dallas Dallas Detroit Detroit Freeport Freeport Freeport Houston Mercer Mercer Monroe Monroe New York Orlando Orlando Park Ridge Park Ridge Toledo Name Bayshore Consulting BayCon Group Cooperative Operatives Clampett Oil Ray Block Spot Out Food Four World Wide Learning U Enterprise Transport Legal Lookup Bed Room Furniture, Inc. The Freelance Big Tree Landscaping Softer Software Paper People The Soda Factory Against the Tide Mosquito No Bite No Waste Disposal Wet Off Towels VendId TV019 TV020 TV021 TV026 TV005 TV007 TV017 TV023 TV024 TV022 TV004 TV011 TV008 TV013 TV012 TV010 TV025 TV006 TV015 TV001

Toledo Toledo Toledo Warren Youngstown

Hit the Deck Counter Productive Narrow Nest The Games All Here Computer Bytes

TV009 TV014 TV027 TV002 TV018

Retrieve all vendors in Mercer, New York, or Park Ridge.

1.

In the SQL text box, type:

Select City, Name, VendId from TrnVendor where City in ( 'Mercer', 'New York', 'Park Ridge') order by City;

2.

Execute the SQL statement.

Results: City Name VendId

Mercer

Bed Room Furniture, Inc.

TV004

Mercer

The Freelance

TV011

New York

Paper People

TV012

Park Ridge

Mosquito No Bite

TV006

Park Ridge

No Waste Disposal

TV015

Retrieve all vendors whose names begin with "Co."

1.

In the SQL text box, type:

Select Name, City, VendId from TrnVendor where Name begins with 'Co' order by Name;

2.

Execute the SQL statement.

Results: Name City VendId

Computer Bytes

Youngstown

TV018

Cooperative Operatives

Dallas

TV021

Counter Productive

Toledo

TV014

Retrieve all vendors whose city ends with the letters "do."

1.

In the SQL text box, type:

Select Name, City, VendId from TrnVendor where City like '%do' order by City;

2.

Execute the SQL statement.

Results: Name City VendId

The Soda Factory

Orlando

TV010

Against the Tide

Orlando

TV025

Wet Off Towels

Toledo

TV001

Hit the Deck

Toledo

TV009

Counter Productive

Toledo

TV014

Narrow Nest

Toledo

TV027

Retrieve all vendors with a current balance between 500 and 1000 dollars.

1.

In the SQL text box, type:

Select VendId, Name, CurrBal [ZZ,ZZZ.99] from TrnVendor where CurrBal between 500 and 1000 order by CurrBal;

2.

Execute the SQL statement.

Results: VendId Name CurrBal

TV003

BedMakers Linen

500

TV012

Paper People

617

TV017

Food Four

642.98

TV023

World Wide Learning U

771.06

TV006

Mosquito No Bite

967.24

TV029

Paltry Play

1,000

Anda mungkin juga menyukai