sqlserver中起别名和添加列、修改列

/*
truncate table 表名(删除表中数据,和delete table 表名 是一样的,效率
要比delete高,即删除速度比delete快)
*/
truncate table student4
SELECT * FROM dbo.student4
 
/*
起别名:2种方式
可以使用as关键字起别名
也可以用=来起别名
*/
SELECT *,stuAddress AS '地址' FROM student
SELECT *,stuAddress AS 地址 FROM student
SELECT  stu.*, '地址' = stu.stuAddress FROM student AS stu
SELECT stu.*,'地址' = stu.stuName + '籍贯在' + stu.stuAddress FROM student AS stu
UPDATE student SET stuAddress = '江西省赣州市' WHERE stuId = '9'
--添加列
ALTER TABLE student ADD hobby VARCHAR(30) NULL
ALTER TABLE student ADD schoolName VARCHAR(30) NULL
ALTER TABLE student ADD groupId VARCHAR(5)
--修改列
ALTER TABLE student ALTER COLUMN hobby VARCHAR(30) NULL
ALTER TABLE student ALTER COLUMN schoolName VARCHAR(25) NOT NULL
--
UPDATE student SET schoolName = '城关小学' WHERE stuId = '5'
UPDATE student SET schoolName = '' WHERE stuId = '5'
UPDATE student SET schoolName = NULL WHERE stuId = '1'
UPDATE student SET schoolName = 'null' WHERE stuId = '1'
--
SELECT * FROM student WHERE schoolName is NULL
SELECT * FROM student WHERE schoolName is NOT NULL
--常量列
SELECT *, 学校名称 = schoolName, '城关小学' AS 小学名称 FROM student 
SELECT *, 学校名称 = schoolName, '实验小学' AS '校名' FROM student 
/*
以下是一个sql的完整写法
SELECT 
列名1 AS 别名,
列名2 AS 别名
FROM
表名
WHERE 
条件
GROUP BY
分组
HAVING
分组后的筛选
ORDER BY desc / asc
排序
*/

 

你可能感兴趣的:(sqlserver,数据库,truncate,起别名,添加列,修改列)