一.sql 中增删改查
--插入数据
1.insert into table_name (column_list) values(column_value)//如果向表中所有列插入数据column_list 可以省略
--删除数据
2.delete table_name where column=''
--修改数据
3.update table_name or view_name set col_name=col_name+1//用set指定要跟新列名及更新值,若要修改多列中间用逗号隔开
--查询数据
4.select[all/distinct]/[top 10] (*/select_list) --all 代表所有行,distinct代表重复行只显示一次
from table_name --要查询的表名
where search_conditions --查询条件
group by [group_by_expression] --分组
having [search_conditions]-- 设定分组条件(此句针对 group by 语句)
order by order_expression [asc/desc]--升序或降序排列
二.约束
--添加主键: Alter table tabname add primary key(col)
--删除主键: Alter table tabname drop primary key(col)
--修改表增加reply列
alter table guestinfo
add
reply varchar(300)null
alter table table_name
add
column_name char(10) null
--修改字段名
Alter table table_name
Change column_name new column_ame varchr(20)b//未验证
--从表中删除一列
alter table table_name
drop column 列名
--将某一列修改为允许为空值
alter table table_name
alter table column 列名 null
--修改学分列的数据类型为int
alter table table_name
alter column 学分 int
--check 约束
alter table table_name
add
constraint 约束名称 check(字段名 between 0 and 100)
--性别检查约束
Sex char(2) check (Sex in('男','女'))
--修改表增加外键约束
alter table article
add
constraint 约束名称 foreign key(字段名)references 参照表(参照字段名)
--删除约束
alter table table_name
drop constraint 约束名
三.
select into 和 insert into select 两种表复制语句
Sql代码
select fld1, fld2
into destTbl
from srcTbl
insert into destTbl(fld1, fld2)
select fld1, 5 from srcTbl
以上两句都是将 srcTbl 的数据插入到 destTbl,但两句又有区别的。
第一句(select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建。
第二句(insert into select from)要求目标表(destTbl)存在,由于目标表已经存在,所以我们除了插入源表(srcTbl)的字段外,还可以插入常量,如例中的:5。