|
| Topics this page:
|
Related Topics:
|
| ||||||||||||||||||||||||||||||||||||||||||||
| Factoid | Numbers to Remember | ||
|---|---|---|---|
| MS-SQL7 | Oracle 8i | mySQL | |
| Max. size of SQL Procedure | - | 64KB | |
| Tables per database | 2 billion | ||
| Columns per table | 1,024 | ||
| Max characters in Column Name | ? | 30 | |
| max # of columns in composite index | 16 | ||
| max nonclustered indexes per table | 249 | ||
| max key bytes | 900 | ||
| page extent size | 64 KB | ||
| page size | 8 KB (8,192) | ||
| max bytes per row | 8,060 | ||
| max char bytes | 8,000 | 255 | |
| max int value | 2.147 billion (2^31) | ||
| max smallint value | 32,767 | ||
| max smallmoney value | $214,748 | ||
| money decimal precision | 4 (.4444) | ||
To invoke MS-SQL automatically, the SQL service Manager is placed in the Startup folder with a Target of:
SQL is pronounced either as a sequence of letters "Es-Queue-Ell" or as acronym "Sequel".
Yahoo Computers and Internet > Programming and Development > Languages > SQL
MySQL AB was created by Monty Widenius.
MySQL Enterprise Solutions
(Indianapolis, Ind. : John Wiley & Sons, Inc. (US), 2003)
by Pachev, Alexander "Sasha"
SQLCMD from Microsoft enables SQL to be applied across several SQL servers.
|
| Action | Oracle SQL | Microsoft SQL |
|---|---|---|
Tool for interactive input, store, and running of stored procedures | C:>SQLPLUSW userid/password@what.world | Run
SQLW
Run isqlw for SQL Query Analyzer |
To control output | set serveroutput on set serverout off | - |
Send output to a file | spool myoutput.out | - |
Stop sending output | spool off | - |
Send output file to printer (for lpr) | spool out | - |
To invoke SQL statements stored in file named my.sql | start my.sql | - |
Return to the OS | exit | - |
Language Name | PL/SQL - Procedural Language | Transact-SQL |
Read product documentation | - | For Books online: Within SQLW, Help toolbar -> Building SQL Server Applications -> Transact-SQL Reference -> System Stored Procedures (T-SQL) |
View properties about an object | select * from all_views |
sp_help 'object_name'
or sp_helptext 'object_name' for unencrypted comments in the syscomments system catalog table. |
Text sourceof all stored objects belonging to the user | user_source | - |
Tool to trap activitybetween client app and SQL to a flat file | - | SQL Server Profiler |
Comment operator | -- double hyphen or between /* and */ | same |
To execute a procedure in the buffer |
start or @ (ampersand) | ? |
To execute SQL script in the buffer |
. dot run; or / (slash) |
GO or F5 key or Ctrl-E |
To concatenate and print | DBMS_OUTPUT.PUT_LINE ( 'hello' || v_name ); | - |
Ending a block | period ends each PL/SQL block | ; semicolon ends each SQL block. |
Every time an Oracle user invokes SQL*Plus, two scripts are also automatically executed:
|
|
|
| Oracle SQL | Microsoft SQL |
|---|---|
|
NVL( column_name, value_if_null )
NVL( num_field, 0 ) NVL( txt_field, "NULL" ) |
ISNULL( column_name, value_if_null )
ISNULL( price, $0.00 ) ISNULL( SSN, 'NNN-NN-NNNN' ) |
To convert a small number of fixed values in Oracle:
To calculate numbers or display text not from any table, use the special dummy table owned by Oracle user sys:
|
|
|
| Action | Oracle SQL | Microsoft SQL |
|---|---|---|
Datatypes and Value Assignment |
skalar char, number, long, date, and varchar2
as in
price := qty*(cost*2.5); plus v_valid_order boolean not null := true; and tables | - |
Set date format just for current session | alter session set nls_date_format = 'YYYY-MON-DD-HH24:MI:ss' | SET DATEFORMAT |
Create Custom Datatype | - | EXEC sp_addtype @typename=typeSSN, @phystype='CHAR(11)', @nulltype='NOT NULL' |
Create a Table with a Check Constraintusing a custom data type typeSSN | - |
CREATE TABLE SSNTable ( EmployeeID INT PRIMARY KEY, EmployeeSSN typeSSN,
( EmployeeSSN LIKE '[0-9][0-9][0-9]—[0-9][0-9]—[0-9][0-9][0-9][0-9]' ) |
|
| Type of Constraint | Oracle | MS-SQL |
|---|---|---|
| list constraints | select constraint_name from user_constraints; | ? |
Domain integrityby limiting acceptable values for a column entry. |
CHECK | |
PRIMARY KEYA column (or a composite index of up to 16 columns) used to uniquely identify a row in a table. A table can only have one. |
CONSTRAINT tab1_pk PRIMARY KEY (col1, col2) If this is not specified, the MS-SQL default is to create a clustered index. Tables created without a clustered index are called heaps. |
|
UNIQUE Entity integrityA non-primary key column is unique among all rows. |
By default, a nonclustered index is created for each such column. Each combination of columns that could uniquely identify rows in a table is called a candidate key. | |
NOT NULL |
Special processing for null values degrade performance. | |
FOREIGN KEYA foreign key in one table points to a primary key in another table. |
ALTER TABLE table1 col_a INT NULL CONSTRAINT tab1_fk1 REFERENCE table2(tab1_col_pk) |
|
IDENTITY property(starting IdentitySeed, IdentityIncrement) |
ALTER TABLE table1 ADD Identity_column INT IDENTITY(1,1) GO Whether a table has an identity column can be determined using the OBJECTPROPERTY function. This can be selected using the IDENTITYCOL keyword. |
|
|
|
| Action | Oracle SQL | Microsoft SQL |
|---|---|---|
Create Index | - |
Syntax: CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX index_name ON table (column [,...n]) [WITH [PAD_INDEX] [[,] FILLFACTOR = fillfactor] [[,] IGNORE_DUP_KEY] [[,] DROP_EXISTING] [[,] STATISTICS_NONRECOMPUTE] [ON filegroup] |
|
|
|
| Action | Oracle | MS-SQL |
|---|---|---|
| Create statistics on every column supporting statistics in the current database | sp_createstats | |
| Create statistics data from the table. | CREATE STATISTICS | |
| When were statistics last updated? | STATS_DATE | |
| Determines whether a particular statistic will be automatically updated. | sp_createstats | |
| The density of an existing statistic set. | DBCC_SHOW_STATISTICS | |
| Update statistics data from the table. | UPDATE STATISTICS | |
| Remove statistics data from the table. | DROP STATISTICS |
|
| Statistic | Description |
|---|---|
Pages Scanned |
Number of pages in the table or index found during scanning. |
Extents Scanned |
Number of physical extents among pages scanned. |
Extent Switches |
Number of times the DBCC statement left an extent while it traversed the pages of the extent. |
Avg. Pages per Extent |
Number of pages per extent in the page chain. |
Scan Density |
100 percent if everything is contiguous. The smaller this value, the more fragmentation exists. The best count is the ideal number of extent changes that would be necessary if everything were contiguously linked. The actual count is the actual number of extent changes. |
Logical Scan Fragmentation |
The percentage of out-of-order pages returned from scanning the leaf pages of an index. |
Extent Scan Fragmentation |
Percentage from a count of out-of-order pages divided by the count of all leaf pages of an index scanned. |
Avg. Bytes free per page |
The absolute number of free bytes averaged over all pages scanned. Lower values are better because they make fuller use of each page. Higher row sizes make this number worse. |
Avg. Page density (full) |
Percentage of each page's bytes of useful data divided by the total bytes of all pages scanned. Higher values are better because they make fuller use of each page. make fuller use of each page. er worse. |
|
|
|
| Action | Oracle SQL | Microsoft SQL |
|---|---|---|
List Views defined | select view_name from user_views; | - |
Display read I/O | - |
SET STATISTICS_IO ON |
Display Clustered indexes usedin optimized query Execution Plans (algorithms) | - |
SET SHOWPLAN_TEXT ON or SET SHOWPLAN_ALL ON for resource costing estimates or graphical SHOWPLAN in SQL Server Query Analyzer |
Update Statistics Used for Auto Indexing | - |
UPDATE STATISTICS or UPDATE STATISTICS NORECOMPUTE to stop updating |
Display database linkages and sizes | - | DBCC CHECKTABLE |
Get
| - |
SELECT OBJECT_ID('PUBS..TITLES') GO DBCC SHOWCONTIG(123456789) number from Data Base Consistency Checker response DBCC CHECKTABLE displays linkages and sizes |
Create View | ? | CREATE VIEW testview as SELECT firstname, lastname FROM employees |
|
| Information Stored | Oracle Data Dictionary View Name | Microsoft MS-SQL |
|---|---|---|
| A list of current errors on all objects accessible to the user | all_errors | . |
| Text source of all stored objects accessible to the user | all_source | . |
| Current errors on all stored objects in the database | dba_errors | . |
| All PL/SQL objects in the database | dba_object_size | . |
| Text source of all stored objects in the database | dba_source | . |
| Current errors on all a user's stored objects | user_errors | . |
| Text source of all stored objects belonging to the user | user_source | . |
| User's PL/SQL objects | user_object_size | . |
|
| Action | Oracle SQL | Microsoft SQL |
|---|---|---|
Control structures |
PL/SQL extensions to SQL:
conditional if-then-else
iterative for-loop sequential while-loop & end loop; exit-when unconditional flow-control goto | - |
Labels to go to | << SampleLabel >>. | - |
Sequential processing using a cursor |
declare cursor each_emp is select ename, sal from emp where deptno = 30; emp_name varchar2(20); emp_sal number(7,2); begin open each_emp; loop fetch each_emp into emp_name, emp_sal; if each_emp%NOTFOUND then exit; end if; emp_sal := emp_sal + (emp_sal * .10); end loop; close each_emp; end; |
use payroll declare @ename as varchar(20) declare @emp_sal as decimal(7,2) DECLARE @each_emp CURSOR FOR SELECT ename, pay_rate FROM emp inner join pay on emp.emp_id = pay.emp_id where deptno = 30 OPEN each_emp FETCH NEXT FROM emp_cursor into @emp_name, @emp_sal WHILE @@FETCH_STATUS = 0 BEGIN
FETCH NEXT FROM each_emp into @emp_name, @emp_sal DEALLOCATE each_emp END |
Related functions and cursors stored together | Packages | - |
Table support |
declare type emp_table_type is table of varchar2(35) index by binary_integer; emp_table emp_table_type; i binary_integer := 0; cursor each_emp is select ename from emp; begin open each_emp; loop i := i + 1; fetch each_emp into emp_table(i); if each_emp%NOTFOUND then exit; end if; end loop; statement(s); -- work with emp_table(1), emp_table(2), etc. close each_emp; end; |
EXEC CREATE TABLE emp_table (column1 INT NOT NULL, column2 CHAR(10) NOT NULL)
GO
EXEC sp_help emp_table
SELECT emp_table FROM INFORMATION_SCHEMA.TABLES
|
Debugging |
Public package dbms_output creates a buffer to accept
dbms_output.put or put_line commands.
set serverout displays the buffer. |
exec sp_addmessage 50500, 14, 'msg'
creates error code 50500 with severity 14 at state 1.
raiseerror (50500,14,1)
|
|
| Item/Object | add/drop/help Stored Procedure EXEC ... | To get a list: SELECT createdate, updatedate, accdate, ... FROM server.database.owner.object | Executive GUI |
|---|---|---|---|
| NT Network Accounts | Domain SAM | ||
| SQL Login | dbname, loginname, isntgroup FROM master..syslogins (view of master table sysxlogins) | ||
| Databases | sp_helpdb database_name | name, filename, crdate FROM master..sysdatabases | |
| Columns | TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS | ||
| Privileges | * FROM INFORMATION_SCHEMA.TABLES_PRIVELEGES | ||
| Database User Accounts | sp_addlogin login, name_in_db | uid, name from northwind..sysusers where islogin = 1 | |
| sp_grantdbaccess login, name_in_db | |||
| sp_grantlogin login, name_in_db
or GRANT CREATE VIEW, CREATE PROCEDURE, CREATE TABLE TO cathy | |||
| sp_revokelogin, sp_denylogin login, name_in_db
or REVOKE CREATE VIEW, CREATE PROCEDURE TO cathy | |||
| sp_denylogin login, name_in_db
or DENY CREATE VIEW, CREATE PROCEDURE TO cathy | |||
| Permissions for Uid's | uid, action, protecttype from northwind..sysprotects | ||
| Fixed Server Role | sp_addsrvrolemember @loginame, @rolename | name from master..syslogins system table | for Logins |
| Fixed Database Role | sp_addrolemember role, security_account | name from northwind..sysusers where issqlrole = 1 | |
| Database Standard User Role | sp_addrole @rolename, @ownername | name from northwind..sysusers where issqluser = 1 | |
| Application Role | sp_addapprole @rolename, @password GO sp_setapprole @rolename, @password | name from northwind..sysusers where isapprole = 1 | |
| Primary Data Files | CREATE DATABASE library ON PRIMARY ( name=library_data, FILEname='c:\mssql7\data\library.mdf', size=10MB, MAXsize=15MB, FILEGROWTH=20%)
DROP DATABASE library | ...\*.mdf containing 8KB data pages (128 pages per MB) | |
| Secondary Data Files | ...\*.ndf containing 8KB data pages (128 pages per MB) | ||
| Log Files | ... LOG ON ( name=library_log, FILEname='c:\mssql7\data\library.ldf', size=3MB, MAXsize=5MB, FILEGROWTH=1MB) | C:\Mssql7\Log\*.ldf (sized 25% of data file by default) | |
| File Groups | ALTER DATABASE northwind ADD FILEGROUP orderhistorygroup GO |
|
|
|
| Action | Oracle | MS-SQL |
|---|---|---|
Get a list of triggers |
. | SELECT trigger_name 'INSERT'=object_name(instrig), 'UPDATE'=object_name(updtrig), 'DELETE'=object_name(deltrig), FROM sysobjects WHERE type = 'U' AND (instrig <> 0 OR updtrig <> 0 OR deltrig <> 0) |
Create a new trigger |
. | CREATE OR REPLACE TRIGGER trigger_name after logon -- after logoff -- after servererror -- after startup -- after shutdown -- after create -- after drop -- after alter ON table FOR ( [DELETE] [,] [INSERT] [,] [UPDATE] } [WITH ENCRYPTION] AS sql_statement |
Drop a trigger |
. | DROP TRIGGER trigger_name |
| Step | Action |
|---|---|
| 1 | Execute before statement triggers. |
| 2 | Loop for each row affected by the SQL statement. a. Execute before row triggers. b. Lock and change rows, check for integrity constraints. |
| 3 | Complete deferred integrity constraint checking. |
| 4 | Execute after statement triggers. |
|
|
| Aggregate functions in Jet databases |
|
|
|
|
|
|
|
|
|
|
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page Thank you! | |||