你的位置:首页 > 操作系统

[操作系统]SQLSERVER基础语句(一)

1、插入一行数据:
INSERT INTO 表名(列名)VALUES(对应的值);
2、一次性插入多条数据
先建表:
INSERT INTO 新建表名(列表)SELECT 原始表列名 FROM 原始表;
执行时创建表:
SELECT 表名.列名 INTO 新建表名 FROM 原始表;
新建标识列:
SELECT Grade.GradeldName,IDENTITY(int,1,1)as GradeId into AddressList5 From Grade;
更新一行数据:
UPDATE Grade SET GradeldName='123' WHERE GradeldName='oop';
更新全部数据:
UPDATE Grade SET GradeldName='123';
删除数据:
DELETE FROM Grade WHERE GradeId = '1';
DELETE FROM Grade WHERE GradeId='2';
删除表(不建议使用,数据无法恢复):
TRUNCATE TABLE 表名;
TRUNCATE TABLE AddressList;

查询语句:
SELECT GradeId FROM AddressList1 WHERE GradeldName='123' ;
查询除了‘123’的所有数据:
SELECT GradeldName FROM AddressList1 WHERE GradeldName<>'123';

合并列:
SELECT NAME=GradeldName+'.'+GradeId FROM AddressList5;

查询空值:
SELECT NAME FROM TABLE WHERE EMAIL IS NULL;
SELECT GradeId FROM AddressList1 WHERE GradeldName IS NULL;

查询并添加一行常量默认值学校名称:
SELECT GRADENAME='123','OBTK' AS SCHOOLNAME;
SELECT GradeldName = 'OOP','T153' AS SCHOOLNAME;

排序:
查多少行的排序:
例:查ID小于3的;
SELECT GradeldName AS '年级'  FROM AddressList1 WHERE GradeId<3 ORDER BY GradeId ;;
1、按照列排序
SELECT GradeldName AS '年级' FROM AddressList1  ORDER BY GradeId ;
SELECT GradeldName  AS '年级' ,'T153' AS '学校名称' FROM AddressList1 ORDER BY GradeldName;
2、列升序:
SELECT GradeldName AS '年级' FROM AddressList1  ORDER BY GradeId ASC ;
SELECT GradeldName  AS '年级' ,'T153' AS '学校名称' FROM AddressList1 ORDER BY GradeldName ASC;
3、列降序:
SELECT GradeldName AS '年级' FROM AddressList1  ORDER BY GradeId DESC;

SELECT GradeldName  AS '年级' ,'T153' AS '学校名称' FROM AddressList1 ORDER BY GradeldName DESC;