56
What's SQL What's SQL ? ? Structured Query Language, is a database computer language designed for managing data in relational database management systems (RDBMS), and originally based upon relational algebra. Its scope includes data query and update, schema creation and modification, and data access control.

My sql presentation

Embed Size (px)

Citation preview

Page 1: My sql presentation

What's SQLWhat's SQL??Structured Query Language, is a

database computer language designed for managing data in

relational database management systems (RDBMS), and originally based upon relational algebra. Its

scope includes data query and update, schema creation and modification, and data access

control.

Page 2: My sql presentation

Downloading MySQL

To download MySQL, go to http://www.mysql.com/get/Downloads/MySQL-3.23/mysql-3.23.58-win.zip/

from/pick and choose a file mirror to download from.

Page 3: My sql presentation

Installing MySQLInstructions:

1. Firstly, we have to extract the MySQL zip archive to a temporary file and run setup.exe

from the extracted directory.2. You can safely use all the default options for

this installer.3. Once the setup wizard is complete it is safe to

delete the files you have just extracted (the mysql-3.23.58-win folder).

4. Run the file C:\mysql\bin\winmysqladmin.exe and a user name and a

password of your choice.

Page 4: My sql presentation

This user name and password combination is used for the MySQL administration utility - it is not used

to connect to databases at all.

Installation of MySQL is now complete.

Page 5: My sql presentation

To test that Apache recognizes the MySQL installation go to http://127.0.0.1/test.php in a

web browser again and scroll down the page until

the MySQL section.

If “MySQL Support” is marked as enabled, you have successfully installed MySQL and should now have a complete working Apache, MySQL, PHP environment.

Page 6: My sql presentation

Basic And Advance SQL

Commands

Page 7: My sql presentation

CREATE TABLESYNTAX: CREATE TABLE [table_name] (

[column_name1] INT AUTO_INCREMENT,

[column_name2] VARCHAR(30) NOT NULL,

[column_name3] ENUM('guest', 'customer', 'admin')NULL,

[column_name4] DATE NULL,

[column_name5] VARCHAR(30) NOT NULL,

[column_name6] DATETIME NOT NULL,

[column_name7] CHAR(1) NULL,

[column_name8] BLOB NULL,

[column_name9] TEXT NOT NULL,

UNIQUE(username),

PRIMARY KEY (column_name1)

);

Example:

CREATE TABLE user (

userid INT AUTO_INCREMENT,

username VARCHAR(30) NOT NULL,

group_type ENUM('guest', 'customer', 'admin') NULL,

date_of_birth DATE NULL,

password VARCHAR(30) NOT NULL,

registration_date DATETIME NOT NULL,

account_disable CHAR(1) NULL,

image BLOB NULL,

comment TEXT NOT NULL,

UNIQUE(username),

PRIMARY KEY (userid)

);

Page 8: My sql presentation

INSERT STATEMENTS Syntax:

INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col_A_data`, `col_B_data`, `col_C_data`) ;

Example:

INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `Abbey Road`);

Page 9: My sql presentation

REPLACE STATEMENTS

Syntax:

REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ;

Example:

REPLACE INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `abbey road`);

Page 10: My sql presentation

UPDATE STATEMENTS Syntax:

UPDATE table_name SET col_B='new_data' WHERE col_A='reference_data' ;

Example:

UPDATE music SET title='Come Together' WHERE id=1;

Page 11: My sql presentation

Add a new column "male" in table user.

Syntax:

ALTER TABLE [table_name]

ADD COLUMN [column_name]

CHAR(1) NOT NULL;

Example:

ALTER TABLE user

ADD COLUMN male

CHAR(1) NOT NULL;

Page 12: My sql presentation

Change column name "male" into "gender" in table user and change the type to VARCHAR(3) and allow NULL

values.Syntax:

ALTER TABLE [table_name]

CHANGE [old_column] [new_column]

VARCHAR(3) NULL;

Example:

ALTER TABLE user

CHANGE male gender

VARCHAR (3) NULL;

Page 13: My sql presentation

Change the size of column "gender" from 3 to 6 in table user.

Syntax:

ALTER TABLE [table_name]

MODIFY [column_name] VARCHAR(6);

Example:

ALTER TABLE user

MODIFY gender VARCHAR(6);

Page 14: My sql presentation

SELECT STATEMENTS

Syntax:

SELECT * FROM table_name WHERE 1 ;

Example:

SELECT * FROM music WHERE 1;

Page 15: My sql presentation

DELETE STATEMENTS

Syntax:

DELETE FROM table_name WHERE column_name='search_data';

Example:

DELETE FROM music WHERE artist='the beatles';

Page 16: My sql presentation

Show field formats of the selected table.

Syntax:

DESCRIBE [table_name];

Example:

DESCRIBE mos_menu;

Page 17: My sql presentation

To see database's field formats.

mysql> describe [table name];

Page 18: My sql presentation

To delete a db.

mysql> drop database [database name];

Example:

DROP DATABASE demodb;

Page 19: My sql presentation

To delete a table.

mysql> drop table [table name];

Example:

DROP TABLE user;

Page 20: My sql presentation

Show all data in a table.

mysql> SELECT * FROM [table name];

Example:

SELECT *

FROM mos_menu;

Page 21: My sql presentation

Show all records from mos_menu table containing

name "Home".

SELECT *

FROM [table_name]

WHERE [field_name]=[value];

Example:

SELECT *

FROM mos_menu

WHERE name = "Home";

Page 22: My sql presentation

Returns the columns and column information pertaining

to the designated table.

mysql> show columns from [table name];

Page 23: My sql presentation

Show certain selected rows with the value "whatever".

mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Page 24: My sql presentation

Show all records containing the name "Bob" AND the phone

number '12345678'.

mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number

= '12345678';

Page 25: My sql presentation

Show all records not containing the name "Bob" and the phone

number '12345678' order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != "Bob" AND

phone_number = '12345678' order by phone_number;

Page 26: My sql presentation

Show all records starting with the letters 'bob' AND the phone

number '12345678'.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND

phone_number = '12345678';

Page 27: My sql presentation

Show all records starting with the letters 'bob' AND the phone

number '12345678' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND

phone_number = '12345678' limit 1,5;

Page 28: My sql presentation

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This

finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Page 29: My sql presentation

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

Page 30: My sql presentation

Show selected records sorted in an ascending (asc) or

descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Page 31: My sql presentation

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

Page 32: My sql presentation

Sum column.

mysql> SELECT SUM(*) FROM [table name];

Page 33: My sql presentation

Join tables on common columns.

mysql> select lookup.illustrationid, lookup.personid,person.birthday from

lookup left join person on lookup.personid=person.personid=statemen

t to join birthday in person table with primary illustration id;

Page 34: My sql presentation

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'))

;mysql> flush privileges;

Page 35: My sql presentation

Change a users password from unix shell.

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-

password'

Page 36: My sql presentation

Change a users password from MySQL prompt. Login as root.

Set the password. Update privs.

# mysql -u root -pmysql> SET PASSWORD FOR

'user'@'hostname' = PASSWORD('passwordhere');

mysql> flush privileges;

Page 37: My sql presentation

Recover a MySQL root password. Stop the MySQL server process. Start again with no

grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL

server.

# /etc/init.d/mysql stop# mysqld_safe --skip-grant-tables &

# mysql -u rootmysql> 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

Page 38: My sql presentation

Set a root password if there is on root password.

# mysqladmin -u root password newpassword

Page 39: My sql presentation

Update a root password.

# mysqladmin -u root -p oldpassword newpassword

Page 40: My sql presentation

Allow the user "bob" to connect to the server from localhost using the password "passwd". Login as root.

Switch to the MySQL db. Give privs. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> grant usage on *.* to bob@localhost identified by 'passwd';

mysql> flush privileges;

Page 41: My sql presentation

Give user privilages for a db. Login as root. Switch to the MySQL db.

Grant privs. Update privs.

# mysql -u root -pmysql> use mysql;

mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,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;

Page 42: My sql presentation

To update info already in a table.

mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv =

'Y',Update_priv = 'Y' where [field name] = 'user';

Page 43: My sql presentation

Delete a row(s) from a table.

mysql> DELETE from [table name] where [field name] = 'whatever';

Page 44: My sql presentation

Update database permissions/privilages.

mysql> flush privileges;

Page 45: My sql presentation

Delete a column.

mysql> alter table [table name] drop column [column name];

Page 46: My sql presentation

Add a new column to db.

mysql> alter table [table name] add column [new column name] varchar (20);

Page 47: My sql presentation

Change column name.

mysql> alter table [table name] change [old column name] [new column name] varchar

(50);

Page 48: My sql presentation

Make a unique column so you get no dupes.

mysql> alter table [table name] add unique ([column name]);

Page 49: My sql presentation

Make a column bigger.

mysql> alter table [table name] modify [column name] VARCHAR(3);

Page 50: My sql presentation

Delete unique from table.

mysql> alter table [table name] drop index [colmn name];

Page 51: My sql presentation

Load a CSV file into a table.

mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE

[table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'

(field1,field2,field3);

Page 52: My sql presentation

Dump all databases for backup. Backup file is sql commands to recreate all

db's.

# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Page 53: My sql presentation

Dump one database for backup.

# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename

>/tmp/databasename.sql

Page 54: My sql presentation

Dump a table from a database.

# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename >

/tmp/databasename.tablename.sql

Page 55: My sql presentation

Restore database (or database table) from backup.

# [mysql dir]/bin/mysql -u username -ppassword databasename <

/tmp/databasename.sql

Page 56: My sql presentation

T H A N K Y O UT H A N K Y O U