Mysql数据库练习

实验步骤:

练习1:

  1. 创建数据库test02_library(test修改为自己的姓名全拼)

CREATE DATABASE wuweixian02_company CHARACTER SET utf8mb4

USE wuweixian02_company;

2、创建表 books,表结构如下:

CREATE TABLE offices(

officCode INT PRIMARY KEY UNIQUE,

city VARCHAR(50) NOT NULL,

address VARCHAR(50) NOT NULL,

country VARCHAR(50) NOT NULL,

postalCode VARCHAR(15) UNIQUE

);

3.使用ALTER语句给books按如下要求增加相应的约束
(1)给id增加主键约束
(2)给id字段增加自增约束
(3)针对于非id的其他所有字段增加非空约束

ALTER TABLE books MODIFY name VARCHAR(50) NOT NULL;

ALTER TABLE books MODIFY authors VARCHAR(100) NOT NULL;

ALTER TABLE books MODIFY price FLOAT NOT NULL;

ALTER TABLE books MODIFY pubdate YEAR NOT NULL;

ALTER TABLE books MODIFY note VARCHAR(100) NOT NULL;

ALTER TABLE books MODIFY num INT NOT NULL;

练习2:

1.创建数据库test02_company(test修改为自己的姓名全拼)

2.按照下表给出的表结构在test02_company数据库中创建两个数据表offices和employees(test修改为自己的姓名全拼)

  1. 将表employees的mobile字段修改到officeCode字段后面

ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER officeCode ;

  1. 将表employees的birth字段改名为employee_birth

ALTER TABLE employees RENAME COLUMN birth TO employee_birth;

  1. 修改sex字段,数据类型为CHAR(1),非空约束

ALTER TABLE employees MODIFY sex char(1);

ALTER TABLE employees MODIFY sex char(1) NOT NULL;

  1. 删除字段note

ALTER TABLE employees DROP note;

  1. 增加字段名favoriate_activity,数据类型为VARCHAR(100)

ALTER TABLE employees ADD favoriate_activity VARCHAR(100) FIRST;

  1. 将表employees名称修改为employees_info

SHOW TABLES;

ALTER TABLE employees RENAME TO employees_info;

源码:

CREATE DATABASE wuweixian02_library CHARACTER SET utf8mb4
USE wuwexian02_library
CREATE TABLE books(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR( 50)NOt NULL UNIQUE,
authors VARCHAR(100)NOT NULL,
price FLOAT NOT NULL,
pubdate YEAR NOT NULL,
note VARCHAR(100) NOT NULL,
num INT NOT NULL
);
DESC books;
ALTER TABLE books ADD PRIMARY KEY(id);
CREATE DATABASE wuweixian02_company CHARACTER SET utf8mb4
USE wuweixian02_company;
CREATE TABLE offices(
officCode INT PRIMARY KEY UNIQUE,
city VARCHAR(50) NOT NULL,
address VARCHAR(50) NOT NULL,
country VARCHAR(50) NOT NULL,
postalCode VARCHAR(15) UNIQUE
);
ALTER TABLE offices ADD UNIQUE(postalCode);
ALTER TABLE offices ADD UNIQUE(officCode);
ALTER TABLE offices DROP index postalCode
DESC offices;
CREATE TABLE employees (
employeeNumber INT(11) PRIMARY KEY UNIQUE,
lastName VARCHAR(50) NOT NULL,
firstName VARCHAR(50) NOT NULL,
mobile VARCHAR(25) UNIQUE,
officeCode INT(10) NOT NULL,
jobTitle VARCHAR(50) NOT NULL,
birth DATETIME NOT NULL,
note VARCHAR(255),
sex VARCHAR(5)
);
DROP TABLE employees;
ALTER TABLE employees MODIFY lastName VARCHAR(50) NOT NULL;
DESC employees;
ALTER TABLE employees MODIFY mobile VARCHAR(25) AFTER officeCode ;
ALTER TABLE employees RENAME COLUMN birth TO employee_birth;
ALTER TABLE employees MODIFY sex char(1);
ALTER TABLE employees MODIFY  sex char(1) NOT NULL;
ALTER TABLE employees DROP note;
ALTER TABLE employees ADD favoriate_activity VARCHAR(100) FIRST;
SHOW TABLEs;
ALTER TABLE employees RENAME TO employees_info;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值