Database_7_SQL Basic Operations - Table Operations

SQL Basic Operations - Table Operations

Tables and fields are inseparable.

1. New data table

create table [if not exists] 表名(
field name data type,
Field name data type -- no comma required on the last line
)[table options];

if not exists: If the table name does not exist, it will be created for a long time, otherwise the creation code will not be executed, which acts as a check function
Table Options: Control table performance
    Character set: charset/character set specific character set; -- Guaranteed character set for data storage in the table
    Proof set: collate specific proof set; -- the table adopts the way of data comparison
    Storage engine: engine specific storage engine (innodb and myisam)

 Example:

-- create table
create table if not exists student(
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;

 

The design of any table must specify the database

Scenario 1: The database described by the specified table shown

  crate table database name. table name; -- create the current data table to the specified database

-- create table
create table if not exists mydatabase.student( -- display the student table under the mydatabase database
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;

Option 2: Implicitly specify the database to which the table belongs: first enter a database environment, and then the table created in this way automatically belongs to a specified database

  Enter the database environment: use database name;

-- Enter the database first, then create the table
use mydatabase;
create table class(
name varchar(10),
room varchar(10)
)charset utf8;

 

When the SQL command to create the data table is executed, what happens?

  1. The corresponding table already exists in the specified database
  2. In the folder corresponding to the database, the structure file of the corresponding table will be generated (related to the storage engine)

2. View the data sheet

 The way the database can be viewed, the table can be viewed.

  • View all tables: show tables;

  

  • View part of the table: Fuzzy matching show tables like 'pattern'; --pattern is the matching pattern, %: means matching multiple characters, _: means matching a single character

    -- View the table ending with s (try not to check in this way, it is inefficient)
    show tables like '%s';
    

  • View the table creation statement: show create table table name;

    -- View table creation statement
    show create table student\g -- \g is equivalent to;
    show create table student\G -- \G means to rotate the found structure by 90 degrees into vertical
    

  • View table structure: View field information in the table desc/describe/show columns from table name;

    -- View table structure
    desc class;
    describe class;
    show columns from class;
    

    Note: The last line of the above picture is wrong, change form to from

3. Modify the data table

The table itself exists and also includes fields, so the modification of the table is divided into two parts: modifying the table itself and modifying the fields

Modify the table itself:

The table itself can be modified: table name and table options

  Modify the table name: rename table old table name to new table name;

-- Rename: student table -> my_student (take the first two letters of the database name)
rename table student to my_student;

    

  Modify table options: character set, collation set and storage engine alter table table name table option [=] value;

show create table my_student;

-- Modify table options: such as character set
alter table my_student charset =GBK;

show create table my_student;

   

  

Modify fields:

  There are many field operations: add, modify, duplicate, delete

  • Add field alter table table name add [aolumn] field name data type [column attribute] [location]

    Location: The field name can be stored anywhere in the table. There are two types of storage methods. The first type is first: the first position, and the second type after: after which field (after field name; the default is the last one after the field)

  • Modify fields

  • Duplicate field

  • delete field

 

Guess you like

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