Common SQL statements for database operations

Source: Internet
Author: User

On weekdays, DBMS is used much more, and SQL statements are not used for operations. Which of the following statements can be used in a written test.

Create a database first

 
 
  1. Create database mical
  2. On
  3. Primary (name = miacal_data1,
  4. Filename = 'e: \ SQL Server 200 \ MSSQL $ MICAL_BO \ SQL server 2000 Personal Edition run \ MSSQL \ Data \ mical_data1.mdf ',
  5. Size = 2 MB,
  6. Maxsize = 10,
  7. Filegrowth = 1 ),
  8. (Name = mical_data2,
  9. Filename = 'e: \ SQL Server 2000 \ MSSQL $ MICAL_BO \ SQL server 2000 Personal Edition run \ MSSQL \ Data \ mical_data2.mdf ',
  10. Size = 2 MB,
  11. Maxsize = 10,
  12. Filegrowth = 1)
  13. Log on
  14. (Name = mical_log1,
  15. Filename = 'e: \ SQL Server 2000 \ MSSQL $ MICAL_BO \ SQL server 2000 Personal Edition run \ MSSQL \ Data \ mical_log1.ldf ',
  16. Size = 2 MB,
  17. Maxsize = 8,
  18. Filegrowth = 1 ),
  19. (Name = mical_log2,
  20. Filename = 'e: \ SQL Server 2000 \ MSSQL $ MICAL_BO \ SQL server 2000 Personal Edition run \ MSSQL \ Data \ mical_log2.ldf ',
  21. Size = 2 MB,
  22. Maxsize = 8,
  23. Filegrowth = 1)
  24. Go

Modify the database name exec sp_renamedb 'mical', 'ss'

1. Start with table creation:

 
 
  1. Use ss
  2. Create table lxp_table
  3. (
  4. Coll1 char (50) not null,
  5. Coll2 int,
  6. Coll3 int identity (1, 1) not null automatically increases by 1
  7. Primary key (coll3)/* Create a primary key */
  8.  
  9.  
  10. Create table lxp_ B
  11. (
  12. B1 varchar not null,
  13. B2 varchar not null,
  14. B3 int identity (1, 1) not null,
  15. Primary key (b3)
  16. )

2. Modify the table name

 
 
  1. EXEC   sp_rename   'lxp_table',   'lxp_a' 

3. Modify the column name

 
 
  1. Exec sp_rename 'lxp_a.[coll1]','a1' 
  2. exec sp_rename 'lxp_a.[coll2]','a2' 
  3. exec sp_rename 'lxp_a.[coll3]','a3' 

4. Add new columns

 
 
  1. alter table lxp_a  
  2. add a_3 varchar 
  3. exec sp_rename 'lxp_a.[a_3]','a4' 

5. Modify the column type

 
 
  1. alter table lxp_a  
  2. alter column a4 char(50)   

When you modify a data type, you can only convert it to a data type that can be converted. When you modify a data type, the system automatically converts the data in this column. If the data cannot be converted, the data cannot be modified)

6. Add a foreign key when creating a table

 
 
  1. Create table a_ B
  2. (
  3. A_id int not null
  4. Constraint aa foreign key (a_id) references lxp_a (a3)
  5. B _id int not null)
  6.  
  7. Drop table a_ B

7. Add a foreign key to the created table

 
 
  1. alter table a_b  
  2. add constraint bb foreign key (b_id) references lxp_b(b3) 

8. delete a foreign key from a created table

 
 
  1. alter table a_b  
  2. drop bb 

9. Find out who is connected to the database

 
 
  1. select * from master..sysprocesses where hostname<>'' 
  2. exec sp_who 

10. query related information of a specified database

 
 
  1. select * from sysobjects where type = 'U';   
  2. select name from sysobjects where type = 'F';   
  3. select name from sysobjects where type = 'P'; 

Because the system table sysobjects stores all database objects, type indicates the types of various objects, including:

U = User table
S = system table
C = CHECK Constraints
D = DEFAULT value or DEFAULT Constraint
F = foreign key constraint
L = Log
FN = scalar function
IF = embedded table functions
P = Stored Procedure
PK = primary key the constraint type is K)
RF = copy and filter the Stored Procedure
TF = table functions
TR = trigger
UQ = the UNIQUE constraint type is K)
V = View
X = Extended Stored Procedures and Related Object Information.

PS: Open the database

 
 
  1. use DNN_LH_493 

11. query all user Databases

 
 
  1. exec sp_databases 

12. query all tables in the specified database.

 
 
  1. use ss  
  2. exec sp_tables 

Original article title: SQL statements for Common Database Operations

Edit recommendations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.