Custom Search

SQL

About SQL Server........


Microsoft SQL Server is a Relational Database Management System (RDBMS) designed to run on platforms ranging from laptops to large multiprocessor servers. SQL Server is commonly used as the backend system for websites and corporate CRMs and can support thousands of concurrent users.

SQL Server comes with a number of tools to help you with your database administration and programming tasks.

SQL Server is much more robust and scalable than a desktop database management system such as Microsoft Access. Anyone who has ever tried using Access as a backend to a website will probably be familiar with the errors that were generated when too many users tried to access the database!

Although SQL Server can also be run as a desktop database system, it is most commonly used as a server database system.

Server Database Systems


Server based database systems are designed to run on a central server, so that multiple users can access the same data simultaneously. The users normally access the database through an application.

For example, a website could store all its content in a database. Whenever a visitor views an article, they are retrieving data from the database. As you know, websites aren't normally limited to just one user. So, at any given moment, a website could be serving up hundreds, or even thousands of articles to its website visitors. At the same time, other users could be updating their personal profile in the members' area, or subscribing to a newsletter, or anything else that website users do.

Generally, it's the application that provides the functionality to these visitors. It is the database that stores the data and makes it available. Having said that, SQL Server does include some useful features that can assist the application in providing its functionality.


SQL Server Management Studio (SSMS)........


SQL Server Management Studio (SSMS) is the main administration console for SQL Server.

SSMS enables you to create database objects (such as databases, tables, views etc), view the data within your database, you can configure user accounts, transfer data between databases, and more.

Here's what SQL Server Management Studio looks like when you first open it up:
Screenshot of SQL Server Management Studio
The left pane contains the Object Explorer. The Object Explorer provides navigation to databases, server objects (such as triggers), log files, and more.
The right pane allows you to write queries against the database and view the results. In this screenshot I have opened a blank query by clicking the "New Query" button. You can also bring up other windows, such as the Properties window.

Note that I have minimized the size of the window for this screenshot. Once maximized, you have much more room to play with.
You can use SQL Server Management Studio to create as many databases as you like. You can also connect to as many databases on as many servers as you like.

Most of the tasks performed with SQL Server Management Studio are initiated either from the top menu, or by right-clicking on an icon/object.
Throughout most of this tutorial, we'll be looking at the various things you can do via SQL Server Management Studio.


SQL Server - Create a Database.........


One of the first things we should look at with SQL Server/Management Studio is how to create a database. After all, most of the tasks you perform with SQL Server will evolve around one or more databases.

System Databases

If you've only just installed SQL Server, you might notice that some databases have already been created. These are system databases.
DatabaseType
masterSystem database
modelSystem database
msdbSystem database

tempdbSystem database

We will now create another database for our own use.

Creating a New Database

The following steps demonstrate how to create a database in SQL Server using SQL Server Management Studio.
  1. Right click on the "Databases" icon and select "New Database...":
    Creating a new database in SQL Server - step 1
  2. Name your database and click "OK":
  3. Creating a new database in SQL Server - step 2

Your New Database

You will now notice your new database appears under the "Databases" section of SQL Server Management Studio.

Your new database is based on the "Model" database. The Model database is a system database which is used as a template whenever a new database is created. If you use the left pane to navigate to your database and expand the tree, you will notice that your database already contains a number of objects. For example, it already contains system functions, system views, system stored procedures, and (hidden) system tables. These are system objects which provide information about the database.

Creating a new database in SQL Server - step 3

Other Options

We have just created a database using the default options. When we created the database, a "Data File" and a "Transaction Log" were created. They were created in the default location for our server.

If we'd wanted to, we could have specified a different location for these files. We also could have changed specifications on whether to allow the file to grow automatically (as it stores more and more data), and if so, how that growth should be managed. We could have done that at step 2. But all is not lost. We can still do it now that we've created the database. We can do it via the Properties dialog box.

To view or change the database properties, simply right click on the database and select "Properties":

Creating a new database in SQL Server - step 4

The Properties dialog contains a large number of options for changing the configuration of your database. For now, we can leave everything at its default setting.

Creating a new database in SQL Server - step 5



The Basic Queries of Sql are............


SQL StatementSyntax
AND / ORSELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLEALTER TABLE table_name
ADD column_name datatypeor
ALTER TABLE table_name
DROP COLUMN column_name
AS (alias)SELECT column_name AS column_alias
FROM table_nameor
SELECT column_name
FROM table_name  AS table_alias
BETWEENSELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASECREATE DATABASE database_name
CREATE TABLECREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
CREATE INDEXCREATE INDEX index_name
ON table_name (column_name)or
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
CREATE VIEWCREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
DELETEDELETE FROM table_name
WHERE some_column=some_valueor
DELETE FROM table_name
(Note: Deletes the entire table!!)
DELETE * FROM table_name
(Note: Deletes the entire table!!)
DROP DATABASEDROP DATABASE database_name
DROP INDEXDROP INDEX table_name.index_name (SQL Server)
DROP INDEX index_name ON table_name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name
DROP INDEX index_name (MySQL)
DROP TABLEDROP TABLE table_name
GROUP BYSELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVINGSELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
INSELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTOINSERT INTO table_name
VALUES (value1, value2, value3,....)or
INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)
INNER JOINSELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LEFT JOINSELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
RIGHT JOINSELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
FULL JOINSELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LIKESELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
ORDER BYSELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
SELECTSELECT column_name(s)
FROM table_name
SELECT *SELECT *
FROM table_name
SELECT DISTINCTSELECT DISTINCT column_name(s)
FROM table_name
SELECT INTOSELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_nameor
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name
SELECT TOPSELECT TOP number|percent column_name(s)
FROM table_name
TRUNCATE TABLETRUNCATE TABLE table_name
UNIONSELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
UNION ALLSELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UPDATEUPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value
WHERESELECT column_name(s)
FROM table_name
WHERE column_name operator value

SQL Server - User Logins........


SQL Server allows for the creation of user logins. Each individual who needs access to SQL Server can be given their own user account.

When the administrator configures these user logins, he/she can assign them to any number of roles and schemas, depending on the access that the individual is entitled to.

In this lesson, we will walk through the steps in creating a user login.

To Create a New User Login

  1. Using SQL Server Management Studio, expand the "Security" option and right click on "Logins"
  2. Click on "New Login"
    Creating a new login in SQL Server
  3. Complete the login properties in the "General" tab by providing a name for the login, choosing the Authentication method (providing a password if you choose "SQL Server authentication"), and selecting the database to use as a default. If you don't choose a language, it will use the default for the current installation of SQL Server.

  4. If you get an error that reads "The MUST_CHANGE option is not supported by this version of Microsoft Windows", simply uncheck the "User must change password at next login" option. The error occurs because your operating system doesn't support this option.

    Creating a new login in SQL Server - General tab
  5. Click the "Server Roles" tab if you need to apply any server-wide security privileges.
    Creating a new login in SQL Server - Server Roles tab
  6. Click the "User Mapping" tab to specify which databases this user account is allowed to access. By default, the login will be assigned to the "Public" role, which provides the login with basic access. If the login needs more access in one or more databases, it can be assigned to another role with greater privileges.Note that these roles are "Database Roles" and are different to the server roles in the previous tab. Server roles are for administering the SQL Server. Database roles are created within each database and specify what the login can do within that database.
    Creating a new login in SQL Server - User Mapping tab

Read more...

Back to TOP