mysql_dir/bin/mysql -h hostname -u root -p
where mysql_dir is replaced by the actual directory path.
mysql -u UserName -p DBName
create database DBNAME;
use DBNAME;
mysql -u <username> -p <pwd> < ScriptFile.sql
--invalid comment
-- valid comment
SELECT * FROM [my table name with space];
SELECT * FROM `my table name with space`;
SET sql_mode='ANSI_QUOTES';
Thanks to both AngelIII and Mark_Wills for mentioning this.
CREATE TABLE MyTable (ID int unsigned NOT NULL Auto_Increment Primary Key);
It seems that an Auto_Increment field MUST be defined as a key ... which one would usually want to do anyway.
CREATE TABLE MyTable (ID int unsigned NOT NULL Auto_Increment Primary Key, View_Date timestamp NOT NULL Default now( ) );
UPDATE T1 Set Field1=T2.F1, ...Fieldn = T2.Fn FROM T1 inner join T2
try (moving the "set...")
Update T1 inner join T2 Set T1.F1 = T2.F1, ... T1.Fn = T2.Fn
UPDATE Furnace_log
SET max_thk=thickness.h_limit, min_thk=thickness.l_limit
FROM Furnace_log
INNER JOIN thickness on Furnace_log.mattyp=thickness.Material
AND Furnace_log.Coating=thickness.Coating
UPDATE Furnace_log
INNER JOIN thickness on Furnace_log.mattyp=thickness.Material
AND Furnace_log.Coating=thickness.Coating
SET Furnace_Log.max_thk=thickness.h_limit,
Furnace_Log.min_thk=thickness.l_limit;
SELECT * FROM books WHERE title LIKE '%PHP%';
Select Table_Name, Table_Rows from information_schema.tables order by table_rows limit 10;
-- OK, that was very basic ... equivalent to SELECT top 10 ...
Select Table_Name, Table_Rows from information_schema.tables order by table_rows limit 10, 10;
-- This one gives you the NEXT 10 records. Hmm ... pagination on your web page, anyone?
Delete from wt_usersettings limit 2;
-- No kidding, that deletes only the "first" 2 records. Defining "first" is still a little up-in-the-air ...
update wt_usersettings set SettingValue = 'Something' Where SettingValue is NULL limit 2;
-- Again, only 2 records will be updated.
Thanks to Mark Wills for suggesting inclusion of this point!
exec sp_help tablename
MySQL:
Describe tablename;
I hope to post another article shortly with more DDL (Create Table, Drop Column, etc.) statements. But this article is about long enough!
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
An idea, from a DBA that often uses MySQL for minor things, I often forget how to login to mysql from an OS prompt. Yes, that sounds a bit embarassing, as I am an Oracle, DB2, Sybase DBA, but its true. Its due to seldom usage, and always having to hit the docs to remember. Maybe you could add that to your quick reference.
Good article.
Commented:
Voted yes above.
Good job!
Commented:
1: mysql -u <username> -p <pwd> < ScriptFile.sql
Commented:
And a good practice is to not include the password clear text in the command string. From some command line / terminal based systems, when another user comes to the same server all they have to do is pull up the previous command and run it regardless of their having the clearance as a database administrator or not. Aside from potential harmful scripts being run without authority, if this is your root password it is now known to other users.
So I tend to do this as :
(then type in the password when prompted)
Open in new window