CREATE TABLE command in SQL to create a database table
What does the CREATE TABLE command in SQL do? In this article, let's learn everything you need to know about the CREATE TABLE statement in SQL !
If you are learning programming, you must know how to use SQL. In fact, learning SQL is not as difficult as many people think. It also has basic functions and commands. Just by knowing them, using SQL in programming projects will become simpler than ever.
Database tables in any relational database management system are used to store data in structured form (fields and records). Here, field is a column that defines the type of data stored in a table and record is a row containing the actual data. SQL provides various queries to interact with data in the most convenient way. You can use SQL commands to create and delete tables, and insert, update, and delete data in these tables. The instructions below will show you how to use SQL to create tables and other information you need to know .
CREATE TABLE command syntax
The SQL CREATE TABLE statement has the following syntax:
CREATE TABLE ten_bang( cot1 kieu_du_kieu, cot2 kieu_du_kieu, cot3 kieu_du_kieu, . cotN kieu_du_kieu, PRIMARY KEY( mot hoac nhieu cot ) );
CREATE TABLE is the keyword that tells the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table immediately follows the CREATE TABLE statement.
The parentheses will identify each column in the table and its data type. The above syntax will be clearer when you see the example below.
Example of CREATE TABLE command
The code below is an example of creating a NHANVIEN table with ID as the primary key and NOT NULL as a constraint to ensure fields cannot be NULL when creating records in this table.
CREATE TABLE NHANVIEN( ID INT NOT NULL, TEN VARCHAR (255) NOT NULL, TUOI INT NOT NULL, DIACHI CHAR (255) , LUONG DECIMAL (18, 2), PRIMARY KEY (ID) );
With the above table creation information, you will create a table with ID and TUOI columns of type INT that will contain an integer. Columns TEN, DIACHI are of type varchar containing characters with a maximum field length of 255 characters. The LUONG column is decimal data with 2 digits after the decimal point. And this table uses the ID column as the primary key (no duplicates).
You can verify if the table was created successfully by looking at the message displayed by the SQL server, or using the DESC command like this:
SQL> DESC NHANVIEN; +-----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | | | | TEN | varchar(255) | NO | | | | | TUOI | int(11) | NO | | | | | DIACHI | char(255) | YES | | NULL | | | LUONG | decimal(18,2) | YES | | NULL | | +---------+---------------+------+-----+---------+---------+ 5 rows in set (0.00 sec)
Create the table another way
You can also create a table from copying an existing table, using a combination of the CREATE TABLE command and the SELECT command. The copied table will have the same column data type as the original table's data type. The syntax is as follows:
CREATE TABLE ten_bang_moi AS SELECT cot_1, cot_2,. FROM ten_bang_da_co WHERE .;
For example : Create a data table KHACHHANG with columns ID, TEN, DIACHI similar to the NHANVIEN table above. You will use the following command:
CREATE TABLE KHACHHANG AS SELECT ID, TEN, DIACHI FROM NHANVIEN
Now, after you have the NHANVIEN and KHACHHANG tables ready in the database, you can start using the INSERT INTO function to insert data into the table.
You should read it
- How to create a table, insert a table in Excel 2016
- MS Access 2003 - Lesson 15: Create an initial table
- How to create automatic table of contents in Word 2016
- 35 tools, scripts and plugins to build HTML Table
- How to create an image table of contents in Word?
- Table in HTML
- Manipulating tables in Excel
- Create table of contents automatically in Word 2007 and 2010
May be interested
- Ms Access 2003 - Lesson 14: Chapter 4: Creating another databasein this chapter, you will meet some of the parts mentioned in chapter 2, but from a different perspective. you will learn how to manually create a table without relying on the table wizard ..
- Create Table (Table) in Access 2016the objects in the database depend a lot on the table, you should always start designing your database by creating all the tables.
- Rational DA Data Architecture and DB2 9: Build an SQL commandare you familiar with database explorer? whether or not, please read the following article to learn about some of the functions and components of this type of data architecture, more specifically about the ability to build sql commands encountered in the database (database ) yours.
- Create automatic table of contents in Word 2003creating an automatic table of contents in word 2003 makes the document look more professional, easy to find the item you need by clicking on the table of contents. so how to create a table of contents in word fastest? the answer is to create an automatic table of contents!
- A serious vulnerability on phpMyAdmin allows an attacker to destroy the databasea serious security vulnerability on phpmyadmin - one of the most popular mysql database management software will harm the database when the tricked administrator clicks on the link.
- The TRUNCATE TABLE command in SQL Serverthe truncate table statement is used to delete all records from a table in sql server.
- MS Access 2003 - Lesson 11: Chapter 3: Customizing componentsin the previous chapter, you learned how to create a database and create a table, as well as how to enter and edit information in the table. in this chapter, you will set up on the basis of chapter 2.
- How to create a table, insert a table in Excel 2016how to create a table, insert a table in excel 2016. your excel data needs to be inserted as a table to facilitate the management and editing of content accordingly. let's refer to the article detailing how to create and edit tables in excel 2016
- How to create a registration app using Python and databasepython has excellent database support built into its standard library, so you can create and interact with a database without having to rely on an external framework like django orm.
- 10 T-SQL Index statements needed with DBAsql server dba (database administrator) people - database administrators know very well that index entries in the database are very similar to index in the library section. or simply understand that index in database is a structure that is closely linked to tables to quickly gather information from the rows in that table.