(1)对照SQL DDL语句的语法,选择使用各种参数;
(2)使用SQL语句创建、修改和删除数据库和基本表;
(3)总结SQL语句常见语法错误的调试方法。
操作系统:Windows 11
应用软件:MySQL Navicat
利用SQL语句完成以下操作
1.创建数据库student_course+学号后两位,该数据库用于网上商店管理系统的后台数据库;
2.创建会员表(members),具体要求如下:
字段名
数据类型
长度
是否可为空
约束
M_account(会员号)
varchar
20
否
主键
M_name(会员姓名)
varchar
20
否
M_birth(会员出生日期)
date
是
M_sex(会员性别)
char
2
是
M_address(会员住址)
varchar
50
是
M_salary(会员工资水平)
decimal
7
否
M_password(密码)
Varchar
20
否
3.创建产品表(products),具体要求如
字段名
数据类型
长度
是否可为空
约束
P_no(产品号)
varchar
20
否
主键
P_name(产品名)
varchar
50
否
P_date(产品生产日期)
datetime
否
P_quantity(产品总量)
int
否
P_price(产品单价)
double
否
P_information(产品信息)
varchar
500
否
4.创建订单表(orders),具体要求如下:
字段名
数据类型
长度
是否可为空
约束
M_account(会员号)
varchar
20
否
主键
主键
P_no(产品号)
varchar
20
否
O_date(定货日期)
datetime
否
O_quantity(订单量)
Int
否
O_ confirm_state(确认状态)
bit
否
O_pay_state(支付状态)
bit
否
O_send_state(发送状态)
bit
否
5.根据刚才创建的表,使用可视化管理工具和SQL语句为每个表添加以下数据。
members表:
products表:
orders表:
1.创建student_course75数据库:
CREATE DATABASE student_course75; -- 创建数据库
USE student_course75; -- 使用此数据库
2.创建三张表:
CREATE TABLE members(
M_account VARCHAR(20) PRIMARY KEY,
M_name VARCHAR(20) NOT NULL,
M_birth date,
M_sex char(2),
M_address VARCHAR(50),
M_salary DECIMAL(7,1) NOT NULL,
M_password VARCHAR(20) NOT NULL);
CREATE TABLE products(
P_no VARCHAR(20) PRIMARY KEY,
P_name VARCHAR(50) NOT NULL,
P_date datetime NOT NULL,
P_quantity int NOT NULL,
P_price DOUBLE NOT NULL,
P_information VARCHAR(500) NOT NULL);
CREATE TABLE orders(
M_account VARCHAR(20),
P_no VARCHAR(20),
O_quantity INT NOT NULL,
O_date datetime,
O_confirm_state BIT NOT NULL,
O_pay_state BIT NOT NULL,
O_send_state BIT NOT NULL,
PRIMARY KEY(M_account,P_no,O_date),
FOREIGN KEY (M_account) REFERENCES members(M_account),
FOREIGN KEY (P_no) REFERENCES products(P_no));
3.为三张表添加数据:
INSERT INTO members VALUES ('liuzc','刘志成','1972-05-18','男','湖南株洲','3500.0','liuzc518');
INSERT INTO members VALUES ('zhao888','赵爱云','1972-02-12','男','湖南株洲','5500.0','zhao888');
INSERT INTO members VALUES ('wangym','王咏梅','1974-08-06','女','湖南长沙','4000.0','wangym0806');
INSERT INTO members VALUES ('jinjin','津津有味','1982-04-14','女','北京市','8200.0','jinjin');
INSERT INTO members VALUES ('lfz','刘法治','1976-08-26','男','天津市','4500.0','lfz0826');
INSERT INTO members VALUES ('zhangzl','张自梁','1975-04-10','男','湖南株洲','4300.0','zhangzl');
INSERT INTO products VALUES ('0140810324','清华同方电脑','2005-12-11 00:00:00.000',7,'8000.00','优惠多多');
INSERT INTO products VALUES ('0140810330','洗衣粉','2005-05-31 00:00:00.000',1000,'8.60','特价销售');
INSERT INTO products VALUES ('0140810332','红彤彤腊肉','2005-05-20 00:00:00.000',43,'15.00','是一种卫生食品');
INSERT INTO products VALUES ('0140810333','力士牌香皂','2005-05-06 00:00:00.000',22,'6.00','是一种清洁用品');
INSERT INTO products VALUES ('0240810330','电动自行车','2005-05-31 00:00:00.000',10,'1586.00','价廉物美');
INSERT INTO products VALUES ('0240810333','自行车','2005-05-31 00:00:00.000',10,'586.00','价廉物美');
INSERT INTO products VALUES ('0910810001','爱国者MP3','2005-05-31 00:00:00.000',100,'450.00','价廉物美');
INSERT INTO products VALUES ('0910810002','商务通','2005-05-20 00:00:00.000',10,'850.00','价廉物美');
INSERT INTO products VALUES ('0910810003','名人好记星','2005-05-31 00:00:00.000',100,'550.00','价廉物美');
INSERT INTO products VALUES ('0910810004','奥美嘉U盘','2005-05-31 00:00:00.000',100,'350.00','价廉物美');
INSERT INTO orders VALUES ('liuzc','0140810324',1,'2005-10-09 00:00:00.000',0,0,0);
INSERT INTO orders VALUES ('liuzc','0910810004',2,'2005-10-09 00:00:00.000',1,1,0);
INSERT INTO orders VALUES ('liuzc','0910810001',1,'2005-10-09 00:00:00.000',1,1,0);
INSERT INTO orders VALUES ('wangym','0910810001',1,'2005-08-09 00:00:00.000',1,0,0);
INSERT INTO orders VALUES ('jinjin','0910810004',1,'2005-08-09 00:00:00.000',1,1,1);
INSERT INTO orders VALUES ('lfz','0910810001',1,'2005-08-09 00:00:00.000',0,0,0);
INSERT INTO orders VALUES ('lfz','0910810004',2,'2005-08-09 00:00:00.000',1,1,1);
INSERT INTO orders VALUES ('lfz','0910810004',2,'2005-06-06 00:00:00.000',1,1,1);
INSERT INTO orders VALUES ('jinjin','0910810004',2,'2005-06-06 00:00:00.000',1,0,0);
INSERT INTO orders VALUES ('zhao888','0240810333',2,'2005-06-06 00:00:00.000',1,1,0);
members表
orders表