Mysql: Dr. Hsiang-Fu Yu National Taipei University of Education Original by Qunfeng Dong
Mysql: Dr. Hsiang-Fu Yu National Taipei University of Education Original by Qunfeng Dong
Dr. Hsiang-Fu Yu
National Taipei University of
Education
Original by Qunfeng Dong
Database and Database
Management System
Database is simply a collection of data. In relational
database, data is organized into tables.
Student_ID Name Major Grade
101 Shannon BCB A
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Create User and Database
mysql>use mysql;
Use database mysql, used by the system
mysql>insert into user (Host, User,
Password) values (localhost, test1,
password(pass1));
Create a new database user test1
An alternative
GRANT USAGE ON *.* TO
test1@localhost IDENTIFIED BY pass1;
Create User and Database
(cont.)
mysql>insert into db (Host, Db, User, Select_priv,
Insert_priv, Update_priv, Delete_priv, Create_priv,
Drop_priv) values (localhost, testdb, test1, Y,
Y, Y, Y, Y, Y);
Create a new database testdb for user test1
mysql>flush privileges
Reloads the privileges from the grant tables in the
database mysql
An alternative
GRANT SELECT, INSERT, UPDATE, DELETE,
CREATE, DROP ON testdb.* TO test1@localhost
IDENTIFIED BY pass1;
Create Database
What are the current databases at the server?
mysql> show databases;
+--------------+
| Database |
+--------------+
| mysql | mysql is a database (stores users password ) used by system.
| test |
+--------------+
Create a database (make a directory) whose name is MyDB
mysql> create database MyDB;
Select database to use
mysql> use MyDB;
Database changed
What tables are currently stored in the MyDB database?
mysql> show tables;
Empty set (0.00 sec)
Create Table
CREATE TABLE Table_Name (column_specifications)
Example
mysql> CREATE TABLE student
-> (
-> student_ID INT UNSIGNED NOT NULL,
-> name VARCHAR(20) NOT NULL,
-> major VARCHAR(50),
-> grade VARCHAR(5)
-> );
Query OK, 0 rows affected (0.00 sec)
Example
mysql> SELECT major, grade FROM 102 Mike BBMB A
student WHERE name='Shannon';
+-------+-------+
| major| grade|
+-------+-------+ 103 Wang MCDB A
| BCB | A |
+-------+-------+
1 row in set (0.00 sec)
Logout MySQL
mysq> quit;
Buck Load
Load batch data instead of inserting records
one by one
Example