How To Connect to MySQL Database in Visual Studio Code and Run SQL Queries | MySQL in VSCode (2024)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can use Visual Studio code to run SQL queries from within your Visual Studio code editor in this particular example I'm going to use MySQL server so I'm going to show you how you can connect to mySQL server using your Visual Studio code how you can create tables insert data and fetch data using visual studio code itself in addition this method you can use to connect to any other popular SQL Server Like postr SQL or Microsoft SQL server or any other popular SQL Server so let's see how we can do it so first of all we need to install the required extension to connect to our SQL server in our case the MySQL so I'm going to go to the extensions section and here search for SQL tools and you will be be able to see this popular extension which is called SQL tools and this SQL tool helps us to manage our SQL database when you scroll down here you will be able to see uh different databases which it supports so we are going to install this extension first of all so I'm going to click on install which is going to install this extension and once this extension is installed you will be able to see the disable or unable install Button as soon as this extension is installed you will be able to see this database icon and when you hover over this database icon it says SQL tools so I'm going to click on this uh SQL tools icon and you will be able to see this kind of section here so let me close uh this window here and let's create a new connection so I can click on this add new connection here but when you click on this uh new connection you will see a blank space here this is because we have installed the SQL tools extension but in order to connect to a particular SQL Server you need to install the driver extension for that also so as in our case we are using MySQL so I'm going to once again go to the extensions section and once again write SQL tools here and you will be able to see the popular drivers which are supported for different database systems like MySQL sqlite postr Microsoft SQL Server so these are the official drivers for all these database systems so whatever database system you are using just click on that uh option so it's going to start with SQL tool and then the name of that database system so in my case its SQL tools MySQL and then I'm going to click on this driver extension also once this driver extension is installed I can close this window and I can see these options now so once again when I click on SQL tools and let me close this and then once again when I click on add new connection I will be able to see these options because I have installed the mysq driver extension for SQL tool and now let's click on this MySQL option here and here you can provide the details about your connection so I'm running my MySQL server locally so I'm going to name my uh connection as MySQL local and the connection group you can provide the name of your connection group I'm going to leave it as default now in the third option you can connect using the server and the port you can also connect via socket file or you can connect via the connection string we are going to use the server and the port option then as I mentioned my server is running on the Local Host so I'm going to leave it as default if your MySQL server is running on some remote server just give the IP address of that remote server Now the default port on which MySQL runs is 33 06 and most probably you are also using this default Port but if you are using any other Port then just provide that port for your MySQL server now if you are running MySQL locally and if you're using SQL tools here you need to add one extra zero in front of uh this port so if you're using 3360 here I need to use one extra zero in order to connect via the SQL tools option so don't forget to add this extra zero and then you also need to provide the name of the database so this database should preexist in your MySQL server I'm going to give the database name demo this database pre-exists on my MySQL server so make sure you have created this database previously now the username is the username which you use to connect to your MySQL server in my case it's root then you also have the option to choose the password mode so you have all these different options like uh SQL tools driver credentials ask on connect use empty password and save as plain text in settings so I'm going to use the last option here because I just uh want to make things simp simple but if you want to use any other option you can uh choose that option from here let me provide the password which I Ed to connect to my MySQL server now here under MySQL driver specific options we need to select uh X protocol for Authentication Protocol and if you're using SSL connection for your server then you can use enabled otherwise leave it as disabled you also need to provide the connection timeout for your uh MySQL server connection I'm going to provide 30 here but you are free to provide any other timeout here and then you also have the option for show records default limit here by default it's 50 I'm okay with it so I'm going to leave it as default once you have given all these uh configurations you can also test your connection by clicking on the test connection button so I'm going to click on test connection and I can see that uh the connection is successful ful so I can see this status here once I'm happy I'm going to click on Save connection and my connection is created here you will be able to see the list of all the connections here on the left hand side and you can also see uh this kind of uh connection configuration on the right hand side now in order to connect to your uh database server you can click on this Connect Now button or you can also click on this plug icon for The Connect so that uh connection will be successful so I'm going to click on uh this connect icon here and if the connection works it's going to show you this kind of SQL session so here you can run your SQL queries so let's say in my demo data base I need to create a new table I can write the query for that the query is really simple I want to create a users's database and the column which I need is ID username and email here in in order to run this query you can uh just click on this run the active connection option which is going to create this so once uh this query runs successfully you can expand this uh option here and you will be able to see your database name and when you expand this database you will be able to see tables here and then when you expand you will be able to see your tables I have recently created this users table so I can see this users table here I can also see what columns this table has by just expanding on the table option so now I have added some columns to my table let's insert some data to the table so to insert the data to the table I can once again write the insert query here so let me just uh do it like this so you can see the query and you can also see the output put here okay so this query is also really simple I'm going to use insert into the table name and then I want to just add the username and email ID because the ID is auto incrementing here right we just want to run this query right so in order to run this query only you can just select the query right click on the query and also you also have the option to run the selected query we need to use this option here so I'm I'm going to click on run selected query which is going to run my query and then we will be able to see the output now once I have inserted some data how can I view this the simple option is you can uh just write the select query so I can just write uh select srix from users so you can see when I type any query it uh gives me the autocomplete option and I'm going to select this query and then right click on it and then click on run selected query you can also press command e command e on Mac if you are using other operating system you will be able to see the key combination in front of this run selected query here so just run it and then I can see the data here you can see this data is available here I have ran this query two times that's why uh it was giving me uh the data two times the select option is also available when you click on the table option so right click on your table here and then click on show table record and it's going to show you the table record you can also right click on your table and you have the option to describe your table so click on that and it's going to describe your tables that means what are the columns which are available here what are uh their constraints if there is a primary key there so all the information about your table right you also have the option to generate insert query right so when you right click on the user table for example I right click here and then click on insert query it's going to insert the insert query just below your other queries so you can see I have the same kind of query which is insert into users and then I have the option uh to add the ID username and email as I mentioned ID is uh Auto incrementing in my case so I'm going to remove the ID option from here but I can add the username and email from here just change these values and then you can insert those values then I can run this uh query you know the drill just click on run selected query and once that successful right click on your table and then click on show table records and I can see the seventh record is added here this is how you can connect to your MySQL server and query and query your mySQL database using the visual studio code and this SQL tools extension now let's say in future you need to change the configuration of your MySQL connection you can do that also so let let's say you have changed the IP address of your MySQL connection and you want to change it into a connection you can right click on your connection here and then click on edit connection and then you will be able to edit your connection configuration from here once again so that is also useful if you want to disconnect you can click on this icon here for disconnection you also have the option to refresh your connection and then delete your connection and then you also have the option for open settings and hopefully this will be helpful tool for you also so that's it for this video I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 14,612
Rating: undefined out of 5
Keywords: How to use MySQL database in VS Code?, How do I run SQL in VS Code?, Can Visual Studio run MySQL?, How to connect code to MySQL database?, Can we run MySQL in VS Code?, How to run MySQL in cmd?, Where can I run MySQL code?, How to connect to MySQL database in Visual Studio C#?, How to create MySQL database?, How to code MySQL database in Python?, How to run SQL code in MySQL?, How to create a database in MySQL command line?
Id: 9ADd-_mM5Dw
Channel Id: undefined
Length: 13min 11sec (791 seconds)
Published: Sat Feb 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.