Professional Documents
Culture Documents
Entering Password:: Mysql
Entering Password:: Mysql
Entering password:
On the running machine: If you are logging in on the same machine that MySQL is running on,
you can omit the host, and simply use the following:
mysql -u root -p
On the host server: host and user represent the host name where your MySQL server is running
and the user name of your MySQL account.
mysql -h host -u user –p
If you want to supply your password on the command line after the -p option, you must
do so with no intervening space (for example, as -ppassword, not as -p password).
Basic queries
Show the version and the date of mysql> SELECT VERSION(), CURRENT_DATE;
The test database often is available as a workspace for users to try things out.
SHOW DATABASES does not show databases that you have no privileges for if you do
not have the SHOW DATABASES privilege.
Creating database
mysql> CREATE DATABASE database name;
database names are case-sensitive (unlike SQL keywords), so you must always refer to
your database as menagerie, not as Menagerie, MENAGERIE, or some other variant. This
is also true for table names. (Under Windows, this restriction does not apply, although
you must refer to databases and tables using the same lettercase throughout a given
query.
Making the current database:
USE menagerie
Your database needs to be created only once, but you must select it for use each time you begin
a mysql session. You can do this by issuing a USE statement as shown in the example.
Alternatively, you can select the database on the command line when you invoke mysql. Just
specify its name after any connection parameters that you might need to provide.
mysql -h host -u user -p database name
To view which database is you are currently working on: SELECT DATABASE();
Data types
INT -----------------------------------whole number
DECIMAL(M,N)-----------------------------M is total number of digit N- digit after decimal
VARCHAR(L)---------------------string of length l
BLOB------------------------------------binary large objects, store large data for images
DATE------------------------------------“yyyy-mm-dd”
TIMESTAMP---------------------------“yyyy-mm-dd hh:mm:ss”
Creating a table
To show table: mysql> SHOW TABLES;
Student_id INT PRIMARY KEY, the primary key indicate the attribute is the primary key
name VARCHAR(20),
Major VARCHAR(20) the last attribute does not end with comma
);
To add attribute after creation of table: ALTER TABLE student ADD gpa DECIMAL(3,2);
If you don’t know one value: INSERT INTO student( student_id , name) VALUES(1,"semir”);
To view table:
Student_id INT AUTO_INCREMENT, #### the data inserted will be automatically incremented, but first
needed to be initiated
name VARCHAR(20) NOT NULL ,#####THE COLUMN CAN NOT BE NULL
Major VARCHAR(20) DEFAULT ‘undecided’ ##### if no data is entered the default will be undecided
);
UPDATE student
To delete row:
BASIC QUERIES
SELECTING:
SELECT * FROM student; ###* IS TO MEAN ALL
SELECT name , major FROM student; ### select only name and major column
SELECT * FROM student WHERE major=”chemistry”; ### select students with major chemistry
SELECT * FROM student WHERE student_id <> 2; ##<> not equal symbol
ORDERING OR SORTING
SELECT student,name
FROM student
ORDER BY name, student_id DESC; ### order by name if same value occur then by student id
FOREIGN KEY
Can be defined only after all the tables are created
FOREIGN KEY (mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL
CREATE TABLE client(
client_id INT PRIMARY KEY,
client_name VARCHAR(40),
FOREIGN KEY(branch_id) REFERENCES branch(branch_id) ON DELETE SET NULL
);
COMPLEX QUERIES
Changing the attribute name and selecting:
FUNCTIONS
SELECT COUNT(emp_id)
FROM employee
WHERE sex =”F” AND birth_date > “1970-01-01”; #### NULL won’t be counted
Aggregate of count
SELECT COUNT(sex) , sex ### the column with red color will be visible
FROM employee
SELECT AVG(emp_id)
FROM employee;
SELECT SUM(emp_id)
FROM employee;
WILDCARD
%----any number of character
_ one character
SELECT *
FROM client
WHERE client_name LIKE "%LLC";
UNION
SELECT first_name #####we cannot add additional column here like SELECT first-name, lastname only
but we can if we add additional in client
FROM employee
UNION
SELECT branch_name
FROM branch
UNION
SELECT client_name
From client
#### first name and branch name must have same data type
JOINS
SELECT employee.emp_id, employee.first_name, branch.branch_name ###these will be visible
FROM employee
JOIN branch
LEFT JOIN branch ### all wil be listed which don’t share same value but with NULL value from left
table
right JOIN branch ### all wil be listed which don’t share same value but with NULL value from right or
on table
NESTED QUERIES
SELECT employee.first_name , employee.last_name
FROM employee
WHERE employee.emp_id IN (
SELECT works_with.emp_id
FROM works_with
WHERE works_with.total_sales > 30000);