Connect to SQL Server Using Visual Studio Code 2022 and Run SQL Queries (Create Read Update Delete)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video i will show you how to connect to sql server database using visual studio code so first we need to download and install sql server so using the browser let's type sql server then let's go to this first link it is on microsoft.com and in this page we have to scroll down and then we can download and install the express edition in my case the express edition is already installed on my computer now let's go to visual studio code and here we need to install a new extension so let's click on this extensions button then here let's write sql server so this is the extension that we need to install let's click on it so we can see that it is from microsoft and it has been installed this number of times so to install it we have just to click on this install button now the extension is installed correctly so we can close this page and here we can see that we have this new item so let's click on this item it is sql server now to create a new connection we can either use this button or also we can click on this plus button so let's click on this button so here we need to provide the address of the sql server we need also to provide the instance name so to find the instance name we can open the services application so here let's write services.msc and in this window let's find sql server so this is sql server it is running and between parentheses i have the instance name so in my case the instance name is sql express so to create the new connection i have to provide the host name which is in my case localhost then slash then the instance name which is sql express it is also possible to remove localhost and to replace it with dot now let's press enter and here we need to provide the name of the database so by default if we don't provide the name of the database we will connect to the default database which is the master database in my case i want to connect to this default database so let's press enter then here we need to select the authentication method so by default when we installed the sql server it comes with the integrated authentication method which means that it uses the authentication of the operating system it is also possible to enable the sql login which allows you to login using user name and password but in my case the only authentication method that is available with sql server is the integrated method so i will just select it here i need to provide the connection profile so the connection profile is the name that will appear in this area in my case i will just call it first sql connection let's press enter so here we can see that this connection has been created so let's expand the databases and here we can see that i have these two databases also i have the system databases now let's execute some sql queries so to execute some queries i have just to make a right click on the connection name then new query so first let's obtain the name of the currently selected database so we have just to execute the following query it is select db name now to execute this query i can make right click then execute query it is also possible to click on this run button and in this area i have the result so currently i am connected to the default database which is the master database now let's create a new database so let's close this page and let's remove this query so to create a new database i have to execute the following query so the query is create database and the name of the database that i want to create now let's run this query so i can make a right click then execute query so here we can see that the query is executed correctly we can close this page but here we don't see the database that i created which is my shop so to see this database here i have just to make a right click on databases then refresh and here we can see that we have this database now i want to switch to this database so let's remove this query and let's execute the following query so to select the database i can use the use keyword followed by the name of the database now let's run it and it is executed correctly we can check which database is currently selected so let's remove this query and let's execute the following query let's run it and now this is the database which is currently selected now i will show you how to create a new table into this database so let's close this page and let's remove this query so to create a new table we can write the following query and here we have this option sql create table let's select it so we obtain this template so we can remove all of this we can also remove the schema name because we already selected one database so here we need to provide the table name i will call it users also i will modify the name of these columns so this one i will call it id so it is an integer it is not null and also it is a primary key so to make this primary key auto incremental i have to add the following keyword it is identity one one now let's change the name of the columns so this one i will call it username it is of type varchar and for the length we can choose 100 for example the second column i will call it email also it is of type varchar we can also add other columns so i will add only one more column so i will call it register date it is of type datetime and it has a default time which is the current time now let's run this query and here we can see that it has been executed correctly let's close this page and to find this table under this database i have to expand tables and here we can see that we have this table so let's expand it and here we can see the different columns now let's insert some rows into this table so let's remove this query and let's write the following query so here we can select this option sql insert rows and we obtain this template so we can remove this command and here let's change the table name also let's change the name of the columns so in our case the id column is auto incremental we don't need to provide it with any value also we have the user name so let's write here username which is the name of the column then let's provide the name of the second column which is email then let's provide the values so here i have to provide the values between simple codes if i use double quotes i will obtain an error so i have to use simple quotes let's provide the username let's write for example bill gates and let's provide the email it should be also provided between simple codes so it is possible to insert multiple rows but in my case i want only to insert one row so i will remove all of this now let's run this query we can close this page and let's add more rows so let's remove this and let's provide new values let's add a new row so let's add comma then the neuro and let's add a third row now let's execute this query and we can see that it has been executed correctly now to see the data of the table we can remove this query and we can execute the following query so to run it let's click on this button and here we have the data of the table so here we can see that we have the id because it is auto incremental and also we have the register date because it has a default value now i will show you how to update a row for example let's update this row so here to update this row we can use its id which is unique so let's write the following query and here we can select this option sql update rows so i will update the table users and i will update the column email so i want to change the email so here i will remove this value and then i will use simple quotes and i will provide the new value so in my case i will only update the email column so i will remove all of this and also i need to provide the where condition so here i will update the row with the id equal to 4. now let's run this query and it has been executed correctly so we can remove this and to see the data of the table we can execute the following query now let's run this query and here we can see that the email has been updated now i will show you how to delete a row from a table so we have to use the following query so here we can select sql delete rows and then we have to provide the name of the table which is users so the condition will be id equal to 4. now let's run it so it has been executed correctly then let's see the data of the table so here we can see that the row number 4 has been removed it is also possible to execute multiple queries simultaneously so let's create two tables i will remove this and i will paste the queries that allows me to create two tables so first i want to create this table called products and also i want to create the second table called messages now let's run this query and it has been executed correctly so to see the new tables that i have created i need just to refresh the tables node and here we can see that i have three tables now i will show you how to drop a table so to drop the table i can use the following query it is drop table followed by the name of the table that i want to drop which is in my case products so let's run this query and we can see that it has been executed correctly so here let's refresh the tables node and we can see that the products table has been removed
Info
Channel: BoostMyTool
Views: 66,546
Rating: undefined out of 5
Keywords:
Id: WVKEkY3FlP4
Channel Id: undefined
Length: 14min 5sec (845 seconds)
Published: Wed Mar 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.