SQL 建表与索引

创建表

  • varchar(max) 类型,最大容量2G的字符,只是直接用sql语句查询时会被截断不能完整显示
  • primary key (主属性)主键约束
  • check 属性的范围约束
  • unique 唯一性约束
create table User(
	userid	int	primary key not null,
	nick	varchar(64)	not null,
	age	int	not null	check(age >= 0 and age <= 120) default(0),
	sex	int	not null	check(sex >= 0) default(0)
)

修改表定义

alter	table <表名>
add	column	<列名>	<数据类型> [约束]			--添加列
drop	column	<列名>	 [ cascade | restrict ]		--删除列
drop constraint	<约束名称>	 [ cascade | restrict ]	--删除约束
alter	column	<列名>	<数据类型>	--修改列

索引

  • 创建索引提高查询效率,同时会降低添加,修改,删除的效率。
create [unique] [cluster] index  index_name on User age asc    --创建索引升序
create [unique] [cluster] index  index_name on User age desc  --创建索引降序
create [unique] [cluster] index  index_name on User name asc, age desc --创建索引
drop index index_name --删除指定名称的索引
alter index index_name rename to index_name2 --修改索引名称
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值