SQL statement to create a table

Reference: SQL CREATE TABLE Statement | Novice Tutorial

Create statement:

CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

eg:

CREATE TABLE websites
(
id int,
name varchar(255),
url varchar(255),
alexa varchar(255),
country varchar(255)
);

Create a table named "websites". The table contains five columns, namely id, name, url, alexa and country.

The specific explanation is as follows:

  • The id column is an integer type and is used to store a unique identifier for each website.
  • The name column is a variable-length string type (up to 255 characters), used to store the name of each website.
  • The url column is a variable-length string type (up to 255 characters), used to store the URL address of each website.
  • The alexa column is a variable-length string type (up to 255 characters), and is used to store the ranking information of each website in the Alexa ranking.
  • The country column is a variable-length string type (up to 255 characters), and is used to store the country information to which each website belongs.

It is also possible to insert some records into the table :

Insert statement:

The first form does not need to specify the column name to insert data, just provide the value to be inserted:

INSERT INTO table_name
VALUES (value1,value2,value3,...);

The second form requires specifying the column names and the values ​​to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

eg:

insert into websites (id, name, url, alexa, country) values (1, 'Google', 'google.cm', 1, 'USA');
insert into websites (id, name, url, alexa, country) values (2, '淘宝', 'taobao.com', 13, 'CN');
insert into websites (id, name, url, alexa, country) values (3, '菜鸟教程', 'runoob.com', 4689, 'CN');
insert into websites (id, name, url, alexa, country) values (4, '微博', 'weibo.com', 20, 'CN');

Insert four records into the table named "websites". Each record represents the information of a website, including the values ​​of attributes such as id, name, url, alexa, and country.

The specific explanation is as follows:

  • The first SQL statement inserts a record into the table, which represents information from the Google website. Among them, id is 1, name is "Google", url is "google.cm", alexa is 1, and country is "USA".
  • The second SQL statement inserts a record into the table, which represents the information of the Taobao website. Among them, the id is 2, the name is "Taobao", the url is "taobao.com", the alexa is 1, and the country is "CN".
  • The third SQL statement inserts a record into the table, which represents the information of the rookie tutorial website. Among them, the id is 3, the name is "Rookie Tutorial", the url is "runoob.com", the alexa is 4689, and the country is "CN".
  • The fourth SQL statement inserts a record into the table, which represents the information of the microblog website. Among them, the id is 4, the name is "Weibo", the url is "weibo.com", the alexa is 20, and the country is "CN".

Then use ' SELECT * FROM websites; ' to view the table

 

Guess you like

Origin blog.csdn.net/Toml_/article/details/131825607