Vous êtes sur la page 1sur 11

Ques

tions how to do login in mysql with unix shell


:1
Answ By below method if password is pass and user
ers : name is root
1
# [mysql dir]/bin/mysql -h hostname -u root -p
pass
Ques
how you will Create a database on the
tions
mysql server with unix shell
:2
Answ
ers : mysql> create database databasename;
2
Ques
how to list or view all databases from the
tions
mysql server.
:3
Answ
ers : mysql> show databases;
3
Ques
tions How Switch (select or use) to a database.
:4
Answ
ers : mysql> use databasename;
4
Ques
How To see all the tables from a database
tions
of mysql server.
:5
Answ mysql> show tables;
ers :

5
Ques
How to see table's field formats or
tions
description of table .
:6
Answ
ers : mysql> describe tablename;
6
Ques
How to delete a database from mysql
tions
server.
:7
Answ
ers : mysql> drop database databasename;
7
Ques
tions How we get Sum of column
:8
Answ
ers : mysql> SELECT SUM(*) FROM [table name];
8
Ques
tions How to delete a table
:9
Answ
ers : mysql> drop table tablename;
9
Ques
tions How you will Show all data from a table.
: 10

Answ
ers : mysql> SELECT * FROM tablename;
10
Ques How to returns the columns and column
tions information pertaining to the designated
: 11 table
Answ
ers : mysql> show columns from tablename;
11
Ques
How to Show certain selected rows with
tions
the value "pcds"
: 12
Answ
mysql> SELECT * FROM tablename WHERE
ers :
fieldname = "pcds";
12
Ques How will Show all records containing the
tions name "sonia" AND the phone number
: 13 '9876543210'
Answ mysql> SELECT * FROM tablename WHERE
ers : name = "sonia" AND phone_number =
13
'9876543210';
Ques How you will Show all records not
tions containing the name "sonia" AND the
: 14 phone number '9876543210' order by the
phone_number field.
Answ mysql> SELECT * FROM tablename WHERE
er :
name != "sonia" AND phone_number =
14
'9876543210' order by phone_number;
Ques How to Show all records starting with the

tions letters 'sonia' AND the phone number


: 15 '9876543210'
Answ mysql> SELECT * FROM tablename WHERE
ers : name like "sonia%" AND phone_number =
15
'9876543210';
Ques How to show all records starting with the
tions letters 'sonia' AND the phone number
: 16 '9876543210' limit to records 1 through 5.
Answ mysql> SELECT * FROM tablename WHERE
ers : name like "sonia%" AND phone_number =
16
'9876543210' limit 1,5;
Ques Use a regular expression to find records.
tions Use "REGEXP BINARY" to force case: 16 sensitivity. This finds any record beginning
with r.
Answ
mysql> SELECT * FROM tablename WHERE rec
er :
RLIKE "^r";
16
Ques
tions How you will Show unique records.
: 17
Answ
mysql> SELECT DISTINCT columnname FROM
er :
tablename;
17
Ques
how we will Show selected records sorted
tions
in an ascending (asc) or descending (desc)
: 18
Answ mysql> SELECT col1,col2 FROM tablename
er :
ORDER BY col2 DESC;
18

mysql> SELECT col1,col2 FROM tablename


ORDER BY col2 ASC;

Ques
tions how to Return total number of rows.
: 19
Answ
ers : mysql> SELECT COUNT(*) FROM tablename;
19
Ques
tions How to Join tables on common columns.
: 20
Answ mysql> select lookup.illustrationid,
er :
lookup.personid,person.birthday from lookup
20
left join person on
lookup.personid=person.personid=statement to
join birthday in person table with primary
illustration id
Ques How to Creating a new user. Login as root.
tions Switch to the MySQL db. Make the user.
: 21 Update privs.
Answ # mysql -u root -p
er :
21
mysql> use mysql;
mysql> INSERT INTO user
(Host,User,Password)
VALUES('%','username',PASSWORD('password')
);
mysql> flush privileges;

Ques
How to Change a users password from
tions
unix shell.
: 22
Answ
# [mysql dir]/bin/mysqladmin -u username -h
ers :
hostname.blah.org -p password 'new-password'
22
Ques How to Change a users password from
tions MySQL prompt. Login as root. Set the
: 23 password. Update privs.
Answ # mysql -u root -p
er :
23
mysql> SET PASSWORD FOR 'user'@'hostname'
= PASSWORD('passwordhere');
mysql> flush privileges;
Ques How to Recover a MySQL root password.
tions Stop the MySQL server process. Start again
: 24 with no grant tables. Login to MySQL as
root. Set new password. Exit MySQL and
restart MySQL server.
Answ # /etc/init.d/mysql stop
er :
# mysqld_safe --skip-grant-tables &
24
# mysql -u root
mysql> use mysql;
mysql> update user set
password=PASSWORD("newrootpassword")
where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
Ques How to Set a root password if there is on

tions
root password.
: 25
Answ
er :
# mysqladmin -u root password newpassword
25
Ques
tions How to Update a root password.
: 26
Answ
# mysqladmin -u root -p oldpassword
er :
newpassword
26
Ques How to allow the user "sonia" to connect
tions to the server from localhost using the
: 27 password "passwd". Login as root. Switch
to the MySQL db. Give privs. Update privs.
Answ # mysql -u root -p
ers : mysql> use mysql;
27
mysql> grant usage on *.* to sonia@localhost
identified by 'passwd';
mysql> flush privileges;
Ques How to give user privilages for a db. Login
tions as root. Switch to the MySQL db. Grant
: 28 privs. Update privs.
Answ # mysql -u root -p
ers : mysql> use mysql;
28
mysql> INSERT INTO user
(Host,Db,User,Select_priv,Insert_priv,Update_pr
iv,Delete_priv,Create_priv,Drop_priv) VALUES
('%','databasename','username','Y','Y','Y','Y','Y',
'N');
mysql> flush privileges;
or

mysql> grant all privileges on databasename.*


to username@localhost;
mysql> flush privileges;
Ques
How To update info already in a table and
tions
Delete a row(s) from a table.
: 29
Answ mysql> UPDATE [table name] SET Select_priv
er :
= 'Y',Insert_priv = 'Y',Update_priv = 'Y' where
29
[field name] = 'user';
mysql> DELETE from [table name] where [field
name] = 'whatever';
Ques
How to Update database
tions
permissions/privilages.
: 30
Answ
er :
mysql> flush privileges;
30
Ques
How to Delete a column and Add a new
tions
column to database
: 31
Answ mysql> alter table [table name] drop column
er :
[column name];
31
mysql> alter table [table name] add column
[new column name] varchar (20);
Ques
Change column name and Make a unique
tions
column so we get no dupes.
: 32
Answ mysql> alter table [table name] change [old
er :
column name] [new column name] varchar
32
(50);

mysql> alter table [table name] add unique


([column name]);
Ques
How to make a column bigger and Delete
tions
unique from table.
: 33
Answ mysql> alter table [table name] modify [column
er :
name] VARCHAR(3);
33
mysql> alter table [table name] drop index
[colmn name];
Ques
tions How to Load a CSV file into a table
: 34
Answ mysql> LOAD DATA INFILE '/tmp/filename.csv'
er :
replace INTO TABLE [table name] FIELDS
34
TERMINATED BY ',' LINES TERMINATED BY '\n'
(field1,field2,field3);
Ques How to dump all databases for backup.
tions Backup file is sql commands to recreate all
: 35 db's.
Answ
# [mysql dir]/bin/mysqldump -u root
er :
-ppassword --opt >/tmp/alldatabases.sql
35
Ques
tions How to dump one database for backup.
: 36
Answ # [mysql dir]/bin/mysqldump -u username
er :
-ppassword --databases databasename
36
>/tmp/databasename.sql
Ques How to dump a table from a database.

tions
: 37
Answ # [mysql dir]/bin/mysqldump -c -u username
er :
-ppassword databasename tablename >
37
/tmp/databasename.tablename.sql
Ques
Restore database (or database table) from
tions
backup.
: 38
Answ # [mysql dir]/bin/mysql -u username
er :
-ppassword databasename <
38
/tmp/databasename.sql
Ques
tions How to Create Table show Example
: 39
Answ mysql> CREATE TABLE [table name] (firstname
er :
VARCHAR(20), middleinitial VARCHAR(3),
39
lastname VARCHAR(35),suffix
VARCHAR(3),officeid VARCHAR(10),userid
VARCHAR(15),username VARCHAR(8),email
VARCHAR(35),phone VARCHAR(25), groups
VARCHAR(15),datestamp DATE,timestamp
time,pgpemail VARCHAR(255));
Ques How to search second maximum(second
tions highest) salary value(integer)from table
: 40 employee (field salary)in the manner so
that mysql gets less load?
Answ
ers : By below query we will get second
maximum(second highest) salary
40
value(integer)from table employee (field
salary)in the manner so that mysql gets less
load?
SELECT DISTINCT(salary) FROM employee

order by salary desc limit 1 , 1 ;


(This way we will able to find out 3rd highest ,
4th highest salary so on just need to change
limit condtion like LIMIT 2,1 for 3rd highest and
LIMIT 3,1 for 4th
some one may finding this way useing below
query that taken more time as compare to
above query SELECT salary FROM employee
where salary < (select max(salary) from
employe) order by salary DESC limit 1 ;

Vous aimerez peut-être aussi