1.Difference
between VARCHAR and NVARCHAR in SQL Server
-
S.No Varchar[(n)] NVarchar[(n)] 1 Basic Definition:Non-Unicode Variable Length character data type.Example:DECLARE @FirstName AS VARCHAR(50) = ‘UMAR’SELECT @FirstNameBasic Definition:UNicode Variable Length character data type. It can store both non-Unicode and Unicode (i.e. Japanese, Korean etc) characters.Example:DECLARE @FirstName AS NVARCHAR(50)= ‘UMAR’SELECT @FirstName2 No. of Bytes required for each character:
It takes 1 byte per characterExample:DECLARE @FirstName AS VARCHAR(50) = ‘UMAR’SELECT @FirstName AS FirstName,DATALENGTH(@FirstName) AS LengthResult:FirstName LengthUMAR 4No. of Bytes required for each character:It takes 2 bytes per Unicode/Non-Unicode character.Example:DECLARE @FirstName AS NVARCHAR(50)= ‘UMAR’SELECT @FirstName AS FirstName,DATALENGTH(@FirstName) AS LengthResult:FirstName LengthUMAR 83 Optional Parameter n range:Optional Parameter n range:4 If Optional Parameter n is not specified in the variable declaration or column definition:If Optional parameter value is not specified in the variable declaration or column definition then it is considered as 1.Example:DECLARE @firstName VARCHAR =‘UMAR’SELECT @firstName FirstName,DATALENGTH(@firstName) LengthResult:FirstName LengthU 1If Optional Parameter n is not specified in the variable declaration or column definition:If Optional parameter value n is not specified in the variable declaration or column definition then it is considered as 2Example:DECLARE @firstName NVARCHAR =‘UMAR’SELECT @firstName FirstName,DATALENGTH(@firstName) LengthResult:FirstName LengthU 25 If Optional Parameter n is notspecified in while usingCAST/CONVERT functions:If Optional Parameter n is notspecified in while usingCAST/CONVERT functions:
When this optional parameter n is not specified while using the CAST/CONVERT functions, then it is considered as 30.Example:DECLARE @firstName VARCHAR(35) =‘UMAR ASIA INDIA TAMIL NADU CUDDALORE’SELECT CAST(@firstName AS VARCHAR) FirstName,DATALENGTH(CAST(@firstName AS VARCHAR)) LengthResult:FirstName LengthUMAR ASIA INDIA TAMIL NADU CUD 30
When this optional parameter n is not specified while using the CAST CONVERT functions, then it is considered as 30.Example:DECLARE @firstName NVARCHAR(35) =‘UMAR ASIA INDIA TAMIL NADU CUDDALORE’SELECT CAST(@firstName AS NVARCHAR) FirstName,DATALENGTH(CAST(@firstName AS NVARCHAR)) LengthResult:FirstName LengthUMAR ASIA INDIA TAMIL NADU CUD 607 Which one to use?If we know that data to be stored in the column or variable doesn’t have any Unicode characters.Which one to use?If we know that data to be stored in the column or variable can have Unicode characters.8 Storage Size:Takes no. of bytes equal to the no. of Characters entered plus two bytes extra for defining offset.Storage Size:Takes no. of bytes equal to twice the no. of Characters entered plus two bytes extra for defining offset.
2.Difference
between SQL Server and MySQL
-
S.No SQL Server MySQL 1 Current Date and Time:SELECT GETDATE()Current Date and Time:SELECT NOW()Optionally: Use CURDATE() for the date only.2 Limiting Results:
SELECT TOP 10 * FROM table WHERE id = 1Limiting Results:SELECT * FROM table WHERE id = 1 LIMIT 103 Date Field Default Value:Date Field Default Value:DATETIME fields cannot have a default value, i.e. "GETDATE()"We must use your INSERT statement to specify CURDATE() for the field.4 Character Length:LEN()Character Length:CHARACTER_LENGTH()Aliases: CHAR_LENGTH(), LENGTH()5 Character Replace:REPLACE() works case insensitivelyCharacter Replace:REPLACE() works case sensitively6 Trim Functions:LTRIM() and RTRIM()Trim Functions:TRIM()7 String Concatenation:CONCATENATION USING + (Does not automatically cast operands to compatible types)String Concatenation:CONCAT(string, string), which accepts two or more arguments.(Automatically casts values into types which can be concatenated)8 Auto Increment Field Definition:tablename_id INT IDENTITY PRIMARY KEYAuto Increment Field Definition:tablename_id INTEGER AUTO_INCREMENT PRIMARY KEY9 Get a List of Tables:SP_TABLESGet a List of Tables:SHOW TABLES10 Get Table Properties:HELP tablenameGet Table Properties:DESCRIBE tablename11 Get Database Version:SELECT @@VERSIONGet Database Version:SELECT VERSION()12 Recordset Paging:Recordset paging done by client side-ADO (very involved)Recordset Paging:Add to end of SQL: "LIMIT " & ((intCurrentPage-1)*intRecsPerPage) & ", " & intRecsPerPageLIMIT: The first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).13 Get ID of Newest Inserted Record:SET NOCOUNT ON; INSERT INTO...; SELECT id=@@IDENTITY; SET NOCOUNT OFF;Get ID of Newest Inserted Record:Two step process:1. Execute your statement: objConn.Execute("INSERT INTO...")2. Set objRS = objConn.Execute("SELECT LAST_INSERT_ID() AS ID")14 Get a Random Record:SELECT TOP 1 * FROM Users ORDER BY NEWID()Get a Random Record:SELECT * FROM Users ORDER BY RAND() LIMIT 115 Generate a Unique GUID:SELECT NEWID()Generate a Unique GUID:SELECT UUID()16Licensing:SQL Server is not an open source and payment has to be made to use SQL Server.Licensing:MySQL is available for free since MySQL is an open source.17View Support:SQL Server offers indexed views which are much more powerful, performance wise.View Support:MySQL offers only updateable views.18XML Support:SQL Server supports XML.XML Support:MySQL does not support XML.19Security:SQL Server provides column level security.Security:MySQL provides only table level security.20Certiication for Security:SQL Server has C2 compliant certification. Database security is verified by third party.Certiication for Security:MySQL does not offer any certification for security.21Support for Triggers:SQL Server provides triggers.Support for Triggers:Earlier versionsof MySQL does not support triggers. Only MySQL 5.0 supports triggers.22Support for UDF:User defined functions are supported in SQL Server.Support for UDF:User defined functions are not supported in MySQL.23Support for Cursors:Cursor feature is available in SQL Server.Support for Cursors:Cursor feature is not available in MySQL.24Support for SPs and Joins:Stored procedures and full join facility is not offered in MySQL.Support for SPs and Joins:Stored procedures and full join facility are offered in SQL Server.25Support for Import/Export Functions:Import and export are extensively supported in MySQL.Support for Import/Export Functions:Import and Export functions have very limited support in MySQL.26Support for Transaction:Transaction support is extensively and fully offered in SQL Server.Support for Transaction:Transaction support is very much limited in MySQL.27Support for Replication:Replication support is extensively and fully offered in SQL Server.Support for Replication:Replication support is very much limited in MySQL.28Support for auto tuning:Auto tuning is supported in SQL Server.Support for auto tuning:Auto tuning is not supported in MySQL.29Support for job scheduling and profiling:Job scheduling and profiling are available in SQL Server.Support for job scheduling and profiling:Job scheduling and profiling are not available in MySQL.30Support for online backup and clustering:Online backup support and clustering support is extensive and complete in SQL Server.Support for online backup and clustering:Online backup support and clustering support is limited in MySQL.31Support for Log shipping and SAN:Log Shipping and Storage Area Network support is available in SQL Server.Support for Log shipping and SAN:Log Shipping and Storage Area Network support is not available in MySQL.32Support for OLAP Services, Data Reporting and Data Mining:OLAP Services, Data Reporting and Data Mining are supported in SQL Server.Support for OLAP Services, Data Reporting and Data Mining:OLAP Services, Data Reporting and Data Mining are not supported in MySQL.
-
S.No SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER OFF 1 Characters Enclosed within double quotes:is treated as IdentifierCharacters Enclosed within double quotes:is treated as Literal2 Try using Characters Enclosed within double quotes as identifier:
WorksExample: Below statement to create a table with table name “Table” succeeds.SET QUOTED_IDENTIFIER ON GOCREATE TABLE dbo.”Table”(id int,”Function” VARCHAR(20)) GO
Try using Characters Enclosed within double quotes as identifier:
FailsExample: Below statement to create a table with table name “Table” Fails.SET QUOTED_IDENTIFIER OFF GOCREATE TABLE dbo.”Table”(id int,”Function” VARCHAR(20)) GOError Message:Msg 102, Level 15, State 1,Line 1 Incorrect syntax near ‘Table’.3 Try using Characters Enclosed within double quotes as Literal:FailsExample: Below statement fails.SET QUOTED_IDENTIFIER ONGOSELECT “BIRADAR”Error Message:Msg 207, Level 16, State 1,Line 1 Invalid column name ‘UMAR’.Try using Characters Enclosed within double quotes as Literal:WorksExample: Below Statement Works.SET QUOTED_IDENTIFIER OFFGOSELECT “UMAR”4 Characters Enclosed within single quotes:is treated as LiteralExample:SET QUOTED_IDENTIFIER ONGOSELECT ‘UMAR’
Characters Enclosed within single quotes:is treated as LiteralExample:SET QUOTED_IDENTIFIER ONGOSELECT ‘UMAR’5 How to find all the objects which are created with SET QUTOED_IDENTIFIER ON/OFF:Below Statement can be used to find all the objects created with SET QUTOED_IDENTIFIER setting as ON:SELECT OBJECT_NAME (object_id) FROM sys.sql_modules WHERE uses_quoted_identifier = 1How to find all the objects which are created with SET QUTOED_IDENTIFIER ON/OFF:Below Statement can be used to find all the objects created with SET QUTOED_IDENTIFIER setting as OFF:SELECT OBJECT_NAME (object_id) FROM sys.sql_modules WHERE uses_quoted_identifier = 0
4.Difference
between DateTime and DateTime2 DataType
-
S.No DateTime DateTime2[(n)] 1 Min Value: 1753-01-01 00:00:00 Min Value: 0001-01-01 00:00:00 2 Max Value:
9999-12-31 23:59:59.997Max Value:
9999-12-31 23:59:59.99999993 Storage Size:Storage Size:6 to 8 bytesNote: Parameter n is optional and if it is not specified then fractional seconds precision is 7 digit and it can be from 0 to 7 digit.For fractional seconds precision <3 6="6" bytes="bytes" font="font" takes="takes">3>For fractional seconds precision 3 or 4 it will take 7 bytesFor fractional seconds precision >4 it will take 8 bytes4 Usage:Declare @now datetimeUsage:Declare @now datetime2(7)5 Current Date and Time function:GetDate() – It returns DB Current Date and Time of DateTime Data TypeExample: SELECT GETDATE()Result: 2011-09-16 13:23:18.767Current Date and Time function:SYSDATETIME()- It returns DB Current Date and Time of DateTime2 Data TypeExample: SELECT SYSDATETIME()Result: 2011-09-16 13:23:18.76767206 +/- days:WORKSExample: DECLARE @nowDateTime DATETIME = GETDATE()SELECT @nowDateTime + 1Result: 2011-09-17 13:44:31.247+/- days:FAILS – Need to use only DateAdd functionExample: DECLARE @nowDateTime2 DATETIME2= SYSDATETIME()SELECT @nowDateTime2+1Result: Msg 206, Level 16, State 2, Line 2Operand type clash: datetime2 is incompatible with int7 Compliance:Is not an ANSI/ISO compliantCompliance:Is an ANSI/ISO compliant
No comments:
Post a Comment