Data application master's SQL basic tutorial sharing 3-create the first database table

Chapter 2 SQL Basics


(This chapter mainly introduces how to create and operate database tables, and let you learn to understand the constraint features of SQL and the most basic SELECT statement.)

1. Database table

Create your first database table

1. Create a table

【Introduction to knowledge points】

In the first chapter, we already know what database and SQL are, then, we start to really learn SQL.
First we need to create a new table.
In SQL, CREATE is used to create the table TABLE, the syntax is as follows:


 

From this it can be seen that:

The name of the table, given after CREATE TABLE;
the name and definition of the column, separated by commas.

【Example】

Use CREATE TABLE to create the Student table.



 

2. Use Null values

【Introduction to knowledge points】

In the SQL data structure in Chapter 1, we mentioned the Null data type. When creating a table, you can use Null and NOT Null to specify columns.
Columns such as:



 

This means that ID and SName do not accept rows without column values, that is, when we insert or update row data in the future, there must be a value;
if NULL is used, it means that the column allows inserting rows, and the value of the column can not be given. .

 

3. Update table

【Introduction to knowledge points】

We can use ALTER TABLE to update the columns in the table, the specific syntax is as follows:



 

Although we can use ALTER TABLE to update the list, ideally, we still need to give full consideration to the initial design of the table to avoid making changes to its structure in the future, which is also what a good programmer needs. The power of vision and complete design thinking.

【Example】

Add a new column Hobby to the Student table.



 

4. Delete table

【Introduction to knowledge points】

Use DROP TABLE to drop an entire table:

DROP TABLE table name;

 

To delete a column from a table you can use:

ALTER TABLE table name
DROP COLUMN 列名;

 

Assuming that there is also a column Hobby (hobby) in Student, to delete the column can be written as follows:

ALTER TABLE Student
DROP COLUMN Hobby;

 

Of course, when we delete a table, we must pay attention to all object associations and constraints related to the table, which are the constraints of the keys to be mentioned below.
In addition, the implication is as I said before, we must think more when designing the table at the beginning, and try to avoid modifying and deleting the columns of the table.

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260630&siteId=291194637