describe table name (can also be abbreviated as desc table name)
mysql> describe test;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
|id| int(10)| NO | PRI | NULL | auto_increment || name | char(20)| NO | MUL | NULL ||| score | decimal(5,2)| YES || NULL ||| address | varchar(20)| YES || 未知 ||
+---------+--------------+------+-----+---------+----------------+
4 rows inset(0.01 sec)
1.4 View the current status;
--------------
mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper
Connection id: 3
Current database: students ## 当前所在数据库
Current user: root@localhost ## 当前所使用用户及登录终端
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.17 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /usr/local/mysql/mysql.sock
Uptime: 8 min 33 sec
Threads: 1 Questions: 22 Slow queries: 0 Opens: 114 Flush tables: 1 Open tables: 107 Queries per second avg: 0.042
--------------
Two, SQL statement
Abbreviation of Structured Query Language, namely structured query module
Relational library language
Used to maintain and manage the database, including data query, data update, access control, object management and other functions.
SQL Classification DDL: Data Definition Language DML: Data Manipulation Language DQL: Data Query Language DCL: Data Control Language
2.1 DDL statement
Used to create database objects, such as libraries, tables, indexes, etc.
2.1.1 Create a database
mysql> create database student1;
2.1.2 Create data table
Basic syntax create table table name (field 01 name field 01 type field 01 constraint, field 02 name field 02 type field 02 constraint) storage engine, character set ## Define multiple types separated by commas
create table xuesheng(id int(10) auto_increment, ## id设置为int型,自增,不能为空
name varchar(15) NOT NULL, ## ## name设置为字符型,不能为空
score decimal(5,2) NOT NULL, ## score 设置为浮点型 最大位数为5,小数点后面保留两位,不能为空
address varchar(50) NOT NULL default ‘未知’, ## 设置为字符型,不能为空 默认为未知
PRIMARY KEY(id));## 设置主键为id
2.1.3 Delete database and table
Delete the specified data table drop table database name. Table name;
Included operations insert: insert new data update: update the original data delete: delete unnecessary data
2.2.1 Insert new data
Basic format: Method 1: INSERT INTO table name (field 1, field 2,...) VALUES (field 1 value, field 2 value...) ## Ordered, the table name and VALUES must be one-to-one correspondence, quantity Also correspond to Method 2: insert into info values (the value of field 1, the value of field 2, the value of field 3) ## Do not follow the field name, write all the values, you must write each one, no field name, default All fields Method three: insert into info (name, score, address) values ('赵六', 99, suzhou), ('tianqi', 60, default) ## Multi-line insert
Is to set or view user permissions, or create users
2.4.1 Set user permissions
Basic format GRANT permission list ON database name. Table name TO user name@source address [IDENTIFIED by'password']
grant all on *.* to 'jerry'@'localhost' identified by 'abc123'## 给予jerry用户本地登录对于所有数据库和表 #所有的权限
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.4.2 View user permissions
The basic format show grants for username@source address;
mysql> show grants for'jerry'@'localhost';
+----------------------------------------------------+
| Grants for jerry@localhost |
+----------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'jerry'@'localhost'|
+----------------------------------------------------+
1 row inset(0.00 sec)
2.4.3 Revoke user permissions
The basic format revoke permission list on database name. table name from username@source address;
mysql> revoke all on *.* from 'jerry'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for'jerry'@'localhost';
+-------------------------------------------+
| Grants for jerry@localhost |
+-------------------------------------------+
| GRANT USAGE ON *.* TO 'jerry'@'localhost'|
+-------------------------------------------+
1 row inset(0.00 sec)
Three, data table advanced operations
2.1 Temporary table
Temporarily created table, used to save some temporary data, will not exist for a long time (in memory, it will not exist after shutdown and restart)
Basic format create temporary table table name (field 01 name field 01 type field 01 constraint, field 02 name field 02 type field 02 constraint) storage engine, character set
create temporary TABLE mytmp1 (id int(10) NOT NULL AUTO_INCREMENT,
NAME varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
level int(10) NOT NULL,
PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.2 Clone table
Method one: create table clone the generated table table name as select * from the table name of the cloned table; (in one go) ## Can not clone the entire table after the select attribute
Method 2: Two steps: create table test like mytmp; (copy a table structure, generate a new table mytmp is copied) insert into test select * from mytmp; (import the data in the original table into the new table)
Basic format truncate table table name## delete from the difference between table name drop and truncate drop directly delete the table, the table is gone, truncate is the empty table, the table structure is still there, the data is not there