Question:
How do I run SQL code in a way similar to regular programming languages using MySQL?
Q&A
2012-12-29 09:53:49 UTC
Do I save the SQL code in a .sql file? How would I run it using MySQL via the command line interface (in Linux)? Could someone give me a simple, self-contained piece of SQL code to test this?

I have used phpmyadmin but, I'd prefer an experience that's more like regular programming.

If more information is needed, just ask.

Any input would be greatly appreciated!

P.S.
The syntax I am talking about is the standard, cross-platform SQL syntax.
Five answers:
Chris D
2012-12-31 04:11:23 UTC
You use the « mysql » command. For example, if you have a password associated with your user account and you want to connect to the database « widgets » you would type this:



mysql -p widgets



This will drop you into the MySQL command line tool, where you can enter SQL statements (terminated with semicolon, ";").



In typical Unix/Linux style you can do all this non-interactively, too. This command line will print the current date/time as perceived by MySQL:



echo 'SELECT NOW();' | mysql -p



Finally, if you're using a programming language it's highly likely there it provides MySQL "bindings" so you can talk to MySQL directly. A good keyword pair to try is "MySQL DBI" (without quotes), along with your programming language name.
Cats Meow
2012-12-29 10:43:36 UTC
Hi Q&A ~



SQL is simply a bunch of commands that put and get info to and from a database.

Once you have (MySql or any version of it) installed on your computer, you can either access various databases (like Oracle or Microsoft's Access) using sql. It doesn't stand on it's own as it's not a programming language. It's more of a "helper" language... like an interpreter.



You can use a command line interface (Linux flavors, Windows or Mac's have console apps) to create "hidden" text file databases using sql, then you can populate these with data, and then use sql commands to access and display the data or sets of the data, add data, replace it, delete it, etc.

Or... you can do the same with database applications, like Oracle or Access.



It's too complicated to post code that would actually work the first time as there are too many parameters such as permissions, etc. to cover here, so I'd just do a Google search for some tutorials or go directly to the MySql web site as that is where I learned along with quick start type book.



~ Cat ~
galt_57
2012-12-29 10:01:05 UTC
You don't run SQL like a programming language. Generally in a language such as PHP you create a connection to the database and then you prepare a query and then you execute the query and then you examine the results that the query produced. Alternately you can just execute SQL at the command line. For MySQL you open the MySQL Command Line Client. From the command line client you can execute SQL commands directly or you can execute .sql files containing SQL commands.
TheMadProfessor
2012-12-31 08:09:24 UTC
You don't...SQL is not and never will be a programming language - it's a database maintenance and manipulation language. While some DBMS like SQL Server and Oracle have scripting languages that can perform traditional programming tasks, MySQL doesn't. What you CAN do is create a program written in PHP, Java or some other traditional programming language and embed code to make a connection to your database, pass it queries and receive back the resultsets for parsing and subsequent use.
grubbs
2016-10-04 05:37:55 UTC
C++ is a compiled language with most of the 'artwork' achieved for the period of compilation. it is, the compiler is complicated and generates helpful code. Python is likewise compiled, yet often enables scripts to execute at run time sq. is a script or grammar used with classes *for the period of execution*. In different words, scripts could run ON some thing on an identical time as compiled code is self-executable (on acceptable of the OS, frequently) i'm unfamiliar with g++. I particularly have heard reliable issues approximately code::blocks, besides the actuality that it is directed in the direction of c++.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...