mysql常用操作之create、update、drop、truncate等

[[email protected]_41_171_centos ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3713

Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

+---------------------------+

| Tables_in_mysql           |

+---------------------------+

| columns_priv              |

| db                        |

| event                     |

| func                      |

| general_log               |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| ndb_binlog_index          |

| plugin                    |

| proc                      |

| procs_priv                |

| servers                   |

| slow_log                  |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

+---------------------------+

23 rows in set (0.00 sec)

mysql> desc user;

+-----------------------+-----------------------------------+------+-----+---------+-------+

| Field                 | Type                              | Null | Key | Default | Extra |

+-----------------------+-----------------------------------+------+-----+---------+-------+

| Host                  | char(60)                          | NO   | PRI |         |       |

| User                  | char(16)                          | NO   | PRI |         |       |

| Password              | char(41)                          | NO   |     |         |       |

| Select_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Insert_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Update_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Delete_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Create_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Drop_priv             | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Reload_priv           | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Shutdown_priv         | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Process_priv          | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| File_priv             | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Grant_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| References_priv       | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Index_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Alter_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Show_db_priv          | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Super_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Create_tmp_table_priv | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Lock_tables_priv      | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Execute_priv          | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Repl_slave_priv       | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Repl_client_priv      | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Create_view_priv      | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Show_view_priv        | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Create_routine_priv   | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Alter_routine_priv    | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Create_user_priv      | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Event_priv            | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| Trigger_priv          | enum(‘N‘,‘Y‘)                     | NO   |     | N       |       |

| ssl_type              | enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) | NO   |     |         |       |

| ssl_cipher            | blob                              | NO   |     | NULL    |       |

| x509_issuer           | blob                              | NO   |     | NULL    |       |

| x509_subject          | blob                              | NO   |     | NULL    |       |

| max_questions         | int(11) unsigned                  | NO   |     | 0       |       |

| max_updates           | int(11) unsigned                  | NO   |     | 0       |       |

| max_connections       | int(11) unsigned                  | NO   |     | 0       |       |

| max_user_connections  | int(11) unsigned                  | NO   |     | 0       |       |

+-----------------------+-----------------------------------+------+-----+---------+-------+

39 rows in set (0.00 sec)

mysql> show create table user;

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| user  | CREATE TABLE `user` (

`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT ‘‘,

`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT ‘‘,

`Select_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Insert_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Update_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Delete_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Drop_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Reload_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Shutdown_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Process_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`File_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Grant_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`References_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Index_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Alter_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Show_db_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Super_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_tmp_table_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Lock_tables_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Execute_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Repl_slave_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Repl_client_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Show_view_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Alter_routine_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Create_user_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Event_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`Trigger_priv` enum(‘N‘,‘Y‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘N‘,

`ssl_type` enum(‘‘,‘ANY‘,‘X509‘,‘SPECIFIED‘) CHARACTER SET utf8 NOT NULL DEFAULT ‘‘,

`ssl_cipher` blob NOT NULL,

`x509_issuer` blob NOT NULL,

`x509_subject` blob NOT NULL,

`max_questions` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_updates` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

`max_user_connections` int(11) unsigned NOT NULL DEFAULT ‘0‘,

PRIMARY KEY (`Host`,`User`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT=‘Users and global privileges‘ |

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP();

+------------------+

| UNIX_TIMESTAMP() |

+------------------+

|       1460579152 |

+------------------+

1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP() * 1000;

+-------------------------+

| UNIX_TIMESTAMP() * 1000 |

+-------------------------+

|           1460579158000 |

+-------------------------+

1 row in set (0.00 sec)

mysql> create database shawyang;

Query OK, 1 row affected (0.00 sec)

mysql> use shawyang;

Database changed

mysql> create table tb1 (`id` int(4), `name` char(40)) ENGINE=MyISAM DEFAULT CHARSET=gbk;

Query OK, 0 rows affected (0.01 sec)

mysql> desc tb1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| id    | int(4)   | YES  |     | NULL    |       |

| name  | char(40) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> show create table tb1\G;

*************************** 1. row ***************************

Table: tb1

Create Table: CREATE TABLE `tb1` (

`id` int(4) DEFAULT NULL,

`name` char(40) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> insert into tb1 values(1, ‘shawyang‘);

Query OK, 1 row affected (0.00 sec)

mysql> insert into tb1 values(2, ‘51cto‘);

Query OK, 1 row affected (0.00 sec)

mysql> show create table tb1\G;

*************************** 1. row ***************************

Table: tb1

Create Table: CREATE TABLE `tb1` (

`id` int(4) DEFAULT NULL,

`name` char(40) DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=gbk

1 row in set (0.00 sec)

ERROR:

No query specified

mysql> insert into tb1 (`id`) values(3);

Query OK, 1 row affected (0.00 sec)

mysql> insert into tb1 (`id`) values(4);

Query OK, 1 row affected (0.00 sec)

mysql> insert into tb1 (`name`) values(‘haha‘);

Query OK, 1 row affected (0.00 sec)

mysql> insert into tb1 (`name`, `id`) values("hehe", 6);

Query OK, 1 row affected (0.00 sec)

mysql> select * from tb1;

+------+----------+

| id   | name     |

+------+----------+

|    1 | shawyang |

|    2 | 51cto    |

|    3 | NULL     |

|    4 | NULL     |

| NULL | haha     |

|    6 | hehe     |

+------+----------+

6 rows in set (0.00 sec)

mysql> update tb1 set id=5 where name = ‘haha‘;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from tb1;

+------+----------+

| id   | name     |

+------+----------+

|    1 | shawyang |

|    2 | 51cto    |

|    3 | NULL     |

|    4 | NULL     |

|    5 | haha     |

|    6 | hehe     |

+------+----------+

6 rows in set (0.00 sec)

mysql> delete from tb1 where name = ‘hehe‘;

Query OK, 1 row affected (0.00 sec)

mysql> select * from tb1;

+------+----------+

| id   | name     |

+------+----------+

|    1 | shawyang |

|    2 | 51cto    |

|    3 | NULL     |

|    4 | NULL     |

|    5 | haha     |

+------+----------+

5 rows in set (0.00 sec)

mysql> truncate table shawyang.tb1;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from tb1;

Empty set (0.00 sec)

mysql> drop table tb1;

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;

Empty set (0.00 sec)

mysql> drop database shawyang;

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

mysql> exit

Bye

[[email protected]_41_171_centos ~]# exit

logout

时间: 2024-10-15 02:39:21

mysql常用操作之create、update、drop、truncate等的相关文章

MySql常用操作【基础且详细(●'?'●)】

有那么挺长段时间没有敲代码了,今敲起来竟然有些sql都想不起来了?? 把以前整理sql的内容看了下,再加了点?? 主要参考自 MySql文档:https://dev.mysql.com/doc/refman/8.0/en/tutorial.html 易百教程:https://www.yiibai.com/mysql 一,基本操作 1.连接操作 连接远程数据库: $ mysql -h host -u -user -p (host:主机 user:用户名) 连接本地数据库: $ mysql -u u

MySQL常用操作总结

MySQL常用操作 前提条件:已安装MySQL. 学习目标:用一条sql语句写出A和B的剩余数量 AA表 BB表 以上为一道面试题,接下来由这道面试题来回顾一些数据库的基本操作. 登录MySQL sudo mysql -p,回车,然后输入密码即可,按ctrl+c退出登录: 显示数据库 show databases; 创建数据库 create database xxx; 选择数据库 use database xxx; 显示数据库表 show tables; 创建数据库表 create table 

ubuntu命令行操作mysql常用操作

登陆mysql [email protected]:~/ruby/mydiary$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. 查看所有的数据库 mysql> show databases; --注意必须要用;结尾否则不会立即执行代码 +--------------------+ | Database | +--------------------+ | inf

MySQL常用操作(2)MySQL用户管理、常用sql语句、 MySQL数据库备份恢复

                MySQL用户管理 创建一个普通用户并且授权 1.grant all on *.* to 'user1' identified by 'passwd'; grant all on *.* to 'user1' identified by '123456'; (创建user1用户,all表示所有权限(读.写,增.删.改.查等):*.*,前面的*表示所有的数据库,后面的*表示所有的表:identified by后面跟密码,要用单引号''引起来) grant all o

mysql常用操作 mysql备份与恢复

先登录mysql  ==>mysql -uroot -p 查看数据库的版本 select version(); 查看有哪些库 show datases; 查看当前处于哪个库 select database(); 查看当前登录用户 select user(); 查看某个库下面的表: use db; show tables; 查看表的字段: desc dong; 创建库 create database db1; 创建表 create  table  tb1 (`id` int(4),`name`ch

MYSQL常用操作及python操作MYSQL常用类

Mysql 常见操作 数据库操作 创建数据库 create database fuzjtest 删除数据库 drop database fuzjtest 查询数据库 show databases 切换数据库 use databas 123123 ###用户授权 创建用户 create user '用户名'@'IP地址' identified by '密码'; 删除用户 drop user '用户名'@'IP地址'; 修改用户 rename user '用户名'@'IP地址'; to '新用户名'

第二篇 Mysql常用操作记录(转载)

我们在创建网站的时候,一般需要用到数据库.考虑到安全性,建议使用非root用户.常用命令如下: 1.新建用户 //登录MYSQL@>mysql -u root -p@>密码//创建用户mysql> insert into mysql.user(Host,User,Password) values(‘localhost’,'allen’,password(‘allen’));//刷新系统权限表mysql>flush privileges;这样就创建了一个名为:allen  密码为:a

CentOS中Mysql常用操作

安装mysqlyum -y install mysql-server 修改mysql配置vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记暂时修改一下编码(添加在密码下方): default-character-set = utf8 设置mysql随系统启动# chkconfig mysqld on ← 设置MySQL服务随系统启动自启动# chkconfig --list mysqld ← 确认MySQL自启动mysqld 0:off 1:off 2:on 3:on

2018-3-22 13周4次课 MySQL常用操作(上)

13.1 设置更改root密码 默认MySQL密码为空 [[email protected] ~]# mysql -uroot -bash: mysql: 未找到命令 [[email protected] ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [[email protected] ~]# export PATH=$PATH:/usr/local/mysql/bin/ [[email p