SQL server2016 数据表操作(增、删、改、查)

创建数据表:
表名、字段名都用英文 字段含义用中文
主键不能为空,也不能重复。外键的数据类型及长度必须与逐渐的类型及长度一致

	use 表名	//打开表,表示再该表中创建内容
	create table 表名
	(字段名1  类型(长度)  constraint   主键名  primary key,	//主键名通常格式为:pk_名字。constrant表示约束
	字段2   类型(长度)not null,
	字段3   类型(长度)  constraint  外键名  foreign key references  引用表名(引用字段名)	//外键:    外键名通常格式为:fk_名字。references表示引用
)

查看表数据:

	select  *  from 表名

删除表:

	drop table 表名

修改表:

	alter table 表名
	add constraint 约束名  约束类型 (列名) 引用	//添加一个约束,

	eg:
	alter table students
	add constraint 
	fk_student_class foreign key  classno 
	references class(classno)

SQL约束:

主键(primary key)
外键(foreign key)
非空(not null)
默认(default)
检查(check)
唯一(unique)

  • 默认值(约束):
	eg:
	alter table teacher
	add constraint 
	df_prof default('讲师') 
	for prof
  • 删除默认值:
	alter table teacher
	drop constraint df_prof default
  • 检查(约束):
	alter table teacher
	add constraint 
	ck_tsex check
	(tsex = '男') 
	//(tsex in ('女','男'))
  • 唯一约束(unique):
	alter table  表名    
	add constrint 
	索引名称 unique(字段名)

	//eg:
	alter table dept
	add constraint 
	ix_deptname unique(deptname)	

数据的操作:

  • 操作语句
	insert update  delete 	//添加修改删除
  • 添加数据:
	insert into 表名 【(字段1,字段2,……) 】
	valuses 
	(表达式1,表达式2,……)【,】
	【(表达式1,表达式2,……)*:字段数与表达式的是一一对应,切值的顺序哟啊对应字段的顺序
  • 删除数据:
	delete from 表名 where 表达式

猜你喜欢

转载自blog.csdn.net/weixin_46622106/article/details/111242263