Oracle and Microsoft SQL

xiaoxiao2021-03-06  83

There are still many different, the repost is as follows: http://www.bristle.com/tips/sql.htm#oracle TIPS

Table of contents:

Oracle Tips

SQL TIPS

Select * and more Materialized View Pl / SQL TIPS SQL Navigator Tips See Also MS SQL Server TIPS

SQL TIPS

Dynamic SQL in A Stored Procedure SQL Enterprise Manager Tips

Keyboard Shortcuts SQL Generating SQL See Also Differences Between Oracle and MS SQL Server

Concepts and Terminology Data Types Limits Operators Built-In Functions Differences in SQL Syntax Differences in SQL Semantics Differences in Managing Databases Differences in Managing Database Objects Differences in Managing Users Differences in Integration with MS ADO, RDO, etc. Miscellaneous Differences See Also

Details of tips:

Oracle Tips

SQL TIPS THIS Section Contains Tips On Standard SQL (STRUCTURED Query Language) Statements in Oracle.

SELECT * and more Last Updated: 6/6 / 1999Applies to: Oracle 7.3, 8 (and probably earlier versions) To select all columns of a table: select * from table However, to select all real columns, plus a pseudo-column like "User": select table. *, user from table the following does not work: select *, user from table --fred materialized view limited: 1/7 / 2002Applies to: Oracle 8 Oracle 8i Introducesd A New Feature Called A . "materialized view" you define it just like any other view, except that you add the keyword MATERIALIZED: CREATE MATERIALIZED VIEW view_name A materialized view is like a combination of a table and a view like a view, it is defined as a logical. view into the data of one or more tables. When you update the tables, subsequent queries of the view see the updated data. However, like a table, its data is stored in the database. Also, like a table, it is faster if You Define Indexes for It. A Regular View Is Stored As a mapping of data from tables. When you modify t he data in the tables, the view is completely ignored. When you access the view, it joins the data currently in the tables, and returns the data you requested. A materialized view is stored as such a mapping along with a copy of the actual Data from the tables. When You Modify The Data in Tables, The View '

s copy of the data is also updated. When you access the view, the data is drawn directly from the copy. Thus a materialized view makes table updates a little slower, but makes view queries much faster. It also consumes additional space in the database . You could accomplish the same effect by defining an additional table instead of the view, and using triggers on the component tables to update it each time they are changed. However, using a materialized view is more convenient, more efficient, and clearer to the . next person who has to maintain your database Thanks to Andy Glick for sending me a sample of a materialized view from his application --FredPL / SQL Tips This section contains tips on PL / SQL statements -! the Oracle "procedural language" superset of SQL that you use to write stored procedures. SQL Navigator Tips This section contains tips on the SQL Navigator tool by Quest Systems. It is a graphical front end to the Oracle database, allowing you to create, delete, view, and Modify All Oracle Objects: Tables, Views, Stored Procedures, etc. See Also Last Updated: 6/6 / 1999Applies TO: Oracle 7.3 The Following Area Good Sources of Info About Oracle:

Koch, George, and Kevin Loney Oracle 8, The Complete Reference Berkeley CA:.. For Oracle Press by Osborne McGraw-Hill, 1997. ISBN 0-07-882396-X.This book includes introductory database concepts as well as a complete reference To Oracle SQL and PL / SQL Statements. The Companion CD Contains a Complete Copy of the Book, So You Can Read It On-line, Search It, etc. Any of the O'Reilly Books. I've Been Very Implessed by All All All All All All All ALL O'Reilly Books Since My Early Unix and X-Windows Days in The 80's, and They Have A Complete Series On Oracle, Covering Pl / SQL, The Standard Packages, etc. --fredms SQL Server Tips

SQL TIPS THIS Section Contains Tips on SQL (Structured Query Language) Statements In MS SQL Server.

Dynamic SQL in a Stored Procedure Last Updated: 2/7 / 1999Applies to: MS SQL Server 6.5 A typical tradeoff for a database application is dynamic SQL (SQL commands embedded in the application - for flexibility) vs. stored procedures (pre- compiled SQL procedures stored in the database and invoked by name from the application -. for speed and control over what SQL statements get executed) However, you can have the best of both worlds by using dynamic SQL inside your stored procedures in a stored. procedure, you can use the EXEC statement to execute a string of SQL statements that you built dynamically in the stored procedure or read from the database or any other data source. Thanks to Steve Rhoads for this tip. --Fred

. SQL Enterprise Manager Tips This section contains tips on the SQL Enterprise Manager tool It is a graphical front end to the database, allowing you to create, delete, view, and modify all MS SQL Server objects: tables, views, stored procedures, etc .

Keyboard Shortcuts Last Updated: 6/20 / 1999Applies to: MS SQL Server 7.0 Here is a list of some of the more useful shortcut keys in SQL Enterprise Manager.KeyFunctionF1Help on SQL Enterprise ManagerShift-F1Help on syntax of current SQL statementCtrl-EExecute selected text .. in Query AnalyzerCtrl-RHide / show results pane in Query Analyzer Obviously, this list is far from complete Please feel free to mail me your favorite shortcuts I'll add to this list as time permits See also:. Windows Shortcut Keys - fred SQL Generating SQL Last Updated: 2/7 / 1999Applies to: MS SQL Server 6.5 to automate tedious database maintenance chores, you can use SQL statements to generate SQL statements that do your maintenance for you for example, to change the permissions on. All Stored Procedures in A Database, You CAN Use A Select Statement Like: SELECT 'GRANT EXECUTE ON' NAME 'TO PUBLIC

Go '

From sysobjects

WHERE type = 'P' The output of this SELECT statement is a series of alternating GRANT and GO statements, one pair per stored procedures, for all stored procedures in the database. Then you copy that output as your next set of commands and execute it Note: Be Sure To Leave The Line Break Before The Word Go. It is Required To Start On A New Line, After The Grant Statement. Thanks to Steve Rhoads for this Tip. --Fred

See Also Last Updated: 6/6 / 1999Applies TO: MS SQL Server 6.5 The Following Area Good Sources of Info About MS SQL Server:

MS SQL Server Books on The MSDN Library CD. --Fred

Differences Between Oracle and MS SQL Server

Concepts and Terminology Last Updated: 4/24 / 2001Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows some differences in concepts and terminology between Oracle and MS SQL Server: Concept / TermOracleMS SQL ServerDatabase enginedatabasedatabase serverDatabase (collection of tables ) schemadatabaseRoles / GroupsrolesgroupsDatabase adminstrator account, database ownerdbasa, dboData about the databaseData dictionary- one per serverDatabase catalog- one per database "master" database- one per serverBlocks and extentsblocks and extentspages and extentsNetwork softwareSQL * NetNet-libraryData stream protocolTransparent Network Substrate (TNS) Tabular Data Stream (TDS) Case sensitivity of names of tables, columns, etc.case-insensitivedepends on character sort order, default is case-insensitiveSynonymssupportednot supportedReadonly transactionsupportednot supported --Fred Data Types Last Updated: 6/6 / 1999Applies to: Oracle 7.3 , MS SQL Server 6.5 The Following Table Shows The Corresp Onding Data Types in Oracle and MS SQL Server:

Data TypeOraclems SQL Serverfixed Length StringChar (N) - Limit 2kbchar (N), Character (n) - Limit 255 (6.5) - Limit 8kb (7.0) Variable Length Stringvarchar2 (N), VARCHAR (N) - LIMIT 4K I Column- Limit 32kb in a variable- varchar is obsoletevarchar (n), char varying (n), character varying (n) - limited 255 (6.5) - limited 8KB (7.0) Integerinteger, Integer (N), Smallintinteger (4 Bytes), int INT (4 Bytes), Smallint (2 Bytes), Tinyint (1 Byte), Bit (1 bit) Fixed PointNumber, Number (N), N, D), Float, Float (n), Float (n, d) Numeric, Numeric (N), Numeric (N, D), Decimal, Decimal (N), Decimal (N, D), DEC, DEC (N), DEC (N, D), Money, Smallmoneyfloating PointDecimalfloat, Float (N ), Double PRECISIME, SMALLDATETIME, TIMESTAMP- TIMESTAMP Auto-UpdatedBinaryRaw (N) - LIMIT 255 BYtesbinary (N), Varbinary (N), Binary Varying (N) - LIMIT 255 (6.5) - LIMIT 8KB (7.0) Large stringlong, long varcha - limited 2GB- LIMIT One PER TABLE ROWCLOB - LIMIT 4GBTEXT- LIMIT 2GBLARGE BINARYLONG RAW - LIMIT 2GB- LIMIT One Per Table Rowblob Imit 4gbimage- limited 2gbmulti-byte charsnchar (n) nvarchar (n) nclob- Same Limits As Char, Varchar, Clobnchar (N), National Char (N), National Character (n) NVARchar (n), National Char Varying (N) NATIONAL CHAR VARYING (N ), NATIONAL CHARACTER VARYING (n) NTEXT, NATIONAL TEXT- same limits as CHAR, VARCHAR, TEXTOS FileBFILE Row Identifierimplicit ROWID column (use an IDENTITY column) Secure OS LabelMLSLABEL, RAW MLSLABEL 128-bit Unique Number (UUID, GUID) UNIQUEIDENTIFIER (version 7.0 only) --Fred Limits Last Updated: 6/14 / 2000Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows differences in limits of Oracle and MS SQL Server:

DescriptionOracleMS SQL ServerColumns per table1000250 (6.5) 1024 (7.0) Row size unlimited1962 bytes (6.5) 8060 bytes (7.0) - includes pointers, but not data, for TEXT and IMAGE columnsLONG and LONG RAW columns per row1 (must be last column) unlimited (16-byte pointer per) LOB, TEXT, and IMAGE columns per rowunlimited (16-byte pointer per) unlimited (16-byte pointer per) clustered indexes per table11Non-clustered indexes per tableunlimited249Columns per index1616Index row size2K bytes900 bytesIdentifier Length30 chars30 chars ( 6.5) 128 chars (7.0) Tables per SELECTunlimited16 (6.5) 256 (7.0) Source code per stored procedure 64KB (6.5) 250MB (7.0) Data type limits (see Data Types) --Fred Operators Last Updated: 6/7 / 1999Applies to: Oracle 7.3 , MS SQL Server 6.5 Most operators are the same in Oracle and MS SQL Server Here are some that differ:. DescriptionOracleMS SQL serverString concatenationstring1 || string2string1 string2 --Fred Built-in Functions Last Updated: 6 / 7 / 1999Applies TO: Oracle 7. 3 , MS SQL Server 6.5 Oracle and MS SQL Server Offer Many of The Same Built-in Functions. For Example, They Both Offer ABS, EXP, ROUND, Upper, Lower, AVG, Count, Sum, ASCII, ETC. The FOLLOWING TABLE SHOWS That Don't Have The Same Name. for a more completion list, see "migrating oracle application to sql server"

DescriptionOracleMS SQL ServerSmallest integer> = nCEILCEILINGModulusMOD% Truncate numberTRUNC Max or min number or string in listGREATEST, LEAST Translate NULL to nNVLISNULLReturn NULL if two values ​​are equalDECODENULLIFString concatenationCONCAT (str1, str2) str1 str2Convert ASCII to charCHRCHARCapitalize first letters of wordsINITCAP Find string in stringINSTRCHARINDEXFind pattern in stringINSTRPATINDEXString lengthLENGTHDATALENGTHPad string with blanksLPAD, RPAD Trim leading or trailing chars other than blanksLTRIM (str, chars), RTRIM (str, chars) Replace chars in stringREPLACESTUFFConvert number to stringTO_CHARSTR, CASTConvert string to numberTO_NUMBERCASTGet substring from stringSUBSTRSUBSTRINGChar for char translation in stringTRANSLATE Date additionADD_MONTH or DATEADDDate subtractionMONTHS_BETWEEN or -DATEDIFFLast day of monthLAST_DAY Time zone conversionNEW_TIME Next specified weekday after dateNEXT_DAY Convert date to stringTO_CHARDATENA ME, CONVERTConvert string to dateTO_DATECASTConvert date to numberTO_NUMBER (TO_CHAR (d)) DATEPARTDate roundROUNDCONVERTDate truncateTRUNCCONVERTCurrent dateSYSDATEGETDATEConvert hex to binaryHEXTORAWCASTConvert binary to hexRAWTOHEXCONVERTIf statement in an expressionDECODECASE ... WHENor COALESCEUser's login id number or nameUID, USERSUSER_ID, SUSER_NAMEUser's database id number or nameUID, USERUSER_ID , USER_NAMECurrent userUSERUSER --Fred Differences in SQL syntax Last Updated: 3/21 / 2001Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows the different syntax used in Oracle and MS SQL Server for the same SQL operations:

DescriptionOracleMS SQL ServerLeft Outer JoinWHERE column1 = column2 ( ) FROM table1 LEFT OUTER JOIN table2 ON table1.column1 = table2.column2Note: The following syntax is also supported, but is no longer recommended: WHERE column1 * = column2Right Outer JoinWHERE column1 ( ) = column2FROM table1 RIGHT OUTER JOIN table2 ON table1.column1 = table2.column2Note: The following syntax is also supported, but is no longer recommended: WHERE column1 = * column2Full Outer Join FROM table1 FULL OUTER JOIN table2 ON table1.column1 = table2.column2SELECT without FROMSELECT 'hello world' FROM DUALSELECT 'hello world'SELECT data into a tableCREATE TABLE AS SELECT ... SELECT ... INTOIntersection of 2 SELECTSSELECT ... INTERSECT SELECT ... SELECT ... WHERE EXISTS (SELECT ... Subtraction of 2 SelectSselect ... minus select ... Select ... Where not exists (select ...) Insert Into a Joinsert INTO SELECT ... CREATE A View and INSERT INTO IT.UPDATE DATA IN A JOINUPDATE SELECT. ..Create a view and insert IT.UPD ATE one table based on criteria in another table UPDATE table FROM ... DELETE rows from one table based on criteria in another table DELETE FROM table FROM ... DROP a column from a table ALTER TABLE table_name DROP COLUMN column_nameReadonly VIEWCREATE VIEW ... WITH READONLYGRANT SELECT ... Save pointSAVEPOINTSAVE TRANSACTIONTable lockLOCK TABLE ... IN SHARE MODE SELECT ... table_name (TABLOCK) Exclusive table lockLOCK TABLE ... IN EXCLUSIVE MODE Select ... Table_name (Tablockx) Reserving Index Space PctFree = 0FillFactor = 100Declaring a local variabledeclare varname type; declare @

varname typeInitializing a local variableDECLARE varname type: = value; Declaring a constantDECLARE varname CONSTANT type: = value; Assigning to a variablevarname: = valueSELECT value INTO varnameSET @varname = valueSELECT @varname = valueAssigning to a variable from a cursorFETCH cursorname INTO varnameFETCH NEXT fROM cursorname INTO varnameDeclaring a cursorCURSOR curname (params) IS SELECT ...; DECLARE curname CURSOR FOR SELECT ... If statementIF ... THENELSIF ... THENELSEENDIFIF ... BEGIN ... ENDELSE BEGIN ... eNDWhile loopWHILE ... lOOPEND LOOPWHILE ... BEGIN ... ENDOther loopsFOR ... END LOOPLOOP ... END LOOP Loop exitEXIT, EXIT WHENBREAK, CONTINUEPrint outputDBMS_OUTPUT.PUT_LINEPRINTRaise errorRAISE_APPLICATION_ERRORRAISERRORStatement terminatorSemi-colon (;) Thanks to Tom Johnston for catching a mistake in this Tip. I had the from dual in the Wrong Column. --fred Difference In Sql Semantics Last Updated: 6/6 / 1999Applies TO: Oracle 7.3 , MS SQL Server 6.5 The Following Table Shows Some Semantic Differences Between Oracle and MS SQL Server:

DescriptionOracleMS SQL ServerCommitExplicit COMMIT statement requiredAutomatic commit unless SET IMPLICIT_TRANSACTIONS ONReading uncommitted dataDatabase does temporary internal rollback to reconstruct most recently committed data for reader.Depending on options, reader as allowed to read uncommitted data, or is forced to wait for writer to commit or rollback. Releasing cursor dataCLOSE CURSOR releases all data. you can not re-open.CLOSE CURSOR does not release data. you must explicitly call DEALLOCATE CURSOR. Until then, you can re-open the cursor.Implicit data conversion in a statement like the following where vc is a column of type VARCHAR2: SELECT * fROM person WHERE vc = 123As each row is fetched from the table, an attempt is made to convert it to a number for the comparison with 123. If any row contains a value that can not be Converted to a Number, a Runtime Error Occurs.The Number 123 Is Converted To The String '123' Once, and THEN THE DATA IS FETCHED from The Table. if Any R ow contains a value that can not be converted to a number, it simply does not match '123' and is skipped without any error.Conversion to NULLSetting a VARCHAR2 column to '' (the empty string) makes it NULL.Setting a VARCHAR column to '' makes it the empty string (not NULL) --Fred Differences in Managing Databases Last Updated:. 6/6 / 1999Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows some differences in how databases are managed In Oracle and MS SQL Server:

DescriptionOracleMS SQL ServerModel databaseNo model databaseNewly created databases inherit characteristics (users, etc.) from the special database named "model" --Fred Differences in Managing Database Objects Last Updated:. 6/6 / 1999Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows some differences in how database objects (tables, views, stored procedures, etc.) are managed in Oracle and MS SQL Server: DescriptionOracleMS SQL ServerFully qualified name [schema.] table [schema.] view [[[ Server.] [database].] Table [[[server.] [Database].] [owner]. Any table named starting with a pound sign (#) is automatically deleted when the user logs off or the procedure ends ## table -. Same as above, except that the table is accessible to other users.Re-creating an objectCREATE oR REPLACE ... Drop ... Create ... Create View Before dependent Tables Create Force ViewNot Su . Pported Tables used by view must exist before view can be created --Fred Differences in Managing Users Last Updated:. 6/6 / 1999Applies to: Oracle 7.3 , MS SQL Server 6.5 The following table shows some differences in how users are Managed in Oracle and MS SQL Server:

DescriptionOracleMS SQL ServerMembership in groupsEach user can be a member of any number of groups.Each user can be a member of only one group other than "public" --Fred Differences in Integration with MS ADO, RDO, etc. Last Updated:. 6 / 6 / 1995Applies TO: Oracle 7.3 , MS SQL Server 6.5 The Following Table Shows The Different Techniqu Used in Oracle and MS SQL Server To Interact with MS ADO, RDO, ETC .:

DescriptionOracleMS SQL ServerReturn a recordset to the callerReturn a handle to a cursor.For more info: See MS KB article Q174679.SELECT with no INTO clause; Multiple such SELECTs return multiple recordsets --Fred Miscellaneous Differences Last Updated: 6/6 / 1999Applies to : Oracle 7.3 , MS SQL Server 6.5 The following table shows miscellaneous differences between Oracle and MS SQL Server: DescriptionOracleMS SQL ServerGenerate unique numbersCREATE SEQUENCEIDENTITY column of a tableCascaded DELETEDELETE CASCADE ... (use triggers) Call a user-defined function from a SQL statement (as column of SELECT or expression in WHERE clause) supportednot supported --Fred See Also Last Updated: 3/3 / 2001Applies to: Oracle 7.3 , MS SQL Server 6.5 The following are good sources of info about differences between Oracle And MS SQL Server:

Bowman, Judith S., Sandra L. Emerson, and Marcy Darnovsky. The Practical SQL Handbook. Addison-Wesley Publishing Company, 1993. ISBN 0-201-62623-3.This book gives a good introduction to SQL, with a slight emphasis on Sybase, but with a useful summary in the back of the syntax for each of the SQL statements (SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT, REVOKE, etc.) for each of the major databases (Oracle, Sybase , DB2, Informix, Ingres, etc.) The book pre-dates MS SQL Server, but the Sybase info is a good approximation since MS SQL Server is a derivative of Sybase. "Migrating Oracle Applications to SQL Server" on MSDN CD, and at MS TechNet Web site: http: //www.microsoft.com/TechNet/sql/Tools/Sqldevkt/ORCL2SQL.aspMicrosoft clearly intended this to be used in one direction only, but I've used it quite successfully to translate my SQL Server Knowledge to Oracle As Well.

转载请注明原文地址:https://www.9cbs.com/read-121944.html

New Post(0)