数据库的基本建表,本章涉及的关键字有【create(建),like(复制),rename(重命名),drop(删除)】
用户表account结构
属性名称 |
含义 |
数据类型 |
是否空 |
默认值 |
备注 |
userid |
用户编号 |
Char(6) |
Not null |
|
主键 |
Fullname |
用户名 |
Vachar(10) |
Not null |
|
|
Password |
密码 |
Varchar(20) |
null |
123456 |
|
Sex |
性别 |
Char(2) |
Not null |
|
|
address |
住址 |
Varchar(40) |
null |
|
|
|
邮箱 |
Varchar(20) |
null |
|
|
phone |
电话 |
Varchar(11) |
Not null |
|
|
商品分类表category结构
属性名称 |
含义 |
数据类型 |
是否空 |
默认值 |
备注 |
catid |
类别编号 |
Char(10) |
Not null |
|
主键 |
Catname |
分类名称 |
Varchar(20) |
Not null |
|
|
cades |
分类描叙 |
text |
null |
|
|
商品product结构
属性名称 |
含义 |
数据类型 |
是否空 |
默认值 |
备注 |
Productid |
商品编号 |
Char(10) |
Not null |
|
主键 |
catid |
类别编号 |
Char(10) |
Not null |
|
|
Name |
商品名 |
Varchar(30) |
Not null |
|
|
descn |
商品介绍 |
text |
null |
|
|
Listprice |
市场价格 |
Decimal(10,2) |
null |
|
|
unistcost |
成本价格 |
Decimal(10,2) |
null |
|
|
qty |
数量 |
Int(11) |
Not null |
|
|
订单表orders结构
属性名称 |
含义 |
数据类型 |
是否空 |
默认值 |
备注 |
Orderid |
订单号 |
Int(11) |
Not null |
|
主键,按订单生成顺序自增 |
Userid |
用户编号 |
Char(6) |
Not null |
|
|
orderdate |
订单日期 |
Dattetime |
Not null |
|
当前日期 |
Listprice |
商品价格 |
Decimal(10,2) |
null |
|
|
totalprice |
总价格 |
Decimal(10,2) |
null |
|
|
status |
订单状态 |
Tinyint(1) |
null |
|
|
订单明细表lineitem结构
含义 |
数据类型 |
是否空 |
默认值 |
备注 |
|
|
订单号 |
Int(11) |
Not null |
|
主键 |
|
|
商品编号 |
Char(10) |
Not null |
|
主键 |
|
|
数量 |
Int(11) |
Not null |
|
|
|
|
单价 |
Decimal(10,2) |
Not null |
|
|
|
|
1.用SQL语句创建数据库store,并设置默认字符集为gb2312,校对规则为gb2312_chinese_ci
mysql> create database store default charcter set gb2312 collate gb2312_chinese_ci;
2.用SQL语句按上表中的要求在store数据库中创建相应的表.
account表,
mysql>create table account (userid char(6) not null primary key,fullname varchar(10)not null,password varchar(20),sex char(2)not null,address varchar(40),email varchar(20),phone varchar(11) not null );
mysql>alter table account alter password set default "123456";
category表
mysql>create table category (catid char(10) not null primary key, catname varchar(20) not null, cades text);
product表
mysql>create table product(Productid Char(10) not null primary key,catid Char(10) Not null, Name Varchar(30) Not null,descn text,Listprice Decimal(10,2),unistcost Decimal(10,2),qty Int(11) Not null);
orders表
mysql>create table orders(Orderid Int(11) Not null primary key,Userid Char(6) Not null,orderdate datetime not null,Listprice Decimal(10,2),totalprice Decimal(10,2),status Tinyint(1));
mysql>alter table orders alter orderdate set default "2020.3.15";
lineitem表
mysql>create table lineitem (Orderid Int(11)Not null,itemid Char(10)Not null,Quanntity Int(11)Not null,unitprice Decimal(10,2)Not null);
mysql>alter table lineitem add primary key(Orderid,itemid);
3.复制用户表account,名称为account_copy,并将account_copy重新命名为account_old.
mysql>create table account_copy like account;
mysql>alter table account_copy rename account_old;
4.用SQL语句修改商品表中name字段的类型为varchar(50).
mysql>alter table product modify column name varchar(50);
5.重新命名商品表中name字段名为shopname.
mysql>alter table product change name shopname varchar(50);
6.用SQL语句删除用户表中的email字段.
mysql>alter table account drop email;
原文:https://www.cnblogs.com/beliss/p/12892418.html