• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SQL语句的基本操作

mysql 搞代码 4年前 (2022-01-09) 25次浏览 已收录 0个评论

–创建数据库create database Etp; –连接数据库connect to Etp; –断开连接disconnect Etp; –查看当前数据库下有哪些表list ta

–创建数据库
create database Etp;


–连接数据库
connect to Etp;


–断开连接
disconnect Etp;


–查看当前数据库下有哪些表
list tables;


–建表
create table studentInfo(
stuno char(5) not null,
stuname varchar(8),
stubirth date
);


–查看表结构
describe table studentinfo;


–新增表字段
alter table studentinfo add stutel int;
alter table studentinfo add abc int;


–修改字段类型
alter table studentinfo alter column stutel set data type char(11);


–删除字段
alter table studentinfo drop column abc;


–增加一个非空约束
alter table studentinfo alter column stuname set not null;

–重构表
reorg table studentinfo;

–增加一个唯一约束
alter table studentinfo alter column stutel set not null;
alter table studentinfo add本文来源gao*daima.com搞@代#码&网6 constraint un_stutel unique(stutel);

–添加检查约束
alter table studentinfo add column stuAge int;
alter table studentinfo add constraint ch_stuAge check(stuAge > 0 and stuAge <150);

–添加主键约束
alter table studentinfo add constraint pk_stuno primary key(stuno);

–删除表
drop table studentinfo;

–创建表的同时添加约束方式1
create table studentinfo(
stuNo int not null,
stuName varchar(8) not null,
stuAge int,
stuTel char(8),
constraint pk_stuNo primary key(stuNo),
constraint un_stuName unique(stuName),
constraint ch_stuAge check(stuAge >=0 and stuAge <150)
);

–创建表的同时添加约束方式2
create table studentinfo(
stuNo int not null primary key,
stuName varchar(8) not null unique,
stuAge int check(stuAge >=0 and stuAge <150),
stuTel char(8)
);

–添加主外键
–新增班级表
create table classInfo(
classId int not null primary key,
className varchar(20)
);


–建表的同时添加外键
create table studentinfo(
stuNo int not null,
stuName varchar(8) not null,
stuBirth date not null,
stuAge int,
stuTel char(8),
fclassId int,
stuBirth date not null,
constraint pk_stuNo primary key(stuNo),
constraint un_stuName unique(stuName),
constraint ch_stuAge check(stuAge >=0 and stuAge <150),
constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);


— 自增
create table studentinfo(
stuNo int not null generated always as identity(start with 1 ,increment by 1),
stuName varchar(8) not null,
stuAge int,
stuTel char(8),
fclassId int,
stuBirth date not null,
constraint pk_stuNo primary key(stuNo),
constraint un_stuName unique(stuName),
constraint ch_stuAge check(stuAge >=0 and stuAge <150),
constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SQL语句的基本操作
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址