In Part 1 of SQL Server, we have seen, using SSMS to connect to SQL Server. In this part we will learn creating, altering and dropping a database.
A SQL Server database can be created, altered and dropped
1. Graphically using SQL Server Management Studio (SSMS) or
2. Using a Query
A SQL Server database can be created, altered and dropped
1. Graphically using SQL Server Management Studio (SSMS) or
2. Using a Query
To create the database graphically
1. Right Click on Databases folder in the Object explorer
2. Select New Database
3. In the New Database dialog box, enter the Database name and click OK.
To Create the database using a query
Create database DatabaseName
Whether, you create a database graphically using the designer or, using a query, the following 2 files gets generated.
.MDF file - Data File (Contains actual data)
.LDF file - Transaction Log file (Used to recover the database)
To alter a database, once it's created
Alter database DatabaseName Modify Name = NewDatabaseName
Alternatively, you can also use system stored procedure
Execute sp_renameDB 'OldDatabaseName','NewDatabaseName'
To Delete or Drop a database
Drop Database DatabaseThatYouWantToDrop
Dropping a database, deletes the LDF and MDF files.
You cannot drop a database, if it is currently in use. You get an error stating - Cannot drop database "NewDatabaseName" because it is currently in use. So, if other users are connected, you need to put the database in single user mode and then drop the database.
Alter Database DatabaseName Set SINGLE_USER With Rollback Immediate
With Rollback Immediate option, will rollback all incomplete transactions and closes the connection to the database.
Note: System databases cannot be dropped.
Hi Venkat,
ReplyDeleteIs it possible to add sample links of scriptlets or snippets of databases and tables as a part of your download.
Additionally, TSQL based programming functions and procedures tutorials will be a good start for beginners.
thanks
Hi Venkat,
ReplyDeleteIf the size of the .LDF file is getting increased in GBs so how to decrease the size of it. In my case the size is increased about 48GB. I have tried with shrink option but it is not working.
Thanks.
Simple take log backup of database and perform DBCC Shrink file operation on it.
DeleteChange the database recovery mode to simple if you don't need to restore the data to point in time.
Deleteif we drop the database .ldf file will be deleted.then how it is used to recover the database?
ReplyDeleteIf the .ldf file is deleted, it means that all the transaction log data is lost, and it will not be possible to use it for recovering the database. In such a scenario, you will have to resort to other backup and restore options to recover your data.
Deletehi Mr. venkat,
ReplyDeletei need to take sql sever course with you
Hi sir
ReplyDeleteIs there any chance to create the Files with Query
Hi Venkat,
ReplyDeleteIn this blog, as per you said if we delete the database both .mdf and .ldf will be deleted but if we rename the database, Will the actual database name in file path be changed?
I found in my case that if I rename the database, the change in database name is not reflecting in the actual file path, so what's the reason behind it?
Thanks
Dear Venkat Sir,
ReplyDeleteWhen I executed following query,
Alter table tblPerson
add constraint tblPerson_GenderId_FK FOREIGN KEY (GenderId) references tblGender(ID)
Getting below error,
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "tblPerson_GenderId_FK". The conflict occurred in database "sample", table "dbo.tblGender", column 'ID'
May be your table "tblPerson" GenderId column has value other than value present in the table "tblGender" Id column. i.e., If GenderId column has a value "99" prior to foreign key constraint is a valid case. But if you try to add foreign key constraint GenderId column must have a value of the table "tblGender" Id column or NULL
DeleteHi Venkat,
ReplyDeleteWill it be possible to share .bak file which you are using for training.
It would be helpful for tarining
Msg 547, Level 16, State 0, Line 1
ReplyDeleteThe ALTER TABLE statement conflicted with the FOREIGN KEY constraint "tblPerson_GenderID_FK". The conflict occurred in database "Sample3", table "dbo.tblGender", column 'ID'.
Avoid using sp_renameDB to rename the Database, as it is soon going to be removed from future version of MS SQL SERVER. Refer: https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-renamedb-transact-sql?view=sql-server-ver15
ReplyDeletetrying to drop a database but it failed. I tried the command shown below (change to a single user ) but I got an error message which is : User does not have permission to alter database. Anyone can help ?
ReplyDelete