Define unique key in SQL server
How to define unique key in table? Is it possible to define one primary key and one unique key in a single table?
Explain self join with the help of an example?
Self Join=It is being defined as a table joins to itself. Example as below, Imagine this Table.. E_ID E_NAME M_ID 111 SUNIL NULL 112 RAKESH 111 113 NEHA 112 114 JUHI 113 ...
joining a table to itself using alias.
select * from table_name t1
inner inner join table_name t2
on t1.x=t2.y
In which scenario I can use the unique key and primary key in SQL server . i mean in real life database how can I judge where I have to use the unique key and primary key.
primary key=not allow null values,create cluster index by default.
unique key=allow only one null values and by default it create non cluster index
A Unique ID and Primary Key Constrain perform the ability to allow unique data in the column but only the Unique constrain allows NULL values in the field along with the unique data. Primary Key constrain fields allow Unique and Not NULL values.
What is query of display the all tables in SQL server ?
Answered by: santanukd
Member Since May-2008 | Answered On : May 14th, 2009
Just replace "[your-schema-name-here]" in the following query with your schema name. Hope it comes handy to some of you out there.
Select OBJECT_TYPE,OBJECT_NAME
FROM
(
Select 'TABLE' as OBJECT_TYPE, TABLE_NAME as OBJECT_NAME, TABLE_SCHEMA as OBJECT_SCHEMA from information_schema.VIEWS
Union
Select 'VIEW' as OBJECT_TYPE, TABLE_NAME as OBJECT_NAME, TABLE_SCHEMA as OBJECT_SCHEMA from information_schema.VIEWS
Union
Select 'INDEX[Type:Name:Table]' as OBJECT_TYPE, concat(CONSTRAINT_TYPE,' : ',CONSTRAINT_NAME,' : ',TABLE_NAME) as OBJECT_NAME,TABLE_SCHEMA AS OBJECT_SCHEMA from information_schema.TABLE_CONSTRAINTS
Union
Select 'Procedure/Functions' as OBJECT_TYPE, ROUTINE_NAME as OBJECT_NAME, ROUTINE_SCHEMA as OBJECT_SCHEMA from information_schema.ROUTINES
) R
Where R.OBJECT_SCHEMA=[your-schema-name-here]
select * from sys.tables where type = U
select * from sys.tables
What is a key in SQL?
Candidate Key, Alternate Key, Composite Key are also the parts of SQL Server: A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary ke...
We have different types of keys in sql:primary key identify record uniquely max one per table do not contain null values &...
What is fill factor?What should be the ideal value of fill factor for any index?Justify the answer.
The fill factor simply says how large or how full to get our index pages
you can set it as 70% ,
If set to 100% it might me a bad thing if we had a lot of DML activity but if this is a read only table then
fill factor=100% is fine.
What is the difference between subqueries and joins? Which is better and why? Give an example of each.
Its true, I would like to add some more information about it, Subquery take separate temp table and checking condition ………….. Besides join is checks conditions first and then put it into tabl...
I am software engineer and iam working on oracle mysql and sql so i have lot of experience on this ,basically joins are using there should be connection two or more than two tables ,joins are always o...
select DATEDIFF(MI,FromTime,ToTime) From ShiftMaster -- between no of minutes
select starttime,stoptime,datediff(day,starttime,stoptime) as newtime from date.try this...........
What are the advantages of SQL server 2005 over SQL server 2000/ SQL server 7.0 ?
SQL Server 2005 has reduced application downtime, increased scalability and performance, and tight yet flexible security controls. SQL Server 2005 makes it simpler and easier to deploy, manage, and op...
sql server 2005 is exclusively designed for dot net developers. what ever the function or procedure that we write in dot net environment those function or procedure that we direcly exucute in sql server environment. this is the new feature in sql 2005.
What is a co-related query in databases?
CO-RELATED Query: It used to find the exits or doesnot exits value. It has two select statement, the outer select statement will execute first and then the inner select statement execute.There is no need of relation between the two select statement.
the sub query is executed more than one time i.e,the inner query depends on outer query output is called co-related query.
How to delete records from two tables. There is relationship between two tables.
It's working in MySQL but not in SQL-server.
For deleting the records from the relational tables there are 4 options available. These options can be used when you create the tables then you have to use these options and create tables and ...
Use a procedure which will delete record from child table first and then it will delete the same record from parent table
Same thing can be achieved using cascade delete and cascade update, this feature was not there in sql server 7.0
How to find out the no of connections in SQL server?
sp_who2
Use SP_WHO2
How can we rename a database with query?
sp_rename 'old_db_name','new_db_name';
Syntax of renaming the database:
sp_renamedb old_database name new_database name
Eg: name of the database is tree. Now if you need to change the database as three. So the query for changing the database as follows:
sp_renamedb tree three
What is the effect of 'delete' on primary key and foreign key
when you have to delete the records on primary key and foreigh key then there is problem will hapen as primary key violation
at that time you have to provide delete rules when you create the tables
those are
1. on delete no action
2. on delete cascade
3. on delete set null
4. on delete set default
For primary key if that record is being refered by some other (Child Table) sql server won't let you to delete that record.While creating relationship if you had mention "Dele...
Clustered & non clustered indexes
What is difference between clustered & non clustered indexes?
In general you put a clustered index on the key columns that you will be querying on and you can put a non clustered index on other key columns in the select statement. But you need to really look at ...
The index in the sql server are in the form of btree having root and leaf nodes.The root node contains the column key value contained in the index and the leaf node contains the table data or pointer ...
How do I lock a column, once records are entered into the table they can not be edited. Also how can I lock the field where only certain values are accepted?
For this purpose, Views are there in SQL Server.
You can Create a view by leaving the column you need to restrict
User can edit all the fields in the view except the one you restrict .
Binita.
May be you can use Constraints.
What are the steps to configure db mirroring?
Database mirroring maintains two copies of a single database that must reside on different server instances of SQL Server Database Engine.
Database mirroring is a primarily software solution for increasing database availability.
How to insert multiple rows in a single insert query?
Store images in SQL server 2005
How to store images in SQL server 2005?
You can store images by BLOB (Binary Large object). Conversion should take place in front-end or SQL supports image as datatype. Try it.
Stored procedure performance tuning
How can we improve stored procedure performance? Explain the performance tuning techniques.
Use the following steps:1) Dont use temporary tables instead use CTE or table Variable2) Use Inner Join instead of Sub Queries.3) Check indexs in tables used4) Instead of Select * from use select colu...
To expect improvemnt in stored procedure performance, pl keep following points in mind :-1. Please don't use many subqueries.2. Don't use cursor, instead of it, use temp. table in the looping....
SQL UNIQUE key... The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of col...
If a column allows the duplication once is known as a unique key.i.e; Unique Key allows duplication once.But, Primary Key will not allow duplication. "mysql CREATE TABLE Employee (...