What is a Database?
Database is an organized collection of related information. In daily world we deal with lots of data. In this internet technology more and more data is getting produced these days. We have multiple database management systems available with us to manage, store and update this enormous data in convenient way. e.g. Oracle, Sybase, Microsoft SQL server management studio etc.
DBMS and SQL
DBMS (Database management server) is a collection of software tools used to manage, update, retrieve the data from the database. SQL (Structured query language is used to connect the DBMS with the database.
All queries have been executed on the Microsoft SQL Server management Studio version 17.0. SSMS is a client tool and not the server. It is rather used as a tool to connect to the database server.
Settings: Local Host
Connect: Database Engine
Use SQL authentication username and password
In Microsoft SQL Server Management Studio you will find two types of databases:
- System Database
- User created Database
-System database can’t be deleted
SQL Command Types:
- DDL (Data Definition language) – Used to define/create database object
- DML (Data manipulation language) – Used to insert values, update and delete values from the database object created by DDL commands.
- TCL (Transaction Control language) – Used to control transactions through Commit and Rollback commands
SQL DDL Commands – data definition language (Create, Alter and Drop commands)
1. Creating a database:
Database can be created either using GUI or through SQL query in SSMS.
Create statement is used for this purpose: Create [Database Object] [Database Object name]
Ex. Create Database db1 (this statement will create a database with name db1)
Whenever we create a database, two types of files are created with it: 1. .MDF file (contains actual data) 2. .LDF file (contains log file)
2. Modify a Database:
Alter statement is used to alter a sql database object.
Alter Command: Alter [Database Object] [Database Object name] Modify Col1 = Value
E.g. Alter Database db1 Modify Name = db2 (this will change the name of the database)
Renaming through stored procedure: sp_renameDB [Old database name] [New database name]
e.g. sp_renameDB db1 db2
3. Dropping a Database:
Drop statement is used to delete a database completely from the system(.mdf and .ldf files are also deleted with it)
Drop command: Drop [Database Object] [Database Object name]
e.g. Drop Database db1 (this will delete database db1)
Note- If a database is getting used by any other user, make sure that database is not getting used by any other database. Else an error will be generated
Resolve this single user thing, use this command:
Alter Database db1 set Single_USER with Rollback immediate
(Rollback immediate, rollback any commands and delete the database immediately)
SQL DML Queries : Insert, Update, delete
1. Create a Table:
Command: Create Table [table name] ([column name] [data type of column] [constraint])
e.g. Create table t1(ID int NOT NULL Primary Key, Gender nvarchar(20) NOT NULL)
This command will create a table with name t1 and 2 columns ID and Gender of INT and nvarchar datatypes respectively. nvarchar is a UNICODE data type and store 2 bytes per characters, while varchar stores 1 byte per character.
In order to store the table in a particular database use the following command:
Use [database name]
Create table command….
Primary Key – Can’t be null and must be unique. It uniquely identify each row in the table
Foreign key– It can contain null values and it references primary key present in some other values (basically the column in which it looks for a value). Foreign key is used to establish relationship between two tables. It is used to enforce database integrity.
Create a Foreign key relation –
Alter table [table name] add constraint [constrain name] foreign key(foreign key column name) references [PrimaryColumn Table Name] (primary key column)
e.g. Alter table tb1 add constraint tb1_genderid foreign key(tb1) references tb(id)
Note- Constraint name should make sense like tablename_columnName
2. Select all values of a table:
Command: Select * from [table name]
To select all tables of a database choose:
Select * from dual (dual refers data dictionary)
3. Insert values in a table :
Insert command is used to insert values in a table: Insert into [table name] (col 1, col 2, …) Values(col 1 value, col 2 value,…)
e.g. Insert into a1(id, name, gender) values(11, “ss”, “male”)
4. Adding a Default value in a column:
We can assign default values to a column rather than assigning Null values:
Alter table [table_name] add constraint constraint_name Default [default value] For [column name]
e.g. Alter table tb1 add constraint tb1_gender default 2 for gender
This command will assign default value 2 to column gender if value not explicitly defined.
5. Adding a New column into table:
Command: Alter table [table name] add [column name] [column data type] [NULL|NOT NULL] add constraint [constraint name] Constraint
Alter table tb1 add Address nvarchar Not Null add constraint tb1_address default ‘xyz’
This command will add one column Address to the table tb1 that don’t accept null value. Also, default value of ‘xyz’ will be assigned to it.
6. Dropping a Constraint:
Command: Alter table [table name] Drop Constraint [constraint name]
e.g. Alter table tb1 drop constraint tb1_gender
This will drop the constraint.
7. Delete a Table record:
To delete a table record, we use delete command:
Delete from [table name] where column1=”column value”
Note: Where clause is used to put some condition on search selection
However, you can’t delete a table record if the table is getting used by some other user. There are some cascading referential integrity constraint imposed on foreign key constraints.
8. Cascade Referential Integrity Constraint:
We can choose options if a foreign key constraint reference is getting deleted. Four options are there:
- No Action : This will simply generate an error if a record from primary key table is deleted that has some value in foreign key table.
- Cascade: This option will delete all the foreign key records that are related to primary key will be deleted
- Set NULL: This option will set the foreign key dependent value to Null.
- Set Default: This option will set the foreign key dependent value to default values provided to the column.
9. Adding a Check Constraint:
This constraint is used to enforce value checks on column. For e.g. The value in the age column>4
Command: Alter table [table name] add constraint [constraint name] check (boolean expression)
e.g. Alter table tb1 add constraint tb1_age_check check(AGE>0 AND AGE<30)
This command will only let you add age between 0 and 30 in the Age column.
Note: The check constraint returns a Boolean value based on which the value is entered in the table. It also let you insert Null values because for Null values, check constraint returns “Unknown”.
10. Identity Column:
It is a column property in SSMS.
Identity column is a column to which values are automatically assigned. There are different properties linked to this column. Please set up following values while creating identity column:
Identity Seed: A value with which the identity column value starts
Identity Increment: The value with which identity column value is incremented.
Command: Create table stu(id int identity(1,1) Primary key)
This command will create a stu table having id as a identity column. The id column here will start from 1 and incremented by 1.
10. Setting up External Values/Explicit Value to Identity Column:
To set up external value in Identity column, add the following command before inserting values in table:
Command: Set IDENTITY_INSERT [table name] ON
Insert into table name(column list) values(1,”23″..etc)
11. Setting Off External Values/Explicit Value to Identity Column:
Command: Set IDENTITY_INSERT [table name] OFF
Insert into table name(column list) values(1,”23″..etc)
Note: To reset the identity column value, use DBCC command.
12. Unique Key Constraint:
Unique key constraint is used to enforce unique values in database. There is a slight difference between primary key and unique key.
Primary key values = Unique+Not Null
Unique constraint value = Unique + values can be null
Command: Alter table table_name add constraint constraint_name unique(column name)
Create table Stu(Name varchar(20) Unique)
13. Applying a Trigger:
Firstly let’s try to understand what a trigger is. A trigger is an sql instruction/set of instructions that will will cause an action once a specific condition occurs. For example: Inserting another row table 2 when a row is entered in the table 1.
Create Trigger [trigger_name] on [table_name] for Insert/Update/Delete/Condition
14. Selecting values from table:
Select is a command used to retrieve records from a table.
- To fetch all records from a table:
Command: Select * from [table_name]
e.g. Select * from emp
2. Select specific columns from a table:
Command: Select [col_name_1], [col_name_2]… from [table_name]
e.g. Select name, age, id from Employee
3. Fetch all distinct records from a table:
Command: Select distinct [column_name] from [table_name]
e.g. Select distinct name from Employee
This command will help in fetching the distinct records from table Employee by Name column.
4. Fetch record matching a specific condition:
Where is used to apply a specific condition in the SQL command.
Command: Select * from table_name where column_name = condition value
e.g. Select name, id from employee where name=”John”
This command will fetch all the records from the table with name column value as john.
5. Fetch record not matching a specific condition (column value):
Command: Select * from table_name where col_name <> Column value
“<>” signifies as not equal to here. We can also use “!=” to compare values.
6. OR operator in SQL:
OR operator is used to specify two or more conditions together.
Command: Select * from table_name where col1=value OR col2=value
e.g. Select name, age, salary from Employee where name=”John” OR name=”Nick”
This sql command will fetch all the table records where name is either John or Nick
7. AND Operator in SQL:
AND operator is used to specify two and more conditions together.
Command: Select * from table_name where col1=value AND col2=value
e.g. Select name, age, salary from Employee where name=”John” AND age=”30″
This sql command will fetch all the table records where name is John and age is Nick
8. IN Operator in SQL:
IN operator is used to retrieve records where condition matches more than 1 value. (And you don’t want to use OR multiple times in a sql command)
Command: Select * from table_name where col_name IN(value1, value2, value3…)
e.g. Select * from Employee where age(21, 25, 30)
This command will fetch all the table record where age is either 21 or 25 or 30.
SQL supports various kind of wild card characters to facilitate data retrieval in multiple ways. Please refer the image for all sql wild card characters.
Post Views: 2,025