mysql8.0

1.创建数据库表

##这是一个单行注释
/*
多行注释
多行注释
多行注释
*/

/*
建立一张用来存储学生信息的表
字段包含学号、姓名、性别,年龄、入学日期、班级,email等信息
*/
-- 创建数据库表:
create table t_student(
	sno int(6), -- 6显示长度 
	sname varchar(5), -- 5个字符
	sex char(1),
	age int(3),
	enterdate date,
	classname varchar(10),
	email varchar(15)
);

-- 查看表的结构:展示表的字段详细信息
desc t_student;

-- 查看表中数据:
select * from t_student;

-- 查看建表语句:
show create table t_student;
/*
CREATE TABLE `t_student` (
  `sno` int DEFAULT NULL,
  `sname` varchar(5) DEFAULT NULL,
  `sex` char(1) DEFAULT NULL,
  `age` int DEFAULT NULL,
  `enterdate` date DEFAULT NULL,
  `classname` varchar(10) DEFAULT NULL,
  `email` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
*/

2.修改删除数据

-- 修改表中数据
update t_student set sex = '女' ;
update t_student set sex = '男' where sno = 10 ;
UPDATE T_STUDENT SET AGE = 21 WHERE SNO = 10;
update t_student set CLASSNAME = 'java01' where sno = 10 ;
update t_student set CLASSNAME = 'JAVA01' where sno = 9 ;
update t_student set age = 29 where classname = 'java01';
-- 删除操作:
delete from t_student where sno = 2;

3.修改删除数据库中的表

-- 查看数据:
select * from t_student;

-- 修改表的结构:
-- 增加一列:
alter table t_student add score double(5,2) ; -- 5:总位数  2:小数位数 
update t_student set score = 123.5678 where sno = 1 ;

-- 增加一列(放在最前面)
alter table t_student add score double(5,2) first;

-- 增加一列(放在sex列的后面)
alter table t_student add score double(5,2) after sex;

-- 删除一列:
alter table t_student drop score;

-- 修改一列:
alter table t_student modify score float(4,1); -- modify修改是列的类型的定义,但是不会改变列的名字
alter table t_student change score score1 double(5,1); -- change修改列名和列的类型的定义

-- 删除表:
drop table t_student;

4.外键、非外键约束

1.非外键约束

create table t_student(
        sno int(6) primary key auto_increment, 
        sname varchar(5) not null, 
        sex char(1) default '男' check(sex='男' || sex='女'),
        age int(3) check(age>=18 and age<=50),
        enterdate date,
        classname varchar(10),
        email varchar(15) unique
);
 
alter table t_student add constraint pk_stu primary key (sno) ; -- 主键约束
alter table t_student modify sno int(6) auto_increment; -- 修改自增条件
alter table t_student add constraint ck_stu_sex check (sex = '男' || sex = '女');
alter table t_student add constraint ck_stu_age check (age >= 18 and age <= 50);
alter table t_student add constraint uq_stu_email unique (email); 

2.外键约束

外键策略4种:no action、restrict(和no action一样,是默认策略)、set null、cascade

空、RESTRICT、NO ACTION

删除:从表记录不存在时,主表才可以删除,删除从表,主表不变。
更新:从表记录不存在时,主表菜可以更新,更新从表,主表不变。

CASCADE

删除:删除主表时自动删除从表。删除从表,主表不变。
更新:更新主表时自动更新从表。更新从表,主表不变。

ET NULL

删除:删除主表时自动更新从表为NULL,删除从表,主表不变。
更新:更新主表时自动更新从表值为NULL。更新从表,主表不变。

CREATE TABLE t_class (
	cno INT ( 4 ) PRIMARY KEY auto_increment,
	cname VARCHAR ( 10 ) NOT NULL,
room CHAR ( 4 ) 
);
CREATE TABLE t_student (
	sno INT ( 6 ) PRIMARY KEY auto_increment,
	sname VARCHAR ( 5 ) NOT NULL,
	classno INT ( 4 ),
	CONSTRAINT fk_stu_classno FOREIGN KEY ( classno ) REFERENCES t_class ( cno ) 
);
-- insert into t_class values (null,'1班',1)
-- insert into t_student values(null,'柽柳',1),(null,'王五',1)
alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno);
alter table t_student drop foreign key fk_stu_classno ;
alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno) on update cascade on delete cascade; -- 级联删除更新
alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class (cno) on update set null on delete set null; -- 置空删除更新

5.复制表或者复制表结构(快速添加)

-- 快速添加,结构跟t_student一致,数据没有:
create table t_student3
as
select * from t_student where 1=2;

select * from t_student3;

-- 快速添加:只要部分列,部分数据:
create table t_student4
as
select sno,sname,age from t_student where sno = 2;

select * from t_student4;

-- 删除数据操作 :清空数据
delete from t_student;
truncate table t_student;

原文地址:http://www.cnblogs.com/blanset/p/16907503.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性