04-SQL Data Management

27
 4. SQL Data Management

description

SQL Data ManagementLPIC 1 - Linux Professional Institute

Transcript of 04-SQL Data Management

  • 4. SQL Data Management

  • Structured Query Language

    Used for retrieving/modifying data stored in a database

    ANSI Standard but the devils in the details (proprietary

    extensions)

  • MySQL Owned by Oracle, released under GPL

    Also has a commercial version (Enterprise Server)

    Used by Wikipedia, Google (for AdWords), and Facebook

    PostgreSQL Open-source, MIT-style license

    Community developed

    Used by Yahoo (2PB of data!), MySpace, hi5, reddit

    SQLite Library, not standalone DBMS

  • Information is stored in tables: Rows are objects (items)

    Columns are attributes

    Name Color Size Price

    1 Mouse Blue 8x12cm $10

    2 3G Modem White 4x8cm $50

    3 Presenter Black 3x10cm $70

    4 Phone Red 5x10cm $600

  • INTEGER (INT) 4-byte integer DECIMAL, NUMERIC exact precision (fixed-

    point) numbers FLOAT, DOUBLE floating-point numbers DATETIME date and time CHAR fixed maximum length string VARCHAR variable length string ENUM enumerated list SET a set of zero or more values

  • Depending on the installation, you may need to authenticate The MySQL root user is different from the Linux root

    user, and has a different password! (set at installation time)

    MySQL commands are case insensitive

    $ mysql

    Welcome to the MySQL monitor. Commands end with ; or \g.

    []

    mysql>

  • mysql> show databases;

    +--------------------+

    | Database |

    +--------------------+

    | mysql |

    | wordpress |

    +--------------------+

    2 rows in set (0.00 sec)

    mysql> use mysql;

    []

    Database changed

  • mysql> show tables;

    +---------------------------+

    | Tables_in_mysql |

    +---------------------------+

    | columns_priv |

    | db |

    | func |

    []

    | time_zone_name |

    | time_zone_transition |

    | time_zone_transition_type |

    | user |

    +---------------------------+

    17 rows in set (0.00 sec)

  • mysql> describe db;

    +-----------------------+---------------+------+-----+---------+-------+

    | Field | Type | Null | Key | Default | Extra |

    +-----------------------+---------------+------+-----+---------+-------+

    | Host | char(60) | NO | PRI | | |

    | Db | char(64) | NO | PRI | | |

    | User | char(16) | NO | PRI | | |

    | Select_priv | enum('N','Y') | NO | | N | |

    | Insert_priv | enum('N','Y') | NO | | N | |

    [] | Execute_priv | enum('N','Y') | NO | | N | |

    | Event_priv | enum('N','Y') | NO | | N | |

    | Trigger_priv | enum('N','Y') | NO | | N | |

    +-----------------------+---------------+------+-----+---------+-------+

    22 rows in set (0.00 sec)

  • mysql> CREATE DATABASE test;

    Query OK, 1 row affected (0.05 sec)

    mysql> USE test;

    Database changed

    mysql> CREATE TABLE items (name VARCHAR(20), price DECIMAL(5,2));

    Query OK, 0 rows affected (0.02 sec)

    mysql> SHOW TABLES;

    +----------------+

    | Tables_in_test |

    +----------------+

    | items |

    +----------------+

    1 row in set (0.00 sec)

  • mysql> INSERT INTO items VALUES ('3G Modem', '50');

    Query OK, 1 row affected (0.00 sec)

    mysql> SELECT * FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 50.00 |

    +----------+-------+

    1 row in set (0.00 sec)

  • mysql> UPDATE items SET price='49.99' WHERE name= '3G Modem';

    Query OK, 1 row affected (0.00 sec)

    Rows matched: 1 Changed: 1 Warnings: 0

    mysql> SELECT * FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    +----------+-------+

    1 row in set (0.00 sec)

  • mysql> SELECT * FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    +----------+-------+

    1 row in set (0.00 sec)

    mysql> SELECT name, price FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    +----------+-------+

    1 row in set (0.00 sec)

  • mysql> SELECT * FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    | Mouse | 3.99 |

    +----------+-------+

    2 rows in set (0.00 sec)

    mysql> SELECT * FROM items WHERE price>10;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    +----------+-------+

    1 row in set (0.00 sec)

  • mysql> SELECT items.name, items.price,

    stocks.quantity

    -> FROM items, stocks

    -> WHERE items.name=stocks.name;

    +----------+-------+----------+

    | name | price | quantity |

    +----------+-------+----------+

    | 3G Modem | 49.99 | 10 |

    | Mouse | 3.99 | 50 |

    +----------+-------+----------+

    2 rows in set (0.00 sec)

  • mysql> SELECT items.name, items.price, stocks.quantity

    -> FROM items

    -> JOIN stocks

    -> ON items.name=stocks.name;

    +----------+-------+----------+

    | name | price | quantity |

    +----------+-------+----------+

    | 3G Modem | 49.99 | 10 |

    | Mouse | 3.99 | 50 |

    +----------+-------+----------+

    2 rows in set (0.00 sec)

  • mysql> DELETE FROM items WHERE price SELECT * FROM items;

    +----------+-------+

    | name | price |

    +----------+-------+

    | 3G Modem | 49.99 |

    +----------+-------+

    1 row in set (0.00 sec)

  • mysql> DROP TABLE items;

    Query OK, 0 rows affected (0.00 sec)

    mysql> SHOW tables;

    +----------------+

    | Tables_in_test |

    +----------------+

    | stocks |

    +----------------+

    1 row in set (0.00 sec)

    mysql> DROP DATABASE test;

    Query OK, 1 row affected (0.04 sec)

    mysql> SHOW DATABASES;

    +--------------------+

    | Database |

    +--------------------+

    | mysql |

    | wordpress |

    +--------------------+

    2 rows in set (0.00 sec)

  • MySQL users and passwords

    Granting privileges

    Backing up and restoring a database

  • Changing the root password:

    user@vm-ubuntu:~$ mysqladmin -u root -p

    password '123'

    Enter password:

    user@vm-ubuntu:~$

  • Creating a user:

    user@vm-ubuntu:~$ mysql -u root -p

    Enter password:

    Welcome to the MySQL monitor. Commands end with ; or \g.

    mysql> CREATE USER "user2"@"localhost" IDENTIFIED BY

    "pass2";

    Query OK, 0 rows affected (0.00 sec)

  • Granting permissions:

    mysql> GRANT select,update,insert on mysql.* TO user2@localhost;

    Query OK, 0 rows affected (0.00 sec)

    mysql> GRANT ALL PRIVILEGES on wordpress.* TO user2@localhost;

    Query OK, 0 rows affected (0.00 sec)

  • user@vm-ubuntu:~$ mysqldump -u root -p mysql >

    mysql.backup.sql

    Enter password:

    user@vm-ubuntu:~$

  • user@vm-ubuntu:~$ mysql -u root -p mysql <

    mysql.backup.sql

    Enter password:

    user@vm-ubuntu:~$