Thực hành MySQL cơ bản – Part 1

Thực hành MySQL cơ bản theo giáo trình “BÀI GIẢNG THỰC HÀNH CƠ SỞ DỮ LIỆU”

<link tham khảo giáo trình: cuối bài>

BÀI GIẢNG THỰC HÀNH CƠ SỞ DỮ LIỆU” là giáo trình thực hành của Đại học Công Nghệ (UET)

Việc sử dụng MySQL Workbech hoặc MySQL Shell để quản lí database rất tiện lợi và cần thiết cho hầu hết những người mới bắt đầu sử dụng và thực hành với MySQL

Bài thực hành 1: chủ yếu thao tác trên mySQL Workbench nên khá khó trong việc mô tả bằng văn bản. Mọi người có thể tham khảo trên youtube hoặc…

Bài thực hành 2:

tạo cơ sở dữ liệu my_groupicmodels;
CREATE TABLE ‘my_classicmodels`.`productlines` (
‘ productLine` VARCHAR (50) NOT NULL,
‘textDescription` VARCHAR (4000) NULL,
‘ htmlDescription` MEDIUMTEXT NULL,
‘image` MEDIUMBLOB NULL,
PRIMARY KEY ( `productLine`));

TẠO BẢNG `my_groupicmodels`.`products` (
` productCode` VARCHAR (15) KHÔNG NULL,
`productName` VARCHAR (70) KHÔNG NULL,
` productLine` VARCHAR (50) KHÔNG NULL,
` ProductScale` VARCHAR (50)
‘productVendor` VARCHAR (50) NOT NULL,
‘ productDesciption` TEXT NOT NULL,
‘quanlityInStock` SMALLINT NOT NULL,
‘ buyPrice` DOUBLE NOT NULL,
CONSTRAINT ‘productLine`
FOREIGN KEY ( `productLine`)
Tài liệu tham khảo’ my_classicmodels`.`productlines` (`ProductLine`)
TRÊN CẬP NHẬT CASCADE,
KEY PRIMARY (` productCode`));
TẠO BẢNG `
my_groupicmodels`.`nings` ( ` oderNumber` INT (11) KHÔNG NULL AUTO_INCREMENT,
`orderDate` DATETIME KHÔNG NULL,
`requiredDate` DATETIME KHÔNG NULL,
` shipDate` DATETIME NULL,
`status` VARCHAR (15) KHÔNG NULL, ` comment`
TEXT NULL,
`customerNumber` INT (11) KHÔNG NULL,
PRIMARY KEY (` oderNumber`));
TẠO BẢNG `my_groupicmodels`.`orderdetails` (
` orderNumber` INT (11) KHÔNG NULL AUTO_INCREMENT,
`ProductCode` VARCHAR (15
) KHÔNG NULL,
` quantOrdered` INT (11) KHÔNG NULL,
`priceEach` `SMALLINT (6) KHÔNG NULL,
CONSTRAINT` ProductCode`
FOREIGN KEY (` ProductCode`)
TÀI LIỆU THAM KHẢO` my_groupicmodels`.`products` (`ProductCode`)
TRÊN CẬP NHẬT CASCADE,
CONSTRAINT`
orderNumber
TÀI LIỆU THAM KHẢO `my_groupicmodels`.`nings` (` oderNumber`)
TRÊN CẬP NHẬT CASCADE,
KEY PRIMARY (`orderNumber`,` productCode`));

Bài thực hành 3:

sử dụng các mô hình cổ điển;
chọn * từ nhân viên nơi báo cáoTo là null;
chọn số lượng khách hàng khác nhau từ các khoản thanh toán;
chọn * từ các đơn hàng trong đó requiredDate = “2003-01-18”;
chọn * từ các đơn hàng trong đó orderDate> = “2005-04-01” && orderDate <= “2005-04-30” && status = “Shipped”;
CHỌN * TỪ classicmodels.products trong đó sản phẩmLine = “Xe cổ điển”;

Bài thực hành 4:

use classicmodels;
select * from customers where city in(“Nantes”, “Lyon”);
SELECT * FROM classicmodels.orders where shippedDate between “2003-01-10” AND “2003-03-10”;
SELECT * FROM classicmodels.products where productLine LIKE ‘%CARS%’;
SELECT * FROM classicmodels.products order by quantityInStock desc limit 10;
alter table classicmodels.products ADD column inventoryMoney int(10) ;
select * from classicmodels.products;

Bài thực hành 5:

use classicmodels;
#Câu 1
select substring(productDescription, 1, 50 ) as titleProducts from products;
#Câu 2
SELECT concat(firstName,” “, lastName,”, “, jobTitle) as employeesDescription FROM classicmodels.employees;
#Câu 3
SET SQL_SAFE_UPDATES = 0;
SET foreign_key_checks = 0;
use classicmodels;
update products set productLine = replace(productLine, “Cars”, “Automobiles” ) ;
update productlines set productLine = replace(productLine, “Cars”, “Automobiles” ) ;
SET foreign_key_checks = 1;
select * from productlines;
#Câu 4
CHỌN orderNumber, dateiff (shipDate, orderDate) dưới dạng vận chuyển Thời gian TỪ classicmodels.nings trong đó shipDate không phải là thứ tự null bởi shipTime asc giới hạn 5;
# Câu 5
CHỌN * TỪ classicmodels.nings trong đó shipDate là null và orderDate trong khoảng từ ‘2005-05-01’ và ‘2005-05-31’;

Còn tiếp….

< Link tham khảo: Tại đây , link không được upload bởi TeamVietDev có thể die bất kì lúc nào =)), bài viết mang tính chất tham khảo>

Nguồn: -S-TeaM.

Bình luận