There are two types of MySQL instructions
(Data Definition Language) DDL:Create database, create table, alter table ,,, .
(Data Manipulation Language) DMLReplace , delete, insert, update , select ,,, .
Review• show databases;• create database New_database;• use new_database;• show tables;• create table <table_name> (column_name data_type [not
null] … ,));• describe <table_name>;• INSERT INTO table_name SET col_name3=value3, …;• SELECT col_name1 FROM table_name;• SELECT * FROM table_name;
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 MyDBmysql> create database MyDB;Select database to use mysql> use MyDB;Database changedWhat tables are currently stored in the MyDB database? mysql> show tables;Empty set (0.00 sec)
Create Database
• CREATE TABLE Table_Name (column_specifications)• Examplemysql> 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)
Student_ID Name Major Grade
Create Table
mysql> show tables;+--------------------+| Tables_in_MyDB |+--------------------+| student |+--------------------+1 row in set (0.00 sec)mysql> describe student;+---------------+----------------------+------+------+----------+--------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+-----+-----------+-------+| student_ID | int(10) unsigned | | | 0 | || name | varchar(20) | | | | || major | varchar(50) | YES | | NULL | || grade | varchar(5) | YES | | NULL | |+---------------+----------------------+-------+------+----------+-------+4 rows in set (0.00 sec)
Display Table Structure
use mysql;show tables;describe db;
• ALTER TABLE table_name Operations
mysql> alter table student add primary key (student_ID);Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;+---------------+--------------------- +-------+------+----------+-------+| Field | Type | Null | Key | Default | Extra |+---------------+----------------------+-------+------+----------+-------+| student_ID | int(10) unsigned | | PRI | 0 | || name | varchar(20) | | | | || major | varchar(10) | YES | | NULL | || grade | varchar(5) | YES | | NULL | |+---------------+----------------------+-------+------+-----------+-------+4 rows in set (0.00 sec)
Modify Table Structure
Alter
Alter table table_nameAdd column_name VARCHAR(20) NOT NULL;
Alter table table_nameChange column_name VARCHAR(20) NOT NULL;
Lab Exercise create database school;
use school;
create table students(s_name varchar(15) not null,s_numbers varchar(4) not null,
s_teachers char(15);)
create table teachers(t_names varchar(15) not null,t_numbers varchar(4) not null,t_students varchar(15) not null;)
alter table studentsadd primary key (s_numbers);
alter table teachersadd primary key (t_numbers);
alter table studentsmodify s_teachers varchar(4) not null;
alter table studentsadd foreign key(s_teachers) references teachers(t_numbers);
alter table studentschange s_name s_names varchar(15) not null;
describe students;describe teachers;
alter table teachersdrop t_students;describe teachers;
Alter with (add) operation
When we write : create table students(s_name varchar(15) not null,
s_numbers varchar(4) not null;) alter table students add primary key (s_numbers);
It’s the same as we write: create table students(s_name varchar(15) not null,s_numbers varchar(4) not null,
primary key (s_numbers);)
To add into the tables
select * from students;select * from teachers;
I want a table of students and
their teachers
select s_names, s_numbers, t_names , t_numbersfrom students, teacherswhere s_teachers = t_numbers;
OK, now I want a table of Kmal
students
select s_names, s_numbersfrom studentswhere s_teachers = '21';
select s_names, s_numbersfrom studentswhere s_teachers ='21'order by s_names;
References
• Dr. Hsiang-Fu Yu, National Taipei University of Education