Professional Documents
Culture Documents
P a g e 1 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 2 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 3 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
All those have the same basic implementation and understand the
SQL structure. So most of code we will be learning in this course,
work on any database management system.
Type of Relationship
We have:
1. One to One
2. One to Many
3. Many to Many
P a g e 5 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
In Comparison
P a g e 6 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 7 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Normalization
Normalization is database design technique which organizes tables in a
manner that reduces redundancy and dependency of data.
We have three normalizations:
▪ First normalization
▪ Second normalization
▪ Third normalization
1. First normalization form (1NF)
The first normalization is:
-Each table cell should contain a single value.
-Each record need to be unique
2. Second normalization form(2NF)
-Rule 1 in 1NF, they are the same.
-Rule 2, single column primary key
3. Third normalization form(3NF)
-Rule 1 be in 2NF
-Rule 2 has no transitive functional dependencies.
Database schema (Blue print=schema)
Database schema is the skeleton structure that we represent the local view of
the entire Database. It defines how database is organized and how the
relations among them are associated.
P a g e 8 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Data Modeler/Diagrams
Defines the conceptual view of a database.
It works around the Real world entity; data modeler is considered as a good
option for designing Database.
Entity: can be real world object, either animate that can be easily identifiable.
Ex: Students, Teachers.
Attributes: are the entities representation. Ex: student name, age, level
❖ Types of attributes
-Simple attribute. Ex: Student phone number
-composite attribute. Ex: student complete name.
-Derived attributes. Ex: age can be derived date of birth
-Simple value attribute. Ex: Student ID
-Multiple value attributes. Ex: a person can have more than one phone
number.
Introduction to SQL
SQL is a standard language for accessing and manipulating databases.
What is SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute) standard
P a g e 9 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Although SQL is an ANSI (American National Standards Institute) standard,
there are different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the
major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a
similar manner.
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
SQL Syntax
In this tutorial we will teach you all about the different SQL statements.
In this tutorial, we will use semicolon at the end of each SQL statement.
P a g e 10 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SQL Commands
Are the instruction, it use to communicate with the database, it also use to
perform specific task.
3. DCL: Data Control Language. It is used to grant take back authority from
any database user.
here are some commands that come under DCL
. Grant (is used to give data access).
. Revoke (is used to take back the permission from the user).
4. DQL: Data Query Language.
- DQL is used to fetch the data from the database.
It uses only the command SELECT
Select * from Employee;
This statement will display all the employee’s information in the
table Employee
SQL developers have to decide what types of data will be stored inside each and
every table column when creating a SQL table. The data type is a label and a
guideline for SQL to understand what type of data is expected inside of each
column, and it also identifies how SQL will interact with the stored data.
char(n) Fixed width character string. Maximum 8,000 characters Defined width
varchar(n) Variable width character string. Maximum 8,000 characters 2 bytes + number
of chars
text Variable width character string. Maximum 2GB of text data 4 bytes + number
of chars
nchar Fixed width Unicode string. Maximum 4,000 characters Defined width x 2
P a g e 12 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
characters
Tables are organized into rows and columns; and each table must have a name.
P a g e 13 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
column_name2 data_type(size),
column_name3 data_type(size),
....
);
The column_name parameters specify the names of the columns of the table.
The data_type parameter specifies what type of data the column can hold (e.g.
varchar, integer, decimal, date, etc.).
The size parameter specifies the maximum length of the column of the table.
Example
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
MySQL\SQL\:
)
or
CREATE TABLE Persons
(
P_Id int NOT NULL
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
Constraint P_ID PRIMARY KEY (P_Id),
)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY
constraint on multiple columns, use the following SQL syntax:
Note: If you use the ALTER TABLE statement to add a primary key, the primary
key column(s) must already have been declared to not contain NULL values
(when the table was first created).
MySQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders"
table is already created, use the following SQL:
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
MySQL:
The first form does not specify the column names where the data will be
inserted, only their values:
The second form specifies both the column names and the values to be
inserted:
P a g e 17 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SQL SELECT Syntax
SELECT column_name,column_name
FROM table_name;
and
If you define a CHECK constraint on a single column it allows only certain values
for this column.
If you define a CHECK constraint on a table, it can limit the values in certain
columns based on values in other columns in the row.
SQL Server:
P a g e 18 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
City varchar(255)
)
SQL Server:
P a g e 19 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
ALTER TABLE Persons
DROP CHECK chk_Person
The default value will be added to all new records, if no other value is specified.
The DEFAULT constraint can also be used to insert system values, by using
functions like GETDATE ():
MySQL
SQL Server:
P a g e 20 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
ALTER TABLE Passengers
ADD CONSTRAINT df_dest
DEFAULT 'Dabola' FOR Destination;
MS Access:
Oracle:
MySQL:
Indexes
An index can be created in a table to find data more quickly and efficiently.
Note: The syntax for creating indexes varies amongst different databases.
Therefore: Check the syntax for creating indexes in your database.
If you want to create an index on a combination of columns, you can list the
column names within the parentheses, separated by commas:
Displaying an INDEX
Select * from Table_Name WITH(Index(Index_Name));
Ex:
select * from saler with(index( SalerIndex))
Droping an INDEX
Drop Index IndexName on Table_Name
drop index SalerIndex on saler;
The SQL DELETE Statement
The DELETE statement is used to delete records in a table.
P a g e 22 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
Note: Be very careful when deleting records. You cannot undo this
statement!
P a g e 23 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
The WHERE clause specifies which record or records that should be
updated. If you omit the WHERE clause, all records will be updated.
Example
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
The selection from the "Customers" table will now look like this:
Update Warning!
Be careful when updating records. If we had omitted the WHERE clause, in
the example above, like this:
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg';
P a g e 24 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Example
SELECT * FROM Customers
WHERE CustomerID=1;
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be
written as !=
P a g e 25 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Example
SELECT * FROM Customers
WHERE City LIKE 's%';
Tip: The "%" sign is used to define wildcards (missing letters) both before and
after the pattern. You will learn more about wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the
letter "s":
P a g e 26 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Example
SELECT * FROM Customers
WHERE City LIKE '%s';
The following SQL statement selects all customers with a Country containing the
pattern "land":
Example
SELECT * FROM Customers
WHERE Country LIKE '%land%';
Using the NOT keyword allows you to select records that do NOT match the
pattern.
The following SQL statement selects all customers with Country NOT containing
the pattern "land":
Example
SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
P a g e 27 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
To display the products outside the range of the previous example, use NOT
BETWEEN:
Example
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
Example
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';
Example
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'C' AND 'M';
Example
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
P a g e 28 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Notice that the BETWEEN operator can produce different result in
different databases!
In some databases, BETWEEN selects fields that are between and excluding the
test values.
In other databases, BETWEEN selects fields that are between and including the
test values.
And in other databases, BETWEEN selects fields between the test values,
including the first test value and excluding the last test value.
SQL VIEW
Create view View_Name as select column1, column2, column3 etc. from Table_Name;
P a g e 29 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Syntax for MySQL
To let the AUTO_INCREMENT sequence start with another value, use the
following SQL statement:
To insert a new record into the "Persons" table, we will NOT have to specify a
value for the "ID" column (a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table.
The "ID" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
In the example above, the starting value for IDENTITY is 1, and it will increment
by 1 for each new record.
Tip: To specify that the "ID" column should start at value 10 and increment by
5, change it to IDENTITY (10,5).
To insert a new record into the "Persons" table, we will NOT have to specify a
value for the "ID" column (a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table.
The "ID" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
Tip: To specify that the "ID" column should start at value 10 and increment by
5, change the auto increment to AUTOINCREMENT (10,5).
P a g e 31 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
To insert a new record into the "Persons" table, we will NOT have to specify a
value for the "ID" column (a unique value will be added automatically):
The SQL statement above would insert a new record into the "Persons" table.
The "P_Id" column would be assigned a unique value. The "FirstName" column
would be set to "Lars" and the "LastName" column would be set to "Monsen".
You will have to create an auto-increment field with the sequence object (this
object generates a number sequence).
The code above creates a sequence object called seq_person, that starts with 1
and will increment by 1. It will also cache up to 10 values for performance. The
cache option specifies how many sequence values will be stored in memory for
faster access.
To insert a new record into the "Persons" table, we will have to use the nextval
function (this function retrieves the next value from seq_person sequence):
The SQL statement above would insert a new record into the "Persons" table.
The "ID" column would be assigned the next number from the seq_person
sequence. The "FirstName" column would be set to "Lars" and the "LastName"
column would be set to "Monsen".
P a g e 32 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
To change the data type of a column in a table, use the following syntax:
Notice that the new column, "DateOfBirth", is of type date and is going to hold a
date. The data type specifies what type of data the column can hold.
P a g e 33 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Now we want to change the data type of the column named "DateOfBirth" in the
"Persons" table.
Notice that the "DateOfBirth" column is now of type year and is going to hold a
year in a two-digit or four-digit format.
SQL Joins
An SQL JOIN clause is used to combine rows from two or more tables, based on
a common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL
INNER JOIN returns all rows from multiple tables where the join condition is
met.
Notice that the "CustomerID" column in the "Orders" table refers to the
"CustomerID" in the "Customers" table. The relationship between the two tables
above is the "CustomerID" column.
P a g e 34 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Then, if we run the following SQL statement (that contains an INNER JOIN):
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Demo:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The INNER JOIN keyword selects all rows from both tables as long as
there is a match between the columns. If there are rows in the "Customers"
table that do not have matches in "Orders", these customers will NOT be listed.
or:
SELECT column_name(s)
FROM table1
P a g e 36 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The LEFT JOIN keyword returns all the rows from the left table
(Customers), even if there are no matches in the right table (Orders).
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
In this tutorial we will use the well-known Northwind sample database.
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders
P a g e 38 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The RIGHT JOIN keyword returns all the rows from the right table
(Employees), even if there are no matches in the left table (Orders).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT
joins.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
P a g e 39 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The FULL OUTER JOIN keyword returns all the rows from the left table
(Customers), and all the rows from the right table (Orders). If there are rows in
"Customers" that do not have matches in "Orders", or if there are rows in
"Orders" that do not have matches in "Customers", those rows will be listed as
well.
Notice that each SELECT statement within the UNION must have the same
number of columns. The columns must also have similar data types. Also, the
columns in each SELECT statement must be in the same order.
Note: The UNION operator selects only distinct values by default. To allow
duplicate values, use the ALL keyword with UNION.
PS: The column names in the result-set of a UNION are usually equal to the
column names in the first SELECT statement in the UNION.
Demo Database
In this tutorial we will use the well-known Northwind sample database.
P a g e 40 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
CustomerID CustomerName City ContactName Country
1 Aliou Balde Conakry Maria Anders Germany
2 Moussa Camara Dakar Ana Mexico
3 Kaba Abou Accra Antonio Mexico
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Note: UNION cannot be used to list ALL cities from the two tables. If several
customers and suppliers share the same city, each city will only be listed once.
UNION selects only distinct values. Use UNION ALL to also select duplicate
values!
Example
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
P a g e 41 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
SQL UNION ALL with WHERE
The following SQL statement uses UNION ALL to select all (duplicate values
also) German cities from the "Customers" and "Suppliers" tables:
Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
SQL Functions
SQL has many built-in functions for performing calculations on data.
Creating individual logins and users for SQL gives you a great security
advantage. A login is used for authentication into a SQL instance while a
User is used for authorization into a SQL Database. Note that Logins are
used at the instance level and users are used at the database level.
Here is how to create a new login and User in SQL server.
What is difference between SQL and T SQL?
SQL and T-SQL are the query languages used to manipulate the database
and are an important part of the DBMS. The prior difference
between the SQL and T-SQL is that the SQL is non-procedural while T-
SQL is a procedural language. T-SQL is an extension to SQL, developed
by Sybase and Microsoft.
P a g e 44 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
How to Create a Login?
To create a login, Navigate to Security > Logins
1. Login Name
2. Select SQL Server authentication
3. Enter Password
P a g e 45 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
4. Click Ok
Login is created
P a g e 46 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
CREATE LOGIN MyLogin WITH PASSWORD = '123';
• Using T-SQL
• Using SQL Server Management Studio
1. Connect to SQL Server then expand the Databases folder from the Object
Explorer.
2. Identify the database for which you need to create the user and expand it.
3. Expand its Security folder.
4. Right-click the Users folder then choose "New User…"
P a g e 47 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
User is created
• Using T-SQL
• Using SQL Server Management Studio
P a g e 49 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 50 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Step 4)
use <database-name>
grant <permission-name> on <object-name> to <username\principle>
For example, the following command shows how you can grant the select
permission to the user Guru99 on the object (table) named Course within the
Database EDU_TSQL:
USE EDU_TSQL
GO
Grant select on Course to Guru99
The permission will be granted!
P a g e 52 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Logins and Users are vital for SQL server to work and now you know
how to create them.
If you want to connect to your User Account, you need to open the
Server Manage.
Other times, we may only see “Microsoft SQL Server Error 18456” along with the
severity and state number. On its own, a state number might not mean much, yet it can
offer more details as to what is wrong and where to look next.
These states of the error, 18456, are the most common. The descriptions and potential
solutions offer a quick explanation and potential troubleshooting guide.
P a g e 53 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 54 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
When you start SSMS, you will be asked to log in to the server. By default, most
MSSQL servers have Windows Authentication enabled, meaning you must log in with
the Windows Administrator or the account specified as the SQL Administrator when
MSSQL was installed and configured.
In SSMS, right-click the Server Name at the top of the Object Explorer window and
choose Properties.
P a g e 55 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
If you find Windows Authentication is the only mode configured, this is the likely
cause of Error 18456, Login failed for user ‘<username>’.
Setting the Server authentication mode to allow SQL Server and Windows
Authentication, you will be able to login to MS-SQL with a SQL user and password or
a Windows user and password. After making this change, you will need to restart the
SQL Server service.
In the above example, Windows Authentication mode was the only mode configured,
and the Error 18456 occurred because the user ‘sa’ is a SQL user and SQL Server
Authentication was not permitted.
In SSMS Object Explorer, expand Security, Logins. Locate the user that was failing
to log in. A red x on the user indicates this user has login disabled.
P a g e 56 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
To allow the user to login, right-click the user and choose Properties, then click
the Status page. Enabling login for the user and click OK.
After refreshing the list user logins, we can confirm the user no longer has a red x
present. This should allow the user to log in. In this example, the SQL user ‘sa’ failed to
log in because there was no permission to log in.
Continuing with user troubleshooting, right-click the user and choose Properties, then
click the General page. Here you can enter a new password and then enter
P a g e 57 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
the confirmation password. Click OK to save the new password. We set a new
password for the user so that we are certain of the password when we attempt to log in.
P a g e 58 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
In this troubleshooting article, we learned how to identify specifics of Error 18456 to help
us track down the root cause of the issue. Still looking for support? Our MSSQL
database solutions come with assistance from our technical support team. Find out how
our high-availability database can work for you!
P a g e 59 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Once you start creating a backup task with SQL Server Management Studio or SSMS,
there is also an option to generate the corresponding T-SQL backup with the help of
Script button. Also, select the Script destination for process completion.
Backup a database
P a g e 60 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
3. Right Click on the database, go to Tasks and click on Backup to open Backup
Database dialog box.
P a g e 61 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
There are different options for relational database backup, and all involve exclusive
SSMS steps. Refer to the site for the list of the available set of SSMS commands and
restore the database from a. back file.
▪ Right Click on the Database, select Task -> Restore -> Database
P a g e 62 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 63 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 64 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
▪ Select the .BAK file and click OK
▪ Click OK
P a g e 65 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 66 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
If you don't want to overwrite the existing database then, you could create a new
database and in that case, you would have to move the physical file to the new location.
P a g e 67 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
The most important part of Restore operation is Recovery State:
If you are doing differential restore after full restore, If you are doing log restore after full
restore you cannot leave your database in recovered state, you leave it in non-
recovered state ready for more backups to be applied.
I am selecting the first option "Leave the database ready to use by rolling back
uncommitted transactions. Additional transaction log cannot be restored. (RESTORE
WITH RECOVERY)"
However, the long list of commands may not be a feasible option for any SQL
Administrator to perform at the time of crisis. Today, downtime in any form is not
acceptable. The process of restore database in SQL server 2014 from .BAK file, using
T-SQL or SSMS Commands is lengthy and time-consuming. Downtime to restore the
database from the .bak file is inevitable, but you can reduce the duration with the help of
a specialized SQL database recovery software. Such software requires selection of the
database to scan, repair, and finally save it at a preferred location.
The BACKUP DATABASE command gives you many options for creating
backups. Following are different examples.
P a g e 69 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
Create a full SQL Server backup with a password
This command creates a backup with a password that will need to be supplied when
restoring the database.
▪ Right Click on the Database, select Task -> Restore -> Database
P a g e 70 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
▪ You can choose the database to restore, or you can create a new database
during restore process. I selected the From device option
P a g e 71 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
▪ Specify the backup
P a g e 72 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
▪ Click OK
P a g e 73 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 74 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
P a g e 75 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
If you don't want to overwrite the existing database then, you could create a new
database and in that case, you would have to move the physical file to the new
location.
If you are doing differential restore after full restore, If you are doing log restore
after full restore you cannot leave your database in recovered state, you leave it in
non-recovered state ready for more backups to be applied.
P a g e 76 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
I am selecting the first option "Leave the database ready to use by rolling back
uncommitted transactions. Additional transaction log cannot be restored. (RESTORE
WITH RECOVERY)"
However, the long list of commands may not be a feasible option for any SQL
Administrator to perform at the time of crisis. Today, downtime in any form is not
acceptable. The process of restore database in SQL server 2014 from .BAK file, using
T-SQL or SSMS Commands is lengthy and time-consuming. Downtime to restore the
database from the .bak file is inevitable, but you can reduce the duration with the
help of a specialized SQL database recovery software. Such software requires
selection of the database to scan, repair, and finally save it at a preferred location.
You can create a database by restoring a database you had backed up earlier. It
can be done by running the restore database command which takes the following
syntax:
Restore Database <database name>
P a g e 77 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877
ADEPTS INSTITUTE
From Disk = ‘<Backup file location + filename>’;
The query should be executed within the query window just like out previous
command.
For example:
Restore database Guinea
From Disk = ‘C:\Backup\Guinea_Backup.bak’
P a g e 78 | 78
Powered by Mr. Kaba Abou/Contact: +233 235 906 915/WhatsApp: +224 628 986 877