Professional Documents
Culture Documents
Create Database;
eg:-
Note: When you increase the width of a column, you won’t face any
problem but while
decreasing the width if the table contains data in it we cannot
decrease the
width less than the max existing characters in the column.
Note: * The truncate command will delete data rows but not the structure of
the table.
* delete all the records or rows from a table without any
condition (No Where clause).
# ***What are the differences between Delete and Truncate Command in SQL Server?***
#Delete
* It is a DML command.
* By using the delete command we can delete a specific record from the table.
* Delete supports WHERE clause.
* It is a temporary deletion (It means a row containing 8 rows if we delete
it next row will be from 9 because it support rollback)
* Delete supports rollback transactions for restoring the deleted data.
* Delete command will not reset identity property.
#Truncate
* It is a DDL command
* But it is not possible with truncate command.
* It is a permanent deletion. (It means a row containing 8 rows if we delete
it. the next row will be from 1 because it does not support rollback)
* Truncate does not support the WHERE clause.
* Truncate doesn’t support rollback transaction so that we cannot restore the
deleted information.
But it will reset the identity property.
4. SQL Server Date and Time data types: (Date, Time, DateTime)
* Date - This data type will accept date format information only. The
default format of the date data type is ‘YYYY/MM/DD’.
* Time - It allows time format information only. The default
format of the time data type is ‘hh:mm:ss.ms’.
* DateTime - It allows date and time format information. The default
format of DateTime data type is ‘YYYY/MM/DD hh:mm: ss.ms’.
SQL Server supports six types of constraints for maintaining data integrity.
They are as follows
1. Default Constraint
2. UNIQUE KEY constraint
3. NOT NULL constraint
4. CHECK KEY constraint
5. PRIMARY KEY constraint
6. FOREIGN KEY constraint.
Note: You should define the constraint name while creating the table.
If you have not specified the constraint name while creating the table,
then the SQL server will give a random name to that constraint which is
not user-friendly.
The Default constraint in SQL Server is used to fill the column with a
default value
that is defined during the creation of a table if the user does not supply
any value while inserting the data.
Note: IDENTITY and timestamp columns can’t be associated with the default
constraint.
When you want a column not to accept NULL then you need to apply the NOT NULL
constraint to that column.
That means this constraint is used to avoid NULL values but it accepted
duplicate values into a column.
Check Constraint enforces the valid entries for a given column value by
restricting the type of the value, the format of the data, or the range of
possible values.
* The Primary Key in SQL Server is the combination of Unique and Not Null
Constraint.
* A table should contain only 1 Primary Key which can be either on a single
or multiple columns i.e. the composite primary key.
* A table should have a primary key to uniquely identify each record.
* A foreign key in one TABLE points to a primary key or unique key in another
table.
* The foreign key constraints are used to enforce referential integrity.
a) We require two tables for binding with each other and those two
tables must have a common column for linking the tables.
b) The common column that is present in both the tables need not have
the same name but their data type must be the same.
c) The common column that is present under the parent table or master
table is known as the reference key column and moreover,
the reference key column should not contain any duplicate values.
So we need to impose either UNIQUE or PRIMARY key constraint on that column.
d) If we impose the primary key constraint on the reference key column
that column will also be the identity column of the table.
e) The common column which is present in the child or detailed table is
known as the Foreign key column and
we need to impose a Foreign key constraint on the column which
refers to the reference key column of the master table.
* Column Level
* Table Level
Rule1: Cannot insert a value into the foreign key column provided that
value is
not existing in the reference key column of the parent (master)
table.
Rule2: Cannot update the reference key value of a parent table provided
that the value
has a corresponding child record in the child table without
addressing what to do with the child records.
Rule3: Cannot delete a record from the parent table provided that
records reference key value
has child record in the child table without addressing what to do
with the child record.
Note: Before adding the primary key constraint to the table first we
need to make the column NOT NULL and later add a primary key to that column.
Case1: Adding a primary key constraint on an existing column in the
table.
Note: While dropping the primary key constraint we first need to drop
the foreign key and then only we can delete the primary key.
The Cascading Referential Integrity Constraints in SQL Server are the foreign
key constraints
that tell SQL Server to perform certain actions whenever a user attempts to
delete or update
a primary key to which an existing foreign keys point.
1. SET NULL:
If a user tries to delete or update statement(s) that will affect rows
in the foreign key table,
then those values will be set to NULL when the primary key record is
deleted or updated in the Primary key table.
The important thing that we need to keep in mind that the foreign key
columns affected must allow NULL values.
2. CASCADE:
If a user tries to delete the statement(s) which will affect the rows
in the foreign key table,
then those rows will be deleted when the primary key record is deleted.
Similarly, if an update statement
affects rows in the foreign key table, then those rows will be updated
with the value from the primary key record after it has been updated.
3. SET DEFAULT:
If a delete or update statement affects rows in a foreign key table,
then all rows containing
those foreign keys are set to the default value. All foreign key
columns in the related table must have default constraints defined on them.
4. NO ACTION:
This is the default action that SQL Server performs. This specifies
that if an update or deletes statement
affects rows in foreign key tables, then the action will be denied and
rolled back. An error message will be raised.
Arguments:
Seed: Starting value of a column. The default value is 1.
Increment: It specifies the incremental value that is added to the identity
column value of the previous row. The default value is 1.
# Select Statement in SQL Server : The Select Statement in SQL Server is used to
returns records in the form of a result set from one or more tables or views.
Generally, the Select Statement retrieves the data in the form of rows from
one or more database tables or views.
* If you want to select all the columns of a table or view then you can also
use “*”, but for better performance use the column list instead of using “*”.
*