SQL start from scratch

Source: Internet
Author: User

SQL is divided into two parts: Data Manipulation Language (DML) and data definition language (DDL)

The query and update Directives form the DML portion of SQL:

    • SELECT -get data from a database table
    • Update-updating data in a database table
    • Delete-deletes data from the database table
    • INSERT INTO-inserts data into a database table
The Data Definition language (DDL) portion of SQL gives us the ability to create or delete tables. We can also define indexes (keys), specify links between tables, and impose constraints between tables.

The most important DDL statement in SQL:

  • CREATE database-creating new databases
  • alter database-Modify Databases
  • CREATE TABLE-Creates a new table
  • ALTER TABLE -Change (change) database table
  • Drop Table-delete tables
  • CREATE index-Creating indexes (search key)
  • Drop Index-delete indexes

Step-by-step:

Set up a database first

(Note: SQL is not case-sensitive, and is used to capitalize)

CREATE DATABASE db_name

Then create a table

CREATE TABLE article (Id int not null,title varchar (), author varchar (), keyword varchar (), CreateDate datetime, Content text,express Text)

And then it's done, a database a table is out, but there is a problem, I want to set a primary key how to do, and to achieve the value of self-increment (that is, the value of this column can be automatically built according to your row values)

That's what I wrote.

ALTER TABLE [db_name]. [dbo]. [Article] ADD CONSTRAINT id_key PRIMARY key (Id)

Don't laugh, I was just beginning to touch the database, so the above error, what error? Is the primary key is done well, but the effect of self-increase is not realized, how to achieve it, and then I tried to find that there are two property settings: Identity and Auto_increment, using the IDENTITY keyword in SQL to perform auto_increment ( Note: The identity start value is 1, each new record is incremented by 1), but unfortunately this keyword can only be used in two cases, one is used when creating a table, as follows:

Create TABLE article (Id int not NULL PRIMARY KEY identity,title varchar (), author varchar (), keyword varchar (#), create Date datetime,content text,express text)
The other is when you add a new field, as follows:

ALTER TABLE [db_name]. [dbo]. [Article] ADD Id int IDENTITY (+)
Feel the first and the second comparison, now can only choose the second type, should be the table has been established, do not want to delete the table reconstruction, then delete the ID field to add the field again, and then I went to delete that field, the tragedy came again, the result that the field is deleted, the code is as follows:

ALTER TABLE [db_name]. [dbo]. [Article] DROP COLUMN Id
Later, the head of the Flash, is not the above I set the primary key when the cause of the deletion, small try, I first delete the primary key constraints, the code is as follows:

ALTER TABLE [db_name]. [dbo]. [Article] DROP CONSTRAINT Id_key
Then execute the code that deletes the ID field, and the result can be

OK, you can re-add the fields you added, the code is as follows:

ALTER TABLE [db_name]. [dbo]. [Article] ADD Id int IDENTITY (+)

OK, add the primary key constraint, the code is as follows:

ALTER TABLE [db_name]. [dbo]. [Article] ADD CONSTRAINT id_key  PRIMARY key (Id)

Well, a self-increasing primary key table is set up. Forget, you can add information, the code is as follows:

INSERT into [db_name]. [dbo]. [Article] (title,author,keyword,createdate,content,express) VALUES (' Mid-Autumn Festival ', ' Home ', ' festivals ', GETDATE (), ' The next festival is the mid-Autumn Festival, since the university to now every mid-Autumn festival is outside, and sometimes calm down to think really sorry home mom and dad, every national day home, Mom and dad will take the house to me to save the moon cakes, all said son, son but on the other ', ' in addition to the national day and the holiday, other holidays are short, the whole holiday days are not enough to spend on the road time, the only thing is their own holiday ')



Cond.....










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.