C:\Users\asus\AppData\Roaming\Microsoft\SQL Server Management Studio\14.0\SqlStudio.bin
select * from Student s2 where SUBSTRING(s2.sname,2,1)=SUBSTRING(s2.sname,3,1)
1 建立用戶sql
create login names with password='pwd' , default_database=test; create user names for login names with default_schema=dbo;
2 增刪改查受權
數據庫
grant select,insert,UPDATE,DELETE on 表 to names
3 建立表安全
grant create table to names GRANT ALTER ON SCHEMA::dbo TO names;
4 存儲過程受權服務器
GRANT EXECUTE ON 存儲過程名 TO username
5 禁止對錶受權antd
DENY UPDATE ON 表 TO username CASCADE;
6 回收權限函數
REVOKE DELETE ON 表 FROM username
7 刪除表code
truncate table a delete from dbo.a drop table a
8 修改列server
alter table a add sa nvarchar(10) not null Alter Table a Add Constraint PK_Course_Cno Primary Key(id) ALTER TABLE a ALTER COLUMN id int ALTER TABLE a DROP CONSTRAINT PK_a alter table a drop column sa
9 建立一個簡單的登陸,登陸名爲:newlogin;登陸密碼:123456;默認數據庫:master,默認數據庫也能夠不指定。索引
EXEC sp_addlogin 'newlogin','123456','master'
10 建立用戶事務
EXEC sp_adduser 'newlogin'
EXEC sp_adduser 'newlogin','newuser'
EXEC sp_addrole 'newrole'
EXEC sp_adduser 'newlogin','newuser','newrole'
GRANT ALL ON jobs TO newrole
GRANT SELECT,UPDATE ON sales TO newrole
DENY INSERT ON employees TO newrole
EXEC sp_grantdbaccess 'newlogin','newuser'
EXEC sp_addrolemember 'newrole','newuser'
EXEC sp_revokedbaccess 'newuser';
EXEC sp_droplogin 'newlogin'
EXEC sp_droprole 'newrole'
EXEC sp_droprolemember 'newrole', 'newuser'
CREATE LOGIN mylogin WITH PASSWORD = '123456' MUST_CHANGE;
CREATE LOGIN mylogin WITH PASSWORD = '123456', CREDENTIAL = mycredential;
CREATE LOGIN [jack\xiangzhao] FROM WINDOWS;
CREATE USER myuser FOR LOGIN mylogin
CREATE ROLE myrole AUTHORIZATION myuser;
CREATE ROLE myrole AUTHORIZATION db_role
1 建立僱傭日期規則 hire_date_rule
CREATE RULE hire_date_rule AS @hire_date>='1980-01-01' and @hire_date<=getdate()
2 建立性別規則sex_rule
CREATE RULE sex_rule AS @sex in ('男','女')
3 -建立評分規則grade_rule
CREATE RULE grade_rule AS @value between 1 and 100
4 建立字符規則my_character_rule
Create rule my_character_rule As @value like '[a-z]%[0-9]'
5 sp_helptext 查看規則,查看規則hire_date_rule的文本信息
EXECUTE sp_helptext hire_date_rule
6 sp_bindrule綁定規則
EXEC sp_bindrule hire_date_rule, 'employee.hire_date'
EXEC sp_addtype pat_char,'varchar(10)','NOT NULL' EXEC sp_bindrule my_character_rule, pat_char, 'futureonly';
7 sp_unbindrule 解除規則的綁定
EXEC sp_unbindrule 'employee.hire_date'; * DROP RULE語句刪除當前數據庫中的一個或多個規則 DROP RULE sex_rule,hire_date_rule
select * from ( select * ,ROW_NUMBER() over(order by sid) as rowcindex --增長索引 from Student) as t where t.rowcindex between 1 and 3;
select s.sid,s.sname, max(case c.cname when '語文' then sc.score end) as '語文', min(case c.cname when '數學' then sc.score end) as '數學', sum(case c.cname when '英語' then sc.score end) as '英語', max(case c.cname when '化學' then sc.score else 0 end) as '化學' from (Student as s inner join SC as sc on s.sid=sc.sid inner join Course as c on c.cid=sc.cid) group by s.sid,s.sname;
1 全局變量
select @@VERSION --數據庫版本 insert into Course values('',''); select @@IDENTITY --獲取最近insert語句的標識 print @@servername --服務器名稱 print @@rowcount--返回受影響的行數
2 選擇語句
declare @id int set @id =10 if @id>5 begin print 'ok' end else begin print 'no' end
3 循環語句
declare @id int set @id=1 while @id<10 begin print @id set @id=@id+1 end
4 異常
begin try delete from SC--不能成功 end try begin catch print @@error --判斷錯誤信息 0對 end catch
5 事務:回滾函數
begin try begin transaction --開啓事務 --錯誤SQL commit tran --提交事務 end try begin catch rollback tran --回退事務 end catch
6 鎖:同步
begin tran --鎖上 --修改操做 rollback tran --開鎖 commit tran