Whаt Iѕ A Cоdе Sniрреt?

Acronyms, Abbreviations, Terms, And Definitions
Acronyms, Abbreviations, Terms, And Definitions

 

Whаt Iѕ A Cоdе Sniрреt?

Thе code ѕniрреt iѕ a tеrm uѕеd in рrоgrаmming tо rеfеr tо ѕmаll раrtѕ оf reusable source соdеѕ. Suсh kinds оf соdеѕ аrе аvаilаblе both in binary оr tеxt context. Cоdе ѕniрреtѕ are commonly dеfinеd аѕ unitѕ or funсtiоnаl mеthоdѕ thаt can bе rеаdilу intеgrаtеd intо larger modules рrоviding functionality. Thiѕ technical tеrm iѕ аlѕо uѕеd to refer tо the рrасtiсе оf minimizing thе uѕе of repeated code thаt iѕ common to many applications.

Java рrоgrаmmеrѕ use соdе ѕniрреtѕ аѕ an informative mean tо ѕuрроrt the рrосеѕѕ оf еnсоding. Normally, a ѕniрреt shows an еntirе functional unit corresponding tо code a ѕmаll рrоgrаm, оr a ѕinglе funсtiоn, a сlаѕѕ, a template or a bunch of related funсtiоnѕ.

Prоgrаmmеrѕ use ѕniрреt codes with thе ѕаmе purposes аѕ аn аррliсаtiоn. Fоr еxаmрlе, they uѕе it as a way to ѕhоw the соdе as a proven ѕоlutiоn to a givеn рrоblеm. Thеу mау аlѕо use this tо illuѕtrаtе рrоgrаmming “triсkѕ” of nоn-triviаl imрlеmеntаtiоn to highlight thе ресuliаritiеѕ of a givеn соmрilеr. Sоmе реорlе uѕе thiѕ as an еxаmрlе оf соdе portability оr еvеn tо uѕе thеm tо lower the Jаvа programming timе. Organic аnd thеmаtiс collections of ѕniрреt соdеѕ inсludе thе digital соllесtiоn оf tiрѕ аnd triсkѕ аnd асt аѕ a ѕоurсе fоr lеаrning and rеfining рrоgrаmming.

Thе snippet iѕ ѕhоrt аnd fulfillѕ thе раrtiсulаr tаѕk well, it dоеѕ nоt nееd any еxtrа соdе beyond ѕtаndаrd library and ѕуѕtеm dереndеnt code. Thе ѕniрреt iѕn’t the complete рrоgrаm – аnd for thаt you will ѕubmit thе соdе in the ѕоurсе code rероѕitоrу that iѕ thе bеѕt place to handle the lаrgеr рrоgrаmѕ. Ideally, thе ѕniрреt must be thе ѕесtiоn of соdе, whiсh уоu mау ѕniр оut оf the lаrgеr рrоgrаm аnd very еаѕilу reuse in оthеr рrоgrаm. In order, to mаkе ѕniрреtѕ ѕimрlе tо use, it is good to еnсарѕulаtе in thе funсtiоn, сlаѕѕ аnd роtеntiаllу, аѕ thе framework tо ѕtаrt thе new рrоgrаm.

For a рrоgrаmmеr, having gооd code ѕniрреtѕ iѕ vеrу imроrtаnt. Mаnу people uѕе different wау tо kеер thеir code with them. Thеrе iѕ a lоt of оnlinе ѕоlutiоn аlѕо for thоѕе likе аgаinѕt. Hаving gооd соdе in hаnd is vеrу imроrtаnt tо dеlivеr best in class рrоduсt. Sniрреtѕ should bе аlwауѕ mоdulаr and роrtаblе. Sо that iѕ should bе plugged intо уоur соdе easily. Many реорlе uѕе github giѕt to keep thеir snippets. Rubу рrоgrаmmеrѕ uѕе mоdulеѕ to сrеаtе соdе ѕniрреtѕ.

Netezza / PureData – How to add a Foreign Key

DDL (Data Definition Language), Netezza PureData How to add a Foreign Key
DDL (Data Definition Language)

Adding a forging key to tables in Netezza / PureData is a best practice; especially, when working with dimensionally modeled data warehouse structures and with modern governance, integration (including virtualization), presentation semantics (including reporting, business intelligence and analytics).

Foreign Key (FK) Guidelines

  • A primary key must be defined on the table and fields (or fields) to which you intend to link the foreign key
  • Avoid using distribution keys as foreign keys
  • Foreign Key field should not be nullable
  • Your foreign key link field(s) must be of the same format(s) (e.g. integer to integer, etc.)
  • Apply standard naming conventions to constraint name:
    • FK_<<Constraint_Name>>_<<Number>>
    • <<Constraint_Name>>_FK<<Number>>
  • Please note that foreign key constraints are not enforced in Netezza

Steps to add a Foreign Key

The process for adding foreign keys involves just a few steps:

  • Verify guidelines above
  • Alter table add constraint SQL command
  • Run statistics, which is optional, but strongly recommended

Basic Foreign Key SQL Command Structure

Here is the basic syntax for adding Foreign key:

ALTER TABLE <<Owner>>.<<NAME_OF_TABLE_BEING_ALTERED>>

ADD CONSTRAINT <<Constraint_Name>>_fk<Number>>

FOREIGN KEY (<<Field_Name or Field_Name List>>) REFERENCES <<Owner>>.<<target_FK_Table_Name>.(<<Field_Name or Field_Name List>>) <<On Update | On Delete>> action;

Example Foreign Key SQL Command

This is a simple one field example of the foreign key (FK)

ALTER TABLE Blog.job_stage_fact

ADD CONSTRAINT job_stage_fact_host_dim_fk1

FOREIGN KEY (hostid) REFERENCES Blog.host_dim(hostid) ON DELETE cascade ON UPDATE no action;

Related References

Alter Table

PureData System for Analytics, PureData System for Analytics 7.2.1, IBM Netezza database user documentation, Netezza SQL command reference, Alter Table, constraints

Database – What is a foreign key?

Acronyms, Abbreviations, Terms, And Definitions, DDL (Data Definition Language), What is a foreign key
Acronyms, Abbreviations, Terms, And Definitions

Definition of a Foreign Key

  • A foreign Key (FK) is a constraint that references the unique primary key (PK) of another table.

Facts About Foreign Keys

  • Foreign Keys act as a cross-reference between tables linking the foreign key (Child record) to the Primary key (parent record) of another table, which establishing a link/relationship between the table keys
  • Foreign keys are not enforced by all RDBMS
  • The concept of referential integrity is derived from foreign key theory
  • Because Foreign keys involve more than one table relationship, their implementation can be more complex than primary keys
  • A foreign-key constraint implicitly defines an index on the foreign-key column(s) in the child table, however, manually defining a matching index may improve join performance in some database
  • The SQL, normally, provides the following referential integrity actions for deletions, when enforcing foreign-keys

Cascade

  • The deletion of a parent (primary key) record may cause the deletion of corresponding foreign-key records.

No Action

  • Forbids the deletion of a parent (primary key) record, if there are dependent foreign-key records.   No Action does not mean to suppress the foreign-key constraint.

Set null

  • The deletion of a parent (primary key) record causes the corresponding foreign-key to be set to null.

Set default

  • The deletion of a record causes the corresponding foreign-keys be set to a default value instead of null upon deletion of a parent (primary key) record

Related References

Netezza / PureData – How to Number for day of week in SQL?

Netezza / PureData - Numeric Day of Week
Netezza / PureData – Numeric Day of Week

I had reason today to get the number of the day of the week, in PureData / Netezza, which I don’t seem to have discussed in previous posts.  So, here is a simple script to get the number for the day of week with a couple of flavors, which may prove useful.

Basic Format

select extract(dow from <<FieldName>>) from <<SchemaName>>.<<tableName>>

Example SQL

 

SELECT

CURRENT_DATE

,  TO_CHAR(CURRENT_DATE,’DAY’) AS DAY_OF_WEEK

—WEEK STARTS ON MONDAY

,  EXTRACT(DOW FROM CURRENT_DATE)-1 AS DAY_OF_WEEK_NUMBER_STARTS_ON_MONDAY

—WEEK STARTS ON SUNDAY

,  EXTRACT(DOW FROM CURRENT_DATE) AS DAY_OF_WEEK_NUMBER_STARTS_ON_SUNDAY

—WEEK STARTS ON SATURDAY

,  EXTRACT(DOW FROM CURRENT_DATE)+1 AS DAY_OF_WEEK_NUMBER_STARTS_ON_SATURDAY

FROM _V_DUAL;

 

Related References

Extract date and time values

PureData System for Analytics, PureData System for Analytics 7.2.1, IBM Netezza database user documentation, Netezza SQL basics, Functions and operators, Functions, Extract date and time values

Date/time functions

PureData System for Analytics, PureData System for Analytics 7.2.1, IBM Netezza database user documentation, Netezza SQL basics, Netezza SQL extensions, Date/time functions

Template patterns for date/time conversions

PureData System for Analytics, PureData System for Analytics 7.2.1, IBM Netezza database user documentation, Netezza SQL basics, Netezza SQL extensions, Conversion functions, Template patterns for date/time conversions

https://www.ibm.com/support/knowledgecenter/en/SSULQD_7.2.1/com.ibm.nz.dbu.doc/r_dbuser_ntz_sql_extns_templ_patterns_date_time_conv.html

Database – What is a Composite Primary Key?

Database Table
Database Table

What is a Composite Primary Key?

A Composite Primary key is Primary key What a primary key, which is defined by having multiple fields (columns) in it.  Like a Primary Key what a composite Primary Key is depends on the database.  Essentially a Composite Primary Key:

  • Is a combination of Fields (columns) which uniquely identifies every row.
  • Is an index in database systems which use indexes for optimization
  • Is a type of table constraint
  • Is applied with a data definition language (DDL) alter command
  • And may define parent-Child relationship between tables

Related References

Data Modeling – Dimension Table Effective Practices

Database Table
Database Table

I’ve had these notes laying around for a while, so, I thought I consolidate them here.   So, here are few guidelines to ensure the quality of your dimension table structures.

Dimension Table Effective Practices

  • The table naming convention should identify it as a dimension table. For example:
    • Suffix Pattern:
      • <<TableName>>_Dim
      • <<TableName>>_D
    • Prefix Pattern:
      • Dim_<TableName>>
      • D_<TableName>>
  • Have Primary Key (PK) assigned on table surrogate Key
  • Audit fields – Type 1 dimensions should:
    • Have a Created Date timestamp – When the record was initially created
    • have a Last Update Timestamp – When was the record last updated
  • Job Flow: Do not place the dimension processing in the fact jobs.
  • Every Dimension should have a Zero (0), Unknown, row
  • Fields should be ‘NOT NULL’ replacing nulls with a zero (0) numeric and integer type fields or space ( ‘ ‘ ) for Character type files.
  • Keep dimension processing outside of the fact jobs

Related References

Datastage – When checking operator: Operator of type “APT_TSortOperator”: will partition despite the preserve-partitioning flag on the data set on input port 0

APT_TSortOperator Warning
APT_TSortOperator Warning

The APT_TSortOperator  warning happens when there is a conflict in the portioning behavior between stages.  Usually, because the successor (down Stream) stage has the ‘Partitioning / Collecting’ and ‘Sorting’ property set in a way that conflicts with predecessor (upstream) stage’s properties, which it is set to preserver.  This can occur when the successor stage has the “Preserve Partitioning” property set to:

  • ‘Default (Propagate)’
  • ‘Propagate’, or
  • ‘Set’

Preserve Partitioning Property - list
Preserve Partitioning Property – list

Message ID

  • IIS-DSEE-TFOR-00074

Message Text

  • <<Link Name Where Warning Occurred>>: When checking operator: Operator of type “APT_TSortOperator”: will partition despite the preserve-partitioning flag on the data set on input port 0.

Warning Fixes

  • First, if the verify that the partitioning behaviors of both stages are correct
  • If so, set the predecessor ‘Preserve Partitioning’ property to “Clear”
  • If not, then correct the partitioning behavior of the stage which is in error

Clear Partitioning Property Screenshot

Preserve Partitioning Property - Set To Clear
Preserve Partitioning Property – Set To Clear