What is PRIMARY KEY?
A PRIMARY KEY constraint is a unique identifier for a row within a
database table. Every table should have a primary key constraint to uniquely
identify each row and only one primary key constraint can be created for each
table. The primary key constraints are used to enforce entity integrity.
What is UNIQUE KEY constraint?
A UNIQUE constraint enforces the uniqueness of the values in a set of
columns, so no duplicate values are entered. The unique key constraints are
used to enforce entity integrity as the primary key constraints.
What is FOREIGN KEY?
A FOREIGN KEY constraint prevents any actions that would destroy links
between tables with the corresponding data values. A foreign key in one table
points to a primary key in another table. Foreign keys prevent actions that
would leave rows with foreign key values when there are no primary keys with that
value. The foreign key constraints are used to enforce referential integrity.
What is CHECK Constraint?
A CHECK constraint is used to limit the values that can be placed in a
column. The check constraints are used to enforce domain integrity.
What is NOT NULL Constraint?
A NOT NULL constraint enforces that the column will not accept null
values. The not null constraints are used to enforce domain integrity, as the
check constraints.
How to get @@ERROR and @@ROWCOUNT at
the same time?
If @@Rowcount is checked after Error checking statement then it will
have 0 as the value of @@Recordcount as it would have been reset. And if
@@Recordcount is checked before the error-checking statement then @@Error would
get reset. To get @@error and @@rowcount at the same time do both in same
statement and store them in local variable. SELECT @RC = @@ROWCOUNT, @ER =
@@ERROR
What is a Scheduled Jobs or What is a
Scheduled Tasks?
Scheduled tasks let user automate processes that run on regular or
predictable cycles. User can schedule administrative tasks, such as cube
processing, to run during times of slow business activity. User can also
determine the order in which tasks run by creating job steps within a SQL
Server Agent job. E.g. back up database, Update Stats of Tables. Job steps give
user control over flow of execution. If one job fails, user can configure SQL
Server Agent to continue to run the remaining tasks or to stop execution.
What are the advantages of using
Stored Procedures?
- Stored
procedure can reduced network traffic and latency, boosting application
performance.
- Stored
procedure execution plans can be reused, staying cached in SQL Server’s
memory, reducing server overhead.
- Stored
procedures help promote code reuse.
- Stored
procedures can encapsulate logic. You can change stored procedure code
without affecting clients.
- Stored
procedures provide better security to your data.
What is a table called, if it has
neither Cluster nor Non-cluster Index? What is it used for?
Unindexed table or Heap. Microsoft Press Books and Book on Line (BOL)
refers it as Heap. A heap is a table that does not have a clustered index and,
therefore, the pages are not linked by pointers. The IAM pages are the only
structures that link the pages in a table together. Unindexed tables are good
for fast storing of data. Many times it is better to drop all indexes from
table and then do bulk of inserts and to restore those indexes after that.
Can SQL Servers linked to other
servers like Oracle?
SQL Server can be linked to any server provided it has OLE-DB provider
from Microsoft to allow a link. E.g. Oracle has an OLE-DB provider for oracle
that Microsoft provides to add it as linked server to SQL Server group
What is BCP? When does it used?
BulkCopy is a tool used to copy huge amount of data from tables and
views. BCP does not copy the structures same as source to destination. BULK
INSERT command helps to import a data file into a database table or view in a
user-specified format.
What command do we use to rename a
db, a table and a column?
To rename db
sp_renamedb 'oldname' , 'newname'
If someone is using db it will not accept sp_renmaedb. In that case
first bring db to single user using sp_dboptions. Use sp_renamedb to rename
database. Use sp_dboptions to bring database to multi user mode.
E.g.
USE master;
GO
EXEC sp_dboption AdventureWorks, 'Single User', True
GO
EXEC sp_renamedb 'AdventureWorks', 'AdventureWorks_New'
GO
EXEC sp_dboption AdventureWorks, 'Single User', False
GO
To rename Table
We can change the table name using sp_rename as follows,
sp_rename 'oldTableName'
'newTableName'
E.g.
sp_RENAME 'Table_First', 'Table_Last'
GO
GO
To rename Column
The script for renaming any column :
sp_rename 'TableName.[OldcolumnName]', 'NewColumnName', 'Column'
E.g.
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
GO
E.g.
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
GO
What are sp_configure commands and
set commands?
Use sp_configure to display or change server-level settings. To change
database-level settings, use ALTER DATABASE. To change settings that affect
only the current user session, use the SET statement.
E.g.
sp_CONFIGURE 'show
advanced', 0
GO
RECONFIGURE
GO
sp_CONFIGURE
GO
RECONFIGURE
GO
sp_CONFIGURE
GO
You can run following command and check advance global configuration
settings.
sp_CONFIGURE 'show
advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE
GO
RECONFIGURE
GO
sp_CONFIGURE
GO
How to implement one-to-one,
one-to-many and many-to-many relationships while designing tables?
One-to-One relationship can be implemented as a single table and rarely
as two tables with primary and foreign key relationships. One-to-Many
relationships are implemented by splitting the data into two tables with
primary key and foreign key relationships.
Many-to-Many relationships are implemented using a junction table with
the keys from both the tables forming the composite primary key of the junction
table.
What is an execution plan? When would
you use it? How would you view the execution plan?
An execution plan is basically a road map that graphically or textually
shows the data retrieval methods chosen by the SQL Server query optimizer for a
stored procedure or ad-hoc query and is a very useful tool for a developer to
understand the performance characteristics of a query or stored procedure since
the plan is the one that SQL Server will place in its cache and use to execute
the stored procedure or query. From within Query Analyzer is an option called
“Show Execution Plan” (located on the Query drop-down menu). If this option is
turned on it will display query execution plan in separate window when query is
ran again.
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos
0 comments
Post a Comment