SqlServer2012- create table, drop table to add a field to delete field operations

New table:
Create Table [table]
(
[number field Auto] int the IDENTITY (1,1) a PRIMARY KEY,
[field 1] nVarChar (50) default \ ' Default \' null,
[Field 2] ntext, null,
[ field. 3] datetime,
[field. 4] Money null,
[field. 5] int default 0,
[field. 6] a Decimal (12,4) default 0,
[field. 7] Image null,
)

Delete a table:
Drop the Table [table name]

Insert data:
the INSERT the INTO [table] (Field 1, Field 2) VALUES (100, \ ' 51WINDOWS.NET \')

To delete data:
DELETE the FROM [table name] WHERE [field name]> 100

Data Update:
the UPDATE [table] the SET [field 1] = 200, [Field 2] = \ '51WINDOWS.NET \' WHERE [ three field] = \ 'HAIWA \'

New field:
the ALTER TABLE [table] the ADD [field name] NVARCHAR (50) NULL

Delete field:
the ALTER TABLE [table name] DROP COLUMN [field name]

Modify the field:
the ALTER TABLE [table] ALTER COLUMN [field name] NVARCHAR (50) NULL

Rename the table: (Access rename the table, please refer to the article: in the Access database rename table)
sp_rename \ 'table name \', \ 'the new table name \', \ 'OBJECT \'

New constraints:
the ALTER TABLE [table] ADD CONSTRAINT constraint name the CHECK ([constraint field] <= \ '2000-1-1 \')

Delete constraint:
the ALTER TABLE [table name] DROP CONSTRAINT constraint name

New default values
ALTER TABLE [table] ADD CONSTRAINT Default name DEFAULT \ '51WINDOWS.NET \' FOR [field name]

Remove the default value
ALTER TABLE [table name] DROP CONSTRAINT name defaults

Sql Server to delete the log, reducing the file size of the database
dump transaction database name no_log with
Backup log database name no_log with
dbcc SHRINKDATABASE (database name)
Exec sp_dboption \ 'database name \', \ 'autoshrink \' , \ 'true \'

\\\'添加字段通用函数
Sub AddColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Add \"&ColumnName&\" \"&ColumnType&\"\")
End Sub

\\\'更改字段通用函数
Sub ModColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Alter Column \"&ColumnName&\" \"&ColumnType&\"\")
End Sub

\\\ 'check table exists

sql=\"select count(*) as dida from sysobjects where id = object_id(N\'[所有者].[表名]\') and OBJECTPROPERTY(id, N\'IsUserTable\') = 1\"

set rs=conn.execute(sql)

response.write rs (\ "dida \") \ 'returns a value, 0 for no, 1 indicates the presence of

判断表的存在:
select * from sysobjects where id = object_id(N\'[dbo].[tablename]\') and OBJECTPROPERTY(id, N\'IsUserTable\') = 1

The structure of a table
select * from syscolumns where id = object_id (N \ '[dbo]. [ Table your name] \') and OBJECTPROPERTY (id , N \ 'IsUserTable \') = 1

 

create table student(
Sno int not null primary key,
Sname char(10)not null,
Ssex bit not null,
Sage tinyint not null,
Sdept char(20) not null)

create table course(
Cno int not null primary key,
Cname char(20)not null,
Cpno int not null,
Ccredit tinyint not null)


create table sc(
Sno int not null,
Cno int not null,
Grade tinyint not null
foreign key(Sno)references student(Sno)
foreign key(Cno)references course(Cno)
)

 

 

(1)
seleCt top 1 S.sno,sname
from SC,S
where Cno='C2' and SC.sno=S.sno
order by grade desC;

(2)
seleCt sname,age
from Student,SC
where SC.sno not in(
seleCt SC.sno
from SC
where Cno='C2' )and SC.sno=S.sno;
(3)
seleCt sno, avg(grade) as average
from SC
group by sno
having(avg(grade)>80);
(3)法二
seleCt sno, avg(grade) ' average'
from SC
group by sno
having(avg(grade)>80);

(4)
delete from SC 
where SC.sno in(
   seleCt sno
   from S
   where sname='S5');
(5)
seleCt sname
from S
where sdept='英语'and sex='男';
(6)
seleCt SC.sno,avg(grade) as average
from S,SC
where S.sno=SC.sno
group by SC.sno;

Guess you like

Origin www.cnblogs.com/javier520/p/10943435.html