Hello MQSQL 2

添加唯一 约束,只能是唯一的

CREATE TABLE user(
	ID CHAR(16) PRIMARY KEY NOT NULL,
	NICK CHAR(10),
	EMAIL CHAR(50) NOT NULL UNIQUE,
	FIRSTNAME CHAR(10),
	LASTNAME CHAR(10)
#	UNIQUE(EMAIL) 这么添加也可以
)

添加约束性别是 ‘男’ Or ‘女’

ALTER TABLE `user` 
ADD SEX CHAR(2) NULL CHECK(SEX = '男' Or SEX = '女')

CONCAT字符串拼接
CONCAT(u.NICK,’-都我的’) AS 店主

SELECT u.ID,CONCAT(u.NICK,'-都我的') AS 店主,s.NAME FROM `user` u JOIN store s
ON u.ID = s.OWNER 
#GROUP BY u.ID
ORDER BY u.ID

在这里插入图片描述
随机数

RAND()

在这里插入图片描述
数字转字符串,字符串截取

RIGHT(cast(RAND() AS char(16)),14) AS 随机ID
#Or
(SUBSTRING(cast(RAND() AS char(18)),3,16))

#查询user有几个store(关闭MYSQL严格模式,严格模式不会用,待加强)

SELECT 
u.ID,u.NICK ,
COUNT(*) AS 拥有store数量
FROM `user` u JOIN store s 
ON u.ID = s.OWNER 
GROUP BY s.OWNER 

在这里插入图片描述
修改product table column ID 为 default random string

ALTER TABLE product
MODIFY COLUMN ID CHAR(16) NOT NULL DEFAULT (SUBSTRING(cast(RAND() AS char(18)),3,16))

更新ID为‘4’的元组NAME属性在store表中

UPDATE store 
set NAME = '阳光八百超市'
WHERE ID = '4'

猜你喜欢

转载自blog.csdn.net/printf_hello/article/details/114782666