Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Monday 22 February 2016

sql-create-table

CREATE TABLE (Transact-SQL)


Creating a simple table means:
1. Give the table name what you wants that is uniquely identified in database.

Example:

Tm_Employee, Tm_Department, Tm_Salary etc. 

2. Table contains Rows and Column.
3.Columns contain the name of the column, data type, and any other attributes for the column.
3. Rows contains the values or data of the table column.

Here is Simple Table Example:

Below table name is [dbo].[Employee] and contains the column Employee_id, First_name, Last_name, Salary, Joinning_date and Department

The Employee_id contains the data-type int and it is identity increment column (means Employee_id value automatically increment by one),  
First_name contains the data-type nvarchar(300) 
Last_name contains the data-type nvarchar(300),  
Salary contains the data-type decimal(20,2),
Joining_Date contains data-type datetime,   
Department contains data-type nvarchar(300).  

Create Table First in SQL Server

From the Windows Start Menu --> select “Microsoft SQL Server”--> Click on “SQL Server Management Studio”.

Right Click on your created database (TestDB) -> and Click on New Query -> write the table query as below  ->  and Click Execute.


CREATE TABLE [dbo].[Employee](

      [Employee_id] [int] IDENTITY(1,1) NOT NULL,

      [First_name] [nvarchar](300) NULL,

      [Last_name] [nvarchar](300) NULL,

      [Salary] [decimal](20, 2) NULL,

      [Joining_date] [datetime] NULL,

      [Department] [nvarchar](200) NULL

) ON [PRIMARY]

 Shows Table and Column


 Insert Record 

Write the table query as below -> and select all ->  and Click Execute.
 
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('John', 'Abraham', 1000000.00, 1905-06-07, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

Show Records


select * from [dbo].[Employee]



 


 

Saturday 20 February 2016

What is SQL

SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database. This tutorial will provide you with the instruction on the basics of each of these commands as well as allow you to put them to practice using the SQL Interpreter.


sql-create-database


 SQL consists of a data definition language, data manipulation language and data control language.
 The scope of SQL Server includes data insert, query, update and delete, schema creation and modification, and data access control.

SQL Server Database contains groups of many objects like table, query, stored procedure, function, trigger etc. we can store schema and data in database. schema means declarative syntax to define fields and data types

Create a Database

From the Windows Start Menu --> select “Microsoft SQL Server”--> Click on “SQL Server Management Studio”.

 Once the Management Studio starts -> Select Server Name( that is your pc name example: abc-PC, xyz-PC) and then select Login and enter password. the database is open.

 

In left side of the database there is Object Explorer. In Object Explorer the menu contains Database, Security, Server Objects, Replication and Management.

 

 

Right click the Databases folder and select new Database. Enter a name in the “Database name” text box. For this example, well use the name “TestDB”.


Finally database shows below


SQL CREATE DATABASE Statement Through sql-query

Create Database Syntax
CREATE DATABASE TestDatabaseName;
 
Create Database Example
 
write sql query on query window and click Execute 
 
CREATE DATABASE Test_DB;
 

 


 

 

Thursday 18 February 2016

how-to-filter-for-sql-null-or-empty-string

SQL Null or Empty How to Check for Null or Empty Column in SQL Server SQL

A Not Null  in a database really means the lack of a value. It is a special “value” that you can’t compare to using the normal operators. You have to use a clause in SQL IS Null.

On the other hand, an empty string is an actual value that can be compared to in a database. You simply use two ticks together.

Create Table First in SQL Server


CREATE TABLE [dbo].[Employee](

      [Employee_id] [int] IDENTITY(1,1) NOT NULL,

      [First_name] [nvarchar](300) NULL,

      [Last_name] [nvarchar](300) NULL,

      [Salary] [decimal](20, 2) NULL,

      [Joining_date] [datetime] NULL,

      [Department] [nvarchar](200) NULL

) ON [PRIMARY]
 Insert Record 
 
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('John', 'Abraham', 1000000.00, 1905-06-07, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Michael', 'Clarke', 800000.00, 1894-06-28, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Roy', 'Thomas', 700000.00, 1894-06-27, 'Banking')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Tom', 'Jose', 600000.00, 1894-06-27, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Jerry', 'Pinto', 650000.00, 1894-06-27, 'Insurance')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Philip', 'Mathew', 750000.00, 1894-06-28, 'Services')

INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('TestName1', '123', 650000.00, 1894-06-28, 'Services')
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('TestName2', 'Lname%', 600000.00, 1894-06-27, 'Insurance')
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Yamuna', 'Singh',4000.00, NULL, NULL)
INSERT [dbo].[Employee] ([First_name], [Last_name], [Salary], [Joining_date], [Department]) VALUES ('Pankaj', 'Roy', 45000.00, NULL, '')

Show Record

select * from [dbo].[Employee]
 



In this above table, I specifically put in some Department that are both null and empty strings.

Get the NULL values using the IS NULL operator.



select * from [dbo].[Employee]
where [Department] is null

Result


Get the empty string using the empty string.


select * from [dbo].[Employee]

where [Department] = ''

Result
 

  If you want to combine them to search for the SQL null or empty string together and retrieve all of the empty strings and nulls all at once, you could do something like this.

select * from [dbo].[Employee]
where [Department] = ''
or [Department] is null

Result