Sqlplus Cheat Sheet

  



MongoDb Cheat Sheets. GitHub Gist: instantly share code, notes, and snippets. These notes are intended to provide a simplified crib sheet (or reminder) on SQL. It is not a tutorial. A number of examples for common types of tasks are provided - but little or no explanation. SQL - Structured Query Language - is a language understood by most database systems.

Basics

The different cheat sheet for SQL Expressions is Numeric, Boolean and Date. The different SQL Constraints are the rules to execute the commands on the table columns to ensure reliability, redundancy, and accuracy while performing operations on a table. Oracle Cheat Sqlplus Commands d2nvmy6529nk. Oracle Cheat Sqlplus Commands d2nvmy6529nk. Home (current) Explore Explore All. Bloomberg Commands Cheat Sheet November 2019 112. More Documents from 'Dong Song' Oracle Cheat Sqlplus Commands December 2019 35. Mary Balogh Intre Decenta Si Dorinta. The PL/SQL cheat sheet includes symbol syntax and methods to help you using PL/SQL.ORACLE PL/SQL is an extension of SQL language that combines the data manipulation power of SQL with the processing power of procedural language to create super-powerful SQL queries.

Related Unix commands

Output formatting

See the last command you typed in the command buffer

Execute the command buffer

Edit a command you just typed in the command buffer

Execute a command (must end in semicolon)

Require screen to stop after each page of output

Set the page size to

Run system commands

Redirect screen output to a file (including everything you type)

Show schemas/tables/tablespaces

Look up SQL based on sql_id

Setting schema type

Dropping a table/tablespace

User Account Management

Look up user account status

Reset user password with hashed value

Disable password expiration (only applies when new password is created or modified)

Unlock user account

Change user password

Query Optimization

Index

Statistics

Check the time table statistics were last updated

Check the time index statistics were last updated

By default, AutoTask is scheduled to run every day, which includes automatic optimizer statistics collection. If automatic optimizer statistics collection is disabled, you can enable it with the ENABLE procedure in DBMS_AUTO_TASK_ADMIN package.

Session

Identify blocking statements

SQL How To ...

These notes are intended to provide a simplified crib sheet (or reminder) on SQL. It is not a tutorial. A number of examples for common types of tasks are provided - but little or no explanation.

SQL - Structured Query Language - is a language understood by most database systems. Except where noted it is believed these SQL statements will work with Microsoft SQL Server, Oracle and MySQL.

Index

Select

Return all records all columns in a table:

Return all records but only field1 and field2 in a table:

Return field1 for all records in a table with a specific value for field2:

Return all records in a table where field1 is one of three possible values:

Return the number of records in a table:

Return the number of records in a table with a specific value for field2:

Simple join:

or

select table1.field1, table2.fieldA from table1, table2
where table1.field2=table2.fieldB

or

select table1.field1, table2.fieldA
from table1 inner join table2 on table1.field2 = table2.fieldB

Select all unique values in field1 from a table (not supported in MSAccess):

or (works in MSAccess):

To get a count of the number of unique values in a field (not supported in MSAccess):

selet count(distinct field1) from TableName

For MSAccess use:

Select all unique values for field1 from a table together with the number of records with that unique value:

Select all unique values for combinations of field1 and field2 from a table together with the number of records with that combination:

Select the number of unique values:

Select all duplicate records in a table, where two (or more) records are considered duplicates if they share a common value for a single field:

Select all duplicate records in a table, where two (or more) records are considered duplicates if they share common values for a pair of fields:

Select similar records, i.e. all records which have duplicate field1 and field2 in a table but with different field3 (i.e. specifying which fields must be the same and which different):

Note:

  • It is important to specify at least one field which is different between the two records otherwise this query will list a record as being the same as itself.
  • This query will not find duplicate records, i.e. records with every field the same.

Select all records from a table which do not share a common ID with records from a second table:

Note:

  • Sub-queries are quite slow.
  • Sub-queries are not supported in versions of MySQL prior to MySQL 5, so the above will not work on older versions of MySQL. My thanks to Kevin Bowman for pointing out that MySQL 5 supports sub-queries.

An alternative using a join (which can be much faster):

Cheat

select table1.* from table1
left join table2 on table1.field1 = table2.field2
where table2.field2 is null;

The following method (which has been suggested by Michael Miller) is to use EXISTS. It is much faster on SQL Server than the above (but Michael says it is comparable with the left join technique on Oracle):

select * from table1
where not exists (select field2 from table2 where table2.field2 = table1.field1)

To perform a two way join:

select * from
table1 left join table2 on table1.field1 = table2.field1,
table1 left join table3 on table1.field2 = table3.field3

this has been tested on SQL Server, but not on Oracle or MySql. It does not work with MS-Access.

Oracle Sqlplus Cheat Sheet Pdf

To combine the results of two queries (be aware that the number and types of fields in both queries must agree):

To return a value based on the contents of a field. This can be done using either Iif, Decode or Case, depending on the database.

The following works with MSAccess:

This is equivalent to the following on SqlServer:

For Oracle use the DECODE function.

To create a new table to hold the results of the select query:

Be aware that this will fail if table2 exists, and that the new table will be created without any indexes. This isn't supported on MySQL.

Insert

Insert new record into a table:

Insert new record into a table explicitly naming fields:

insert into TableName (field1,field2,field3) values (1,2,3)

Insert new record into a table using values from another table:

MySQL (but not Oracle or SQL Server) allow a single insert statement to insert multiple rows rather than once at a time:

Sqlplus Cheat Sheet 2019

insert into TableName (field1,field2,field3)
values (1,2,3),(4,5,6)

Update

Update all records in a table:

Update specific records in a table:

To update more than one field at a time:

Update a field in a table using a value from another table where both records are referenced by a common key - warning, different databases support different syntax!

This works in MS-Access and MySQL (5) but not in SQL Server:

update TableOne
inner join TableTwo on TableOne.commonID = TableTwo.commonID
set TableOne.field1 = TableTwo.fieldX

or

This works in MS-Access and MySQL but not in SQL Server:

or

This works in SQL Server (my thanks to John Lee for this), but not in MS-Access or MySQL:

Note:

  • MS-Access gives the error 'Operation must use an updateable query' if you attempt to use any of the above with a view/query rather than a table. The work around is to copy the data from the query into a temporary table and use the temporary table instead.

Delete

Delete all records in a table (dangerous):

Delete specific records in a table:

Sqlplus Cheat Sheet Excel

Delete records from one table which do not have a matching field in another table:

Keys

Be aware that there are often subtle syntax variations between different database systems. Also other key properties (for example 'clustered') will vary between database systems. Therefore please treat this part of the SQL crib sheet as a guide only.

Create a primary key on a table:

Alter Table TheTable Add Primary Key (field1, field2)

To add an index on a field:

Sql Plus Cheat Sheet

To remove a primary key:

About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.