+ All Categories
Home > Documents > There are two types of MySQL instructions

There are two types of MySQL instructions

Date post: 10-Feb-2016
Category:
Upload: mostyn
View: 28 times
Download: 0 times
Share this document with a friend
Description:
There are two types of MySQL instructions. (Data Definition Language) DDL: Create database, create table, alter table ,,, . (Data Manipulation Language) DML Replace , delete, insert, update , select ,,,. Review. show databases; create database New_database ; use new_database ; - PowerPoint PPT Presentation
21
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table ,,, . (Data Manipulation Language) DML Replace , delete, insert, update , select ,,, .
Transcript
Page 1: There are two types of MySQL instructions

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 ,,, .

Page 2: There are two types of MySQL instructions

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;

Page 3: There are two types of MySQL instructions

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

Page 4: There are two types of MySQL instructions

• 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

Page 5: There are two types of MySQL instructions

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

Page 6: There are two types of MySQL instructions

use mysql;show tables;describe db;

Page 7: There are two types of MySQL instructions

• 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

Page 8: There are two types of MySQL instructions

Alter

Alter table table_nameAdd column_name VARCHAR(20) NOT NULL;

Alter table table_nameChange column_name VARCHAR(20) NOT NULL;

Page 9: There are two types of MySQL instructions

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;

Page 10: There are two types of MySQL instructions
Page 11: There are two types of MySQL instructions

describe students;describe teachers;

Page 12: There are two types of MySQL instructions

alter table teachersdrop t_students;describe teachers;

Page 13: There are two types of MySQL instructions

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);)

Page 14: There are two types of MySQL instructions

To add into the tables

Page 15: There are two types of MySQL instructions

select * from students;select * from teachers;

Page 16: There are two types of MySQL instructions

I want a table of students and

their teachers

Page 17: There are two types of MySQL instructions

select s_names, s_numbers, t_names , t_numbersfrom students, teacherswhere s_teachers = t_numbers;

Page 18: There are two types of MySQL instructions

OK, now I want a table of Kmal

students

Page 19: There are two types of MySQL instructions

select s_names, s_numbersfrom studentswhere s_teachers = '21';

Page 20: There are two types of MySQL instructions

select s_names, s_numbersfrom studentswhere s_teachers ='21'order by s_names;

Page 21: There are two types of MySQL instructions

References

• Dr. Hsiang-Fu Yu, National Taipei University of Education


Recommended