SQL Developer Shortcuts - sql

I am using SQL Developer to connect to an Oracle DB.
I would like to be able to see the constraints of a table via the command window. Usually I have to navigate the tables tree and then open the table details. Is there a shortcut to allow me to access the constraints (in particular the FK constraints) by writing a command? Ideally I want something like
desc table_name
where desc describes the table. I know desc is SQL based but are there any commands I can use within SQL developer?
Thanks in advance.

If you can browse for the table in table browser under your connection. Open it. You can find a constraints tab where you can find the information about constraints on the table. Instead you can click on table name in command window (the worksheet) press the shortcut Shift +F4 which will give the same information.

A similar question was asked here:
http://p2p.wrox.com/oracle/30730-sp_help-equivalent-oracle.html
Someone suggested:
select * from user_constraints where table_name=[yourtable]
There are other suggestions too...

The following select command will show the table details in Oracle
Select * from user_tables
where table_name='COUNTRIES'
Another solution to see the description of table in Oracle, on sql> prompt
SQL>Description Countries
or
SQL>Desc Countries
This will show the column name and datatype as well as constraints.

Related

Copy Table Constraints/Keys along with Data and Structure

I have table "TableA", I want to make a copy of it "TableA_Copy", when I use the below script, it creates Table and data, but Constraints are not copied, is it possible copy the constraints along with Structure and Data
SELECT * INTO TableA_Copy FROM TableA
Note am using SQL Server 2016
Right click on the database and go to tasks->Generate script.
Select the table you want TableA, go to next step and ynder advanced options select data and schema.
Save the script or have it in a new query window.
Once the script is generated, replace TableA with TableA_copy
This way you will get data, schema and all the constraints. Remember to change the name of constraints to avoid any errors.
If you mean programmatically, then yes there are a number of ways in tsql to accomplish this by using the INFORMATION_SCHEMA views. The particular ones you will need for the table/columns are INFORMATION_SCHEMA.TABLES, and INFORMATION_SCHEMA.COLUMNS. For the constraints you can use INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE which will provide you the PK and FKs and INFORMATION_SCHEMA.CHECK_CONSTRAINTS for all check constraints.
Using these you can rebuild the table, complete, for use as you indicate above.

How to view a normal table in SQL

SQL using sp_HelpText to view a stored procedure is used to view a stored procedure but what if i want to view my table,
is there any query to view my table? (I am newb to Sql and C#)
You can use sp_help "table_name" or select table & press Alt+F1 to view details of table..To View script, you need to script that using SQL server Management Studio.
Expand Database > Tables > Right Click on the table > Click on Create Script.
Thanks
I would suggest using the native INFORMATION_SCHEMA views to get table information. The following query contains a complete list of columns, data types etc for a given table without needing to join several sys views.
SELECT
*
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
[TABLE_NAME] = 'YourTable'
The table details can be viewed by pressing Alt+F1 after highlighting the table name.

System table or stored procedure in Netezza to get create-table-sql of a table

I need to extract the create-table-sql of a table created in a database in Netezza, by using a query executed from a program. If there is any system table, stored procedure or otherwise, which let's me do that, please let me know. Your help will be highly appreciated.
The create statements are not saved in a table, however you can query the table structure from the system tables in you database. A good start is NFORMATION_SCHEMA.COLUMNS:
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'
All information on the create parameters is stored in the system tables like TABLES.
Dumping information about a table:
exec sp_help 'tableName'
If you have netezza SQL Aginity Editor 4.1 or above, then the following helps:
Right click on the tablename in the Object Browser Pane
Hover over Script in the menu
Click on 'DDl query to new window'
This will give you the SQL code required to create the table.

Oracle create table with column comments

Is there a column comment syntax that allows me to specify a column comment directly where I declare the column in the create table statement (i.e. inline)? The 11g spec does not mention anything, on another page something is mentioned but I could not get it to work. There is a way to specify comments after creating the table, but I think it is annoying that the comment is separated from the field definition. I am looking for something like this (which does not work):
create table whatever (
field number(15,0) primary key comment 'primary key generated from sequence pkseq',
...
)
I'm afraid the "annoying" COMMENT ON syntax is the only way of doing this. SQL Server, PostgreSQL and DB2 use the same syntax (even though, as far as I know, there is no ANSI standard syntax for adding comments to database objects).
MySQL supports the way you would like it to work. I agree it would be a nicer mechanism, but in my experience so few people use comments at all that I doubt Oracle will ever change it.
I'm afraid it can only be done after table creation, using the comment on column ... is '' syntax.
A workaround to this annoying syntax is also to view and edit the tables in Oracles SQLExplorer. It contains a wizard that allows you to edit the comments right next to the columns. It even allows easy creation of alter table scripts.
My procedure when editing tables is to enter the changes in the wizard without actually executing them, then go to its DDL tab and retrieve the SQL from there (as update, not full create script) and press cancel on the wizard. Then I put the created SQL into the SQL script I am writing. Only when I am finished with the script I execute everything; I do never make any changes with the wizard itself.
Test on sqlplus (or similar), but the syntax is as follows:
-- assuming you have privileges
COMMENT ON COLUMN SCHEMA1.TABLE1.COL1
IS 'My comment'
-- then you can double check like this
SELECT * FROM all_col_comments WHERE
(OWNER, TABLE_NAME, COLUMN_NAME)
IN (('SCHEMA1','TABLE1','COL1'));
Note that the comment will now show in SQLDeveloper (or Toad or whatever env you have) until you reopen said table's properties.
Similar syntax can be used to annotate tables, indexes and materialized views. [source: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4009.htm]
I understand similar syntax exists for MySQL and others, but it is not proper ANSI. It's very useful, though.

SSMS: How to sort objects by date?

i created a stored procedure recently, and i would like to sort the list of stored procedures by date.
12 years ago this was easy:
What is the preferred way to accomplish this in SQL Server Management Studio?
Bonus: How to sort by owner?
Answer: It's not possible.
Upate: Possible
Sometime in the intervening 9 years since i first asked this question, SSMS added the feature that was present c. 2000: Object Explorer Details:
And you get the functionality of a user interface that was created 25 years ago:
I understand why didn't want to let see items in a folder: it's so much work. It's easier to make the user's suffer everyday, rather than the developers once.
You'll want to right-click the database that you are interested in and select the New Query button in the toolbar. In the Query window, insert the following TSQL:
SELECT type, type_desc, name, create_date, modify_date
FROM sys.objects
GO
This gives you system objects as well so you'll have to filter it accordingly. You could always change the select statement to "SELECT *" to get a quick view of the other columns available.
I wanted to give you the starting point and you can go where you want with it.
i am not sure if it's preferred or not but, i guess you can insert the result table into a temptable and write a select query for that table with the order by.
downside, you have to write the column names and types every time.
declare #temptable table(Name nvarchar(50),Owner nvarchar(50),Type nvarchar(50),CreateDate DateTime)
insert into #temptable your_procedure
select * from #temptable order by CreateDate desc
In SSMS, you can go to "Object Explorer Details" (F7 in 2008, menu in 2005/2008)
You have a choice of details view, you can see the create date.
MSDN
I suspect it isn't what you want though... and I don't know it it works with SQL Server 2000