首页 / MYSQL / SQL语句的基本操作
SQL语句的基本操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SQL语句的基本操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2559字,纯文字阅读大概需要4分钟。
内容图文
![SQL语句的基本操作](/upload/InfoBanner/zyjiaocheng/551/a5a0e90ec2af4d9e923b6ca8b05b1e26.jpg)
--创建数据库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 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)
);
内容总结
以上是互联网集市为您收集整理的SQL语句的基本操作全部内容,希望文章能够帮你解决SQL语句的基本操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。