Understanding adventureworks2012 db structure - sql-server

I go through a tutorial on hierachyid and for their explanation they use the following query on the advantureworks2012
Select * From HumanRessources.Employee
I generated the diagram and can find a table called employee but no HumanRessources one. .
I tried to find a documentation of the AW2012 db but could only find one of the AW2008 which does not have HumanRessources.
My question: What is HumanRessources, since it is not a table I do not understand what it is and how this is implemented?

HumanResources is indeed the schema, with Employee being the table name.
You would therefore query the table using SELECT * FROM HumanResources.Employee;
You might find the official data dictionary useful here: https://technet.microsoft.com/en-us/library/ms124438(v=sql.100).aspx
Alternatively, see the following diagram for an overview of the AdventureWorks2008 database ( it's almost identical to 2012). Notice the shaded areas represent the different schemas.

Related

Issue SELECT with temporary table

I am able to DROP and CREATE a temporary table but when I do a select, it doesn't recognize the object. I know it's there but how can I access it?
FYI, I have multiple databases in SQL Server (2008). I tried the below but it doesn't work.
SELECT *
FROM tempdb..#TBL_IMPORT
Usually to access tables I have to type this: dbname.dbo.tablename
Any clue? Thank you.
With the amount of information given, the answer is in the comments.
If you would like to query a temp table from a second session, you'll need to create the temp table as a global temp table.
select *
into ##MyGlobalTable
from SourceTable
If you're using SSMS you will want to use the same window you create the temp table in if not using a global temp table. If you're using a secondary application you'll want to validate you're using the same SPID.
Other approaches you may be interested in would include CTEs (common table expressions) and variable tables. Google will have a wide assortment of assistance, or you could update your question here.

Difference between dba_segments and dba_users when trying to identify list of schema in Oracle DB

I am trying to figure out the list schema created in a database, I came across many answers like this and this which are trying to tell either use dba_segments or use dba_users.
But when I use those in my database then results have substantial difference.
I am looking for answers explaining which one is correct (dba_segments or dba_users) and why, so please do not think that my question is "how to get a list of all available schema in database".
dba_segments shows SEGMENTS - which are owned by schemas
you can have a schema that has no segments - objects that use segments can generally be thought of as tables or indexes. A user could own a synonym or a PL/SQL unit but have no segments for example.
Here's a list of segment types for my 12c system
HR#orcl🍻🍺 >select distinct segment_type from dba_segments;
SEGMENT_TYPE
LOBINDEX
INDEX PARTITION
ROLLBACK
NESTED TABLE
TABLE PARTITION
LOB PARTITION
LOBSEGMENT
INDEX
TABLE
CLUSTER
dba_users will show you EVERY user in the database, whether they own 'data' or not
here's how to find SCHEMAS with no segments, or one way
HR#orcl🍻🍺 >select distinct username
2 from dba_users
3 minus
4 select distinct owner
5 from dba_segments;
USERNAME
ANONYMOUS
APEX_LISTENER
APEX_PUBLIC_USER
APEX_REST_PUBLIC_USER
APPQOSSYS
BASIC_PRIVS
BI...

What is parent_left and parent_right columns on Odoo ERP database?

Does anyone know what is the usage of parent_left or parent_right columns in Odoo ERP database? As I can see there are already parent_id column which is useful. What I don't see usage for parent_left or parent_right. You can see them using below queries.
select * from stock_location;
select * from account_account
Thanks
The parent_left and parent_right are 2 special fields that are related
to the parent_id field. The purpose of those fields is to make queries
within the hierarchy execute efficiently: with parent_left and
parent_right, you can retrieve all the descendants of a node without
making recursive queries.
You must refer this link: https://answers.launchpad.net/openobject-server/+question/186704

What is a good way to document what your tables and columns mean in a SQL Server database?

I'm working on a C# project using a SQL Server 2008 database. There are 50+ tables in the database and from the name alone, the purpose of these tables and the columns within them aren't immediately obvious. I want to create some form of documentation so future team members will know what the columns and tables do.
I'm looking for the SQL Server equivalent of C# "code comments" or "XML documentation on a method" - something a new person can glance at to gain understanding of a database table or column.
What are some options?
You can add extended properties
Viewing Extended Properties
e.g. displays all extended properties in the AdventureWorks2008R2 sample database
USE AdventureWorks2008R2;
GO
SELECT class, class_desc, major_id, minor_id, name, value
FROM sys.extended_properties;
GO
Fun with Extended Properties in SQL Server 2008
A simple approach to document table columns is to use the SQL Server Management Studio table designer. Fill in the Description field for each column.
Then you can view table info with a query joining the sys.extended_properties table:
-- list table columns
SELECT OBJECT_NAME(c.OBJECT_ID) [TableName], c.name [ColumnName], t.name [DataType], x.Value [Description], c.max_length [Length], c.Precision ,c.Scale
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
LEFT JOIN sys.extended_properties x on x.major_id = c.object_id and x.minor_id = c.column_id
WHERE OBJECT_NAME(c.OBJECT_ID) = 'Employees'
ORDER BY c.OBJECT_ID, c.column_id;
Extended properties? Can be set in code or in the GUI
We also use Red Gate SQL Doc to publish our schema too and extended properties appear in the descriptions here. Very useful.
Documentation is a whole skill set by itself. Good naming methodologies are good, as are consistency so that there aren't any changes across the database in regards to your style.
Besides using SQL Diagrams or something, the best thing is a good document that explains the tables and columns. Define the overall structure and relationships between objects/tables/columns. Give explanations if concepts are more complicated, but make it easy for someone to say 'what does this column do?' and find that answer quick without having to read through a paragraph or two to understand.
Are you willing to investigate (and possibly later invest in) a separate tool for this job??
If so - have a look at these two:
Red-Gate SQL Doc (browse their Sample AdventureWorks HTML doc online)
ApexSQL Doc
Both aren't free - they do cost a bit of money upfront - but both create excellent quality documentation, in HTML, PDF, CHM format - your choice. They also get every little piece of information from the database - all the relationships between objects, constraints and default values on columns, and a lot more.
In my opinion definitely worth the initial investment, if you're doing any serious kind of work.

How to create column names/descriptors programmatically

In SQL Server given a Table/View how can you generate a definition of the Table/View in the form:
C1 int,
C2 varchar(20),
C3 double
The information required to do it is contained in the meta-tables of SQL Server but is there a standard script / IDE faciltity to output the data contained there in the form described above ?.
For the curious I want this as I have to maintain a number of SP's which contain Table objects (that is a form of temporary table used by SQL Server). The Table objects need to match the definition of Tables or Views already in the database - it would make life a lot easier if these definitions could be generated automatically.
Here is an example of listing the names and types of columns in a table:
select
COLUMN_NAME,
COLUMN_DEFAULT,
IS_NULLABLE,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE
from
INFORMATION_SCHEMA.COLUMNS
where
TABLE_NAME = 'YOUR_TABLE_NAME_HERE'
order by
Ordinal_Position
Generating DDL from that information is more difficult. There seems to be some suggestions at SQLTeam
If you want to duplpicate a table definition you could use:
select top 0
*
into
newtable
from
mytable
Edit: Sorry, just re-read your question, and realised this might not answer it. Could you be clear on what you are after, do you want an exact duplicate of the table definition, or a table that contains information about the tables definition?
Thanks for your replies. Yes I do want an exact duplicate of the DDL but I've realised I misstated exactly what I needed. It's DDL which will create a temporary table which will match the columns of a view.
I realised this in looking at Duckworths suggestion - which is good but unfortunately doesn't cover the case of a view.
SELECT VIEWDEFINITION FROM
INFORMATIONSCHEMA.VIEWS
... will give you a list of columns in a view and (assuming that all columns in the view are derived directly from a table) it should then be possible to use an amended version of Duckworths suggestion to pull together the relevant DLL.
I'm just amazed it's not easier ! I was expecting someone to tell me that there was a well established routine to do this given the TABLE objects need to have all columns full defined (rather than the way Oracle does it which is to say - "give me something which looks like table X".
Anyway thanks again for help and any further suggestions welcomed.
In this posting to another question I've got a DB reverse engineering script that will do tables, views, PK, UK and index definitions and foreign keys. This one is for SQL Server 2005 and is a port of one I originally wrote for SQL Server 2000. If you need a SQL Server 2000 version add a comment to this post and I'll post it up here.

Resources