2. SQL--Query table, CREATE TABLE, insert data into table

Source: Internet
Author: User

(1): Query whether a table exists in a database ( two ways): Assuming that the table name is table_name

if exists (select * from sysobjects where name= ' table_name ')
DROP TABLE table_name


If OBJECT_ID (' table_name ') is NOT null
DROP TABLE table_name


The same operation can also be used to determine whether the database exists!


(2): Some instances of the table operation:

create an instance of a table: (Student score table:grade_table)

if exists (select * from sysobjects where name = ' grade_table ')
drop table Grade_table

Go
CREATE TABLE Grade_table
(
Stuid varchar (20),
CourseID varchar (20),
Grade int
)

(3): Insert data in table: (Student score table:grade_table)

INSERT into grade_table values (' 10001 ', ' 001 ', ' 85 ')

INSERT into grade_table values (' 10002 ', ' 001 ', ' 95 ')

(4): update the data in the table:

Update grade_table set grade=70 where stuid= ' 10001 ' and courseid= ' 001 '

(5): Delete data from table:

Delete grade_table where stuid= ' 10001 ' and courseid= ' 001 '

(6): Create a new table with the same student_table as the student and then insert the data queried in student_table, which is generally used to guide some data

  CREATE TABLE student_table
      (
              stuid varchar,
              CourseID varchar,
              grade int
     )
     
      CREATE TABLE Student
      (
              stuid varchar,
              CourseID varchar,
              grade int
     )
     
       INSERT into student_table values (' 10001 ', ' 001 ', ' I ')

INSERT into student_table values (' 10002 ', ' 001 ', ' 95 ')


INSERT INTO student
SELECT * from student_table as S1
where S1.stuid not in (select Stuid from student)

This article from "Ricky's Blog" blog, reproduced please contact the author!

2. SQL--Query table, CREATE TABLE, insert data into table

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.