CCleaner :- Defraggler

Background

Using Microsoft Windows Computer Manager \ Disk Administrator.

Trying to change my logical disks size by shrinking one volume and expand an adjacent volume unto the availed space.

 

Computer Management

Disk Administrator

Images

Outline

  1. Querying Shrink Space
  2. Shrink Space ( 6 GB )
  3. Error
    • You cannot shrink a volume beyond the point where any unmovable files are located.

Image – Querying Shrink Space

Image – Shrink Space ( 6 GB )

 

Image – You cannot shrink a volume beyond

Image

Textual

You cannot shrink a volume beyond the point where any unmovable files are located.

See the “defrag” event in the Application log for detailed information about the operation when it has completed.

 

Defraggler

Download

Downloaded Defraggler from CCleaner’s web site.

The URL is https://www.ccleaner.com/defraggler ( Link )

 

Installed

Installed Defraggler.

Use

Outline

  1. Launch Defraggler
  2. Select Drive
  3. Analyze Disk
  4. Defrag Disk

Image

Image 01

 

Image 02

 

Image 03

 

Summary

De-fragmenting the disk uncluttered our files and created “whole” unused storage area.

Rebooted the machine.

And, we were able to return to Computer Management, and shrink the disk volume.

Sample Database – w3resource

Background

Looking for sample database model for a query that I had in mind.

Found a simple on at w3Resource.

 

w3Resource

The sample is title “SQL Exercises, Practice, Solution – exercises on employee Database”.

And, it is available here.

 

Model

Data

Employee

 

SQL

Data Definition Language ( DDL )

Employee

SQL


if schema_id('w3resource') is null
begin

    exec('create schema [w3resource] authorization [dbo]')

end
go

/*

    w3resource
    SQL employee Database [115 Exercise with Solution]
    https://www.w3resource.com/sql-exercises/employee-database-exercise/index.php

*/

/*

    drop table [w3resource].[employee];

*/
if object_id('[w3resource].[employee]') is null
begin

    create table [w3resource].[employee]
    (
          [id]              int          not null
                            identity(1,1)

        , [empID]           int          not null

        , [empName]         varchar(60)  not null

        , [jobName]         varchar(60)  null

        , [managerID]       int          null

        , [hireDate]        date         null

        , [salary]          decimal(10,2) null

        , [commision]       decimal(7,2) null

        , [departmentID]    int          null

        , constraint [PK_w3resource.employee]
            primary key
            (
                [empID]
            )
        , constraint [FK_w3resource.employee.managerID]
            foreign key
            (
                [managerID]
            )
            references [w3resource].[employee]
            (
                [empID]
            )

   ) 

end   

go



 

Data Manipulation Language ( DML )

Employees

SQL



set nocount on
go

set XACT_ABORT on
go


/*

    w3resource
    SQL employee Database [115 Exercise with Solution]
    https://www.w3resource.com/sql-exercises/employee-database-exercise/index.php

*/

delete 
from    [w3resource].[employee]
;

DBCC CHECKIDENT ('[w3resource].[employee]', RESEED, 0)  
go

insert into [w3resource].[employee]
(
      [empID]   
    , [empName] 
    , [jobName] 
    , [managerID]
    , [hireDate]
    , [salary]
    , [commision]
    , [departmentID]
)     
select   
      63819 as [empID]   
    , 'Kayling' as [empName] 
    , 'President' as [jobName] 
    , null as [managerID]
    , '1991-11-18'
    , 6000
    , null
    , 1001


union all

select   
      66928 as [empID]   
    , 'Blaze' as [empName] 
    , 'Manager' as [jobName] 
    , 63819 as [managerID]
    , '1991-05-01'
    , 2750
    , null
    , 3001

union all

select   
      67832 as [empID]   
    , 'Clare' as [empName] 
    , 'Manager' as [jobName] 
    , 63819 as [managerID]
    , '1991-06-09'
    , 2550
    , null
    , 1001

union all

select   
      65646 as [empID]   
    , 'Jonas' as [empName] 
    , 'Manager' as [jobName] 
    , 63819 as [managerID]
    , '1991-04-02'
    , 2957.00
    , null
    , 2001

union all

select   
      67858 as [empID]   
    , 'Scarlet' as [empName] 
    , 'Analyst' as [jobName] 
    , 65646 as [managerID]
    , '1997-04-19'
    , 3100.00
    , null
    , 2001


union all

--Frank
select   
      69062 as [empID]   
    , 'Frank' as [empName] 
    , 'Analyst' as [jobName] 
    , 65646 as [managerID]
    , '1991-12-03'
    , 3100.00
    , null
    , 2001


union all

-- Sandrine
select   
      63679 as [empID]   
    , 'Sandrine' as [empName] 
    , 'Clerk' as [jobName] 
    , 69062 as [managerID]
    , '1990-12-18'
    , 900
    , null
    , 2001

union all

-- Adelyn
select   
      64989 as [empID]   
    , 'Adelyn' as [empName] 
    , 'Salesman' as [jobName] 
    , 66928 as [managerID]
    , '1991-02-20'
    , 1700
    , 400
    , 3001


union all

-- Wade
select   
      65271 as [empID]   
    , 'Wade' as [empName] 
    , 'Salesman' as [jobName] 
    , 66928 as [managerID]
    , '1991-02-22'
    , 1350.00
    , 600.00
    , 3001

union all
-- Madden
select   
      66564 as [empID]   
    , 'Madden' as [empName] 
    , 'Salesman' as [jobName] 
    , 66928 as [managerID]
    , '1991-09-28'
    , 1350.00
    , 600.00
    , 3001

union all

-- Tucker
select   
      68454 as [empID]   
    , 'Tucker' as [empName] 
    , 'Salesman' as [jobName] 
    , 66928 as [managerID]
    , '1991-09-08'
    , 1600.00
    , 0.00
    , 3001

union all

-- Adnres
select   
      68736 as [empID]   
    , 'Adnres' as [empName] 
    , 'Clerk' as [jobName] 
    , 67858 as [managerID]
    , '1997-05-23'
    , 1200.00
    , null
    , 2001

union all

-- Julius
select   
      69000 as [empID]   
    , 'Julius' as [empName] 
    , 'Clerk' as [jobName] 
    , 66928 as [managerID]
    , '1991-12-03'
    , 1050.00
    , null
    , 3001

union all

-- Marker
select   
      69324 as [empID]   
    , 'Marker' as [empName] 
    , 'Clerk' as [jobName] 
    , 67832 as [managerID]
    , '1992-01-23'
    , 1400.00
    , null
    , 1001

go

 

Source Control

GitHub

DanielAdeniji/w3ResourceDBSample
Link

Transact SQL :- List Default Constraints

Background

List SQL Server Default Constraints.

Metadata

If one decides to use Sequences, one has to be able to document where it is being used.

Identity Columns

Identify Columns has self documenting tooling.

Inclusive :-

  1. sys.columns
    • is_identity
  2. sys.identity_columns
  3. OBJECTPROPERTY
    • TableHasIdentity
  4.  COLUMNPROPERTY
    • IsIdentity

Sequence

There is really nothing like a Sequence Column.

They are defined outside of a table definition.

It is not intrinsic so to speak.

It is a service that can be used as a communal toolset.

Outline

To detect use we can ask :-

  1. sys.columns
  2. sys.default_constraints
  3. sys.sql_expression_dependencies

Code


 if SUSER_ID('rdsa') is null
 begin

    use [master]

 end
 go

 if object_id('[dbo].[sp_helpConstraintDefault]') is null
 begin

    exec('create procedure [dbo].[sp_helpConstraintDefault] as ')

 end
 go

 alter procedure [dbo].[sp_helpConstraintDefault]
 as
 begin

   select 

              [schema]
                = tblSS.[name]

            , [object]
                = tblSO.[name]

            , [column]
                = tblSC.[name]

            , [columnType]
                = tblST.[name]

            , [is_identity]
                = tblSC.is_identity

            , [is_rowguidcol]
                = tblSC.is_rowguidcol

            , [columnDefault]
                = tblSDC.[name]

            , [definition]
                = tblSDC.[definition]

            , [referencedObject]
                = quotename
                    (
                        tblSORefS.[name]
                    )
                    + '.'
                    + quoteName
                        (
                            tblSORef.[name]
                        )

            , [referencedObjectType]
                = tblSORef.[type_desc]

    from   sys.columns tblSC

    inner join sys.types tblST

        on  tblSC.system_type_id = tblST.system_type_id

        and tblSC.user_type_id = tblST.user_type_id

    inner join sys.objects tblSO

        on tblSC.[object_id] = tblSO.[object_id]

    inner join sys.schemas tblSS

        on tblSO.[schema_id] = tblSS.[schema_id]

    inner join sys.default_constraints tblSDC

        on  tblSC.object_id = tblSDC.parent_object_id

        and tblSC.column_id = tblSDC.parent_column_id

    left outer join sys.sql_expression_dependencies AS tblSSED 

        on tblSC.[default_object_id] = tblSSED.referencing_id

    left outer join sys.objects tblSORef

        on tblSSED.referenced_id = tblSORef.[object_id]

    left outer join sys.schemas tblSORefS
        on tblSORef.[schema_id] = tblSORefS.[schema_id]

    order by

              tblSS.[name]

            , tblSO.[name]

            , tblSC.[name]

 end
 go

 if SUSER_ID('rdsa') is null
 begin

    exec sp_MS_marksystemobject '[dbo].[sp_helpConstraintDefault]'

 end
 go

Output

Output – Sequence – #1

Output – Database – Idf – #1

 

Source Control

GitHub

Repository

DanielAdeniji/dbo.sp_helpConstraintDefault
Link

Transact SQL :- Sequence Object

Background

As I read more about Hibernate, reminded once again the usage of Identity Columns to implement Sequences is quite unique to SQL Server and likely Sybase.

And, that Oracle, for one, uses Sequence to generate increasing numbers.

Hibernate

hibernate.IdGeneratorStrategy

Sample Code

Outline

  1. Schema
    • Does Schema exists?
      • schema_id()
    • If Schema does not exist?
      • Create Schema
  2. Sequence
    • Does Sequence exists?
      • Consult sys.sequences
    • If Sequence does not exist
      • Create Using “CREATE SEQUENCE”
  3. Utilize Sequence Generator
    • Add Data
      • Get next available Sequence
        • Syntax
          • NEXT VALUE FOR [sequence object]
        •  Sample
          • NEXT VALUE FOR [lab].[seq]
  4. Review Sequence Objects
    • sys.sequences
      • name
      • is_cached
      • increment
      • current_value
      • is_exhausted
      • maximum_value

Code


use [tempdb]
go

set nocount on
go

set XACT_ABORT on
go

declare @commit bit

declare @object  sysname

declare @schema_id int
declare @schema    sysname
declare @sequence  sysname

set @commit = 0

set @schema = 'lab'
set @sequence = 'seq';

begin tran

    /*
        Get schema_id for schema @schema
    */
    set @schema_id = schema_id(@schema);

    /*
        If schema_id is null, then does not exist
        create it
    */
    if @schema_id is null
    begin

        exec('create schema [lab] authorization [dbo]')

    end

    set @schema_id = schema_id(@schema);

    /*
        If schema does not exist, please create it
    */
    if not exists
    (
        select *
        from   sys.sequences
        where  [name] = @sequence
        and    [schema_id] = @schema_id
    )
    begin

        CREATE SEQUENCE [lab].[seq]
        AS INTEGER
        START WITH 1
        INCREMENT BY 1
        MINVALUE 1
        --MAXVALUE 1000000
        CYCLE
        ; 

    end

    /*
        Please create table lab.vehicle
    */
    if object_id('[lab].[vehicle]') is null
    begin

        create table [lab].[vehicle]
        (
            id bigint not null
                DEFAULT NEXT VALUE FOR [lab].[seq]
        )

    end

    /*
        Please create table lab.bridge
    */
    if object_id('[lab].[bridge]') is null
    begin

        create table [lab].[bridge]
        (
            id bigint not null
                DEFAULT NEXT VALUE FOR [lab].[seq]

            , [name] nvarchar(80) not null

        )

    end

    /*
        Insert data
    */
    insert into [lab].[vehicle] default values;

    insert into [lab].[bridge]([name]) values('Seven Mile Bridge');

    insert into [lab].[vehicle] default values;

    insert into [lab].[bridge]([name]) values('Royal Gorge Bridge and Park');

    insert into [lab].[bridge]([name]) values('New River Gorge Bridge');

    /*
        Review data
    */
    select *

    from   [lab].[vehicle]

    select *

    from   [lab].[bridge]

    /*
        Review Sequence
    */
    select
              [schema] = tblSS.[name]
            , tblSeq.[name]
            , tblSeq.is_cached
            , tblSeq.increment
            , tblSeq.current_value
            , tblSeq.is_exhausted
            , tblSeq.maximum_value

    from   sys.sequences tblSeq

    inner join sys.schemas tblSS

            on tblSeq.[schema_id] = tblSS.schema_id

    where  tblSeq.[name] = @sequence
    ;

while (@@TRANCOUNT>0)
begin

    if (@commit =1)
    begin

        commit tran;

    end
    else
    begin

        rollback tran;
    end
end

Output

sequence.lab.20190512.1134PM

SQL Server Agent on AWS/RDS

Background

There is quite a bit of SQL Server management surface area that is not available when running on AWS/RDS.

 

Surface Area

For instance with SQL Server Agent :-

  1. SQL Server Agent
    • Category
      • Add new category
        • API
          • sp_add_category
        • Error
          • Msg 229, Level 14, State 5, Procedure msdb.dbo.sp_add_category
          • The EXECUTE permission was denied on the object ‘sp_add_category’, database ‘msdb’, schema ‘dbo’.
    • Job Step
      • Fetch Job Steps
        • API
          • SELECT * FROM msdb.dbo.sysjobsteps
        • Error
          • Msg 229, Level 14, State 5, Line 71
          • The SELECT permission was denied on the object ‘sysjobsteps’, database ‘msdb’, schema ‘dbo’.

 

Scripting

In terms of scripting, SQL Server Agent is not accessible in an AWS/RDS Environment.

XQuery/Transact SQL – Case Sensitivity

Background

When issuing XPath queries, XQuery in SQL, one has to keep in mind that though the database itself could be defined as case-insensitive, XML is case-sensitive.

 

Create & Populate Data

Create Table

SQL


declare @tbl TABLE
(
	[id] smallint not null
		identity(1,1)

	, [xml] xml
)

Populate Table with data

SQL


insert into @tbl
(
	[xml]
)
select ''

union all

select ''

union all

select ''

union all

select ''

union all

select ''

union all

select ''

Data

 

Queries

  1. Normal Queries
    • Fetch all data
    • Fetch data whose part name is Teeth
  2. Function – fn:lower-case
    • Fetch all data
    • Fetch data whose part name is Teeth ( Case In-Sensitive )

Normal Query

Fetch all Records

Code


select
          [src] = 'all'

        , *

        , [count]
            = [xml].value
                (
                    '(/body/part/@count)[1]'
                    , 'smallint'
                ) 

        , [countOfteeth]
            = [xml].value
                (
                    '(/body/part[@name="teeth"]/@count)[1]'
                    , 'nvarchar(max)'
                ) 

from   @tbl

Output

Body Part = teeth


select
          [src] = 'name=teeth'

        , *

        , [count]
            = [xml].value
                (
                    '(/body/part/@count)[1]'
                    , 'smallint'
                ) 

        , [countofteeth]
            = [xml].value
            (
                '(/body/part[fn:lower-case(@name)="teeth"]/@count)[1]'
                , 'smallint'
            ) 

from   @tbl

where  [xml].exist
        (
            '(/body/part[@name="teeth"])'
        )
        = 1		 

 

Output

 

Function – Lower Case

Read All Records

SQL


select
          [src] = 'all'

        , *

        , [count]
            = [xml].value
                (
                    '(/body/part/@count)[1]'
                    , 'smallint'
                ) 

        , [countOfteeth]
            = [xml].value
                (
                    '(/body/part[@name="teeth"]/@count)[1]'
                    , 'smallint'
                ) 

        , [countOfteethCI]
            = [xml].value
              (
                '(/body/part[fn:lower-case(@name)="teeth"]/@count)[1]'
                , 'smallint'
              )

from   @tbl

Output

Body Part = teeth

SQL


select 

          [src] = 'name=teeth (lowercase)'

        , *

        , [countofteeth]
            = [xml].value
            (
                '(/body/part[fn:lower-case(@name)="teeth"]/@count)[1]'
                , 'smallint'
            ) 

from   @tbl

where  [xml].exist
        (
            '(/body/part[fn:lower-case(@name)="teeth"])'
        )
        = 1

Output

SQL Server Version

XML datatype and XQuery was introduced in SQL Server v2005.

But, v2005, programmability support was limited.

Some String functions such as lower-case and upper-case were not introduced till v2008.

 

Summary

You know your data.

If you suspect it might be mixed-case, please take “precausive” measures.

 

References

  1. Stackoveflow
    • Kevin Aenmey
      • how to perform a case-insensitive attribute selector in xquery
        Link
  2. Microsoft
    • XQuery
      • XQuery Functions against the xml Data Type
        Link
      • Functions on String Values – lower-case
        Link
  3. IBM
    • IBM Knowledge Center
      • Home > IBM i 7.1 > Database Programming > SQL XML programming > Overview of XPath > Descriptions of XPath functions
        • fn:lower-case function
          Link
  4. W3.Org
    • Xpath Functions
      • XQuery, XPath, and XSLT Functions and Operators Namespace Document
        Link

Event Viewer – Error – “MMC cannot open the file C:\Windows\system32\eventvwr.msc”

Background

On one of our MS Windows Systems, I  have been Unable to use Event Viewer.

Error

Error Image

MMCCannotOpenTheFile.PNG

Error Text

MMC cannot open the file C:\Windows\system32\eventvwr.msc.

Remediation

Outline

  1. Launch Microsoft Management Console ( MMC.exe) shell
    • mmc.exe
  2. In new empty shell
    • Add Event Viewer SnapIn
      • From the “Available snap-ins”
        • Select “Event Viewer”
      • Click Add Button
      • SnapIn should appear under “Selected Snapins”
    • Save Console
      • Save Console under a new name
  3. Launch new MMC Console
  4. Once happy
    • Once happy, return to MMC and overwrite original Event Viewer

Image

New Empty Shell

Console.MMC.01.PNG

Select Computer

Console.MMC.03.SelectComputer.20190425.0153PM.PNG

Add or Remove Snap-ins

Console.MMC.02.AddAndRemove.20190425.0152PM.PNG

Save SnapIn

Save SnapIn – Save As – 01

OSDesktop.Windows.System32.saveAs.20190425.0256PM

Save SnapIn – Save As – 02

OSDesktop.Windows.System32.saveAs.20190425.0258PM.PNG