SQL数据库建表、表中列的常见类型与一些常用约束的操作
来源:互联网 发布:个国家域名后缀 编辑:程序博客网 时间:2024/06/13 06:32
硬是一拖再拖,直到了今天才更。
--表中列的类型
-- int 整型-- char(10) 字符串 固定长度查询速度快,浪费空间如身份证号 手机号码 学号
-- varchar(10) 字符串可变长度姓名 地址
-- 不带n的:字符或者数字占一个字节,汉字为两个字节
-- 带n的:双字节存储,字母或者数字、汉字两个字节
-- nchar(10)
-- nvarchar(10)
-- 存图片时,图片上传到服务器上,在数据库只存路径
-- 建表语句
use studentmanager
if exists(select *from sysobjects where name='table_student')
drop table table_student
create table table_student
(
-- 字段名 字段的特征
id int identity(1,1) primary key,--在创建表时就设定为主键列与自增列
stu_number char(12) not null,--不允许为空
stu_name varchar(20) not null,
stu_age int,
birthday datetime,
class_id int references table_class(class_id) --外键
)
create table table_class
(
class_id int identity(1,1) primary key,
class_name varchar(20) not null,
create_time datetime
)
-- 约束的目的 确保表中数据的完整性
-- 行完整性(实体完整性):主键约束与唯一约束
-- 列完整性(域完整性) :检查约束、默认约束与非空约束
-- 参照完整性 :外键约束
use studentmanager
drop table table_class
drop table table_student
if exists(select *from sysobjects where name='table_student')
drop table table_student
create table table_student
(
-- 字段名 字段的特征
id int not null,
stu_number char(12),
stu_name varchar(20) ,
stu_age int,
birthday datetime,
class_id int
)
create table table_class
(
class_id int not null,
class_name varchar(20),
create_time datetime
)
-- 追加约束
-- 主键约束(向哪个表 加什么约束 加在哪个列上面) 非空唯一
alter table table_student
add constraint PK_id primary key(id)
alter table table_class
add constraint PK_class_id primary key(class_id)
-- 唯一约束 可以为空 但唯一 只允许一个为空
alter table table_student
add constraint UK_stu_number unique(stu_number)
--检查约束
alter table table_student
add constraint CK_stu_age check(stu_age>0 and stu_age<150)
--默认约束
alter table table_class
add constraint DF_create_time default(getdate()) for create_time
--外键约束
alter table table_student
add constraint FK_class_id foreign key (class_id) references table_class(class_id)
--建表时直接加约束
--create table table_student
--(
-- id int identity(1,1) primary key,
-- stu_number char(12) unique not null,
-- stu_name varchar(20) not null,
-- stu_age int check(stu_age>0 and stu_age<150),
-- birthday datetime default(getdate()),
-- class_id int references table_class(class_id)
-- )
--删除约束
alter table table_student
drop constraint UK_stu_number
--------携着一股什么也不服的劲在活着
阅读全文
0 0
- SQL数据库建表、表中列的常见类型与一些常用约束的操作
- sql sever数据库与oracle数据库的一些常用操作
- 数据库---常见约束的SQL语句的研究与使用
- ]常见数据库字段类型与java.sql.Types的对应
- 常见数据库字段类型与java.sql.Types的对应
- 常见数据库字段类型与java.sql.Types的对应
- 数据库建表的一些操作,包括主键外键约束等等
- SQL数据库的一些操作
- SQL数据库中 一些常使用的约束 排序 系统函数的用法 与联合主键
- SQL数据库的常用操作
- SQL 的常用更改表约束
- 数据库常用的SQL 语句(一)创建数据库,表以及添加约束
- Oracle数据库Constraint约束的常用操作及异常处理
- Oracle数据库Constraint约束的常用操作及异常处理
- Oracle表约束的sql操作
- SQL Server数据库表的六大约束
- MySQL数据库(操作表的约束)
- Oracle数据库之操作表的约束
- HDUOJ 1045 Fire Net
- java 小常识
- 《机器学习实战》学习笔记-[7]-利用AdaBoot元提高分类性能
- CSS之解决高度塌陷问题(三)
- 通过匿名内部类创建对象
- SQL数据库建表、表中列的常见类型与一些常用约束的操作
- java异常初解
- maven打包可运行的fat-jar的简单方法
- Vue, App与我(四)
- retrofit 2.0 如何通过 HttpLoggingInterceptor 打印服务器响应的 json ?
- 重发:“设计师的实战过程”下载
- XML文档的语法规则(儿歌)
- POJ 3481 Double Queue(STL之双向优先队列)
- HashTable与HashMap的区别