7.20_MYSQL

MySQL

C:\WINDOWS\system32>net start mysql 启动mysql
请求的服务已经启动。

请键入 NET HELPMSG 2182 以获得更多的帮助。

C:\WINDOWS\system32>mysql -u root -p 进入mysql
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.30 MySQL Community Server - GPL

Copyright © 2000, 2022, Oracle and/or its affiliates.

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> CREATE DATABASE company_info; 创建数据库
Query OK, 1 row affected (0.01 sec)

mysql> USE company_info; 使用数据库company_info
Database changed
mysql> SHOW DATABASES; 查看所有的数据库
±-------------------+
| Database |
±-------------------+
| company_info |
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+
5 rows in set (0.01 sec)

mysql> USE mysql
Database changed
mysql> show tables;
±-----------------------------------------------------+
| Tables_in_mysql |
±-----------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| replication_group_configuration_version |
| replication_group_member_actions |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
±-----------------------------------------------------+
37 rows in set (0.01 sec)

mysql> USE company_info; 使用数据库company_info
Database changed
mysql> create database test character set utf8; 创建表text以及编码
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> drop detabase test; 删除表text
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘detabase test’ at line 1
mysql> drop database test;
Query OK, 0 rows affected (0.02 sec)

mysql>  CREATE TABLE `dept`(`deptno` INT DEFAULT 1 COMMENT '部门编号', `deptname` VARCHAR(20) COMMENT '部门名称');//创建表dept

Query OK, 0 rows affected (0.03 sec)

mysql> show tables; 显示所有的表
±-----------------------+
| Tables_in_company_info |
±-----------------------+
| dept |
±-----------------------+
1 row in set (0.00 sec)

ping 10.0.4.234 用网络连接别人的数据库

正在 Ping 10.0.4.234 具有 32 字节的数据:
来自 10.0.4.234 的回复: 字节=32 时间=2ms TTL=128
来自 10.0.4.234 的回复: 字节=32 时间=2ms TTL=128
来自 10.0.4.234 的回复: 字节=32 时间=2ms TTL=128
来自 10.0.4.234 的回复: 字节=32 时间=2ms TTL=128

10.0.4.234 的 Ping 统计信息:

数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

最短 = 2ms,最长 = 2ms,平均 = 2ms

mysql> use company_info;使用数据库
Database changed

mysql> create table user(name varchar(20) comment '姓名',gender char(1) comment '性别',age int comment '年龄',birthday date comment '生日',phone varchar(20) comment '电话',address varchar(50) comment '地址',email varchar(20) comment '邮箱');

Query OK, 0 rows affected (0.03 sec)

mysql> desc user;查询表结构
±---------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±---------±------------±-----±----±--------±------+
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int | YES | | NULL | |
| birthday | date | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
±---------±------------±-----±----±--------±------+
7 rows in set (0.02 sec)

mysql> --添加列

mysql> ALTER TABLE user ADD create_time datetime;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc user;
±------------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±------+
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int | YES | | NULL | |
| birthday | date | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| create_time | datetime | YES | | NULL | |
±------------±------------±-----±----±--------±------+
8 rows in set (0.00 sec)

mysql> alter table user modify create_time timestamp;修改表user列为create_time的数据类型
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc user;
±------------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±------+
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int | YES | | NULL | |
| birthday | date | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| create_time | timestamp | YES | | NULL | |
±------------±------------±-----±----±--------±------+
8 rows in set (0.00 sec)

mysql> alter table user change birthday birth date;修改birthday为birth
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc user;
±------------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±------+
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int | YES | | NULL | |
| birth | date | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| create_time | timestamp | YES | | NULL | |
±------------±------------±-----±----±--------±------+
8 rows in set (0.00 sec)

mysql> show databases;查看数据库
±-------------------+
| Database |
±-------------------+
| company_info |
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
±-------------------+
6 rows in set (0.00 sec)

mysql> insert into dept values(1,'研发部');插入数据
Query OK, 1 row affected (0.01 sec)

mysql> select * from dept;查询dept表
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
±-------±----------+
1 row in set (0.01 sec)

mysql> insert into dept(deptno,deptname) values (2,'人事部'),(3,'安保部'),(5,'宣传部'),(6,'组织部'),(7,'财务部');
//添加数据

Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from dept;查询dept表
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
±-------±----------+
6 rows in set (0.00 sec)

mysql> insert into dept(deptno) values (4);
Query OK, 1 row affected (0.01 sec)

mysql> select * from dept;
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
| 4 | NULL |
±-------±----------+
7 rows in set (0.00 sec)

mysql> select * from company_info.dept;
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
| 4 | NULL |
±-------±----------+
7 rows in set (0.00 sec)

mysql> select deptname where dept;
ERROR 1054 (42S22): Unknown column ‘deptname’ in ‘field list’
mysql> select deptname from dept;
±----------+
| deptname |
±----------+
| 研发部 |
| 人事部 |
| 安保部 |
| 宣传部 |
| 组织部 |
| 财务部 |
| NULL |
±----------+
7 rows in set (0.00 sec)

mysql> select deptno,deptname from dept;
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
| 4 | NULL |
±-------±----------+
7 rows in set (0.00 sec)

mysql> select * from dept where deptno = 5;查询deptno=5的数据
±-------±----------+
| deptno | deptname |
±-------±----------+
| 5 | 宣传部 |
±-------±----------+
1 row in set (0.01 sec)

mysql> select * from dept where deptno < 5;查询deptno<5的数据
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 4 | NULL |
±-------±----------+
4 rows in set (0.00 sec)

mysql> select * from dept where deptno <> 4;查询deptno不包含4的数据
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
±-------±----------+
6 rows in set (0.00 sec)

mysql> select * from dept where deptno != 3;查询deptno不等于3的数据
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 5 | 宣传部 |
| 6 | 组织部 |
| 7 | 财务部 |
| 4 | NULL |
±-------±----------+
6 rows in set (0.00 sec)

mysql> select * from dept where deptno = 3 and deptname = '安保部';
±-------±----------+
| deptno | deptname |
±-------±----------+
| 3 | 安保部 |
±-------±----------+
1 row in set (0.00 sec)

mysql> select * from dept where deptno = 5 or deptname = '人事部';
±-------±----------+
| deptno | deptname |
±-------±----------+
| 2 | 人事部 |
| 5 | 宣传部 |
±-------±----------+
2 rows in set (0.00 sec)

mysql> select * from dept where deptno between 1 and 5;
±-------±----------+
| deptno | deptname |
±-------±----------+
| 1 | 研发部 |
| 2 | 人事部 |
| 3 | 安保部 |
| 5 | 宣传部 |
| 4 | NULL |
±-------±----------+
5 rows in set (0.00 sec)

mysql> select * from dept where deptno <= 4 and deptno >= 2;
±-------±----------+
| deptno | deptname |
±-------±----------+
| 2 | 人事部 |
| 3 | 安保部 |
| 4 | NULL |
±-------±----------+
3 rows in set (0.00 sec)

mysql> update dept set deptname = '开发喵·' + deptname;给名字添加前缀
ERROR 1292 (22007): Truncated incorrect DOUBLE value: ‘开发喵·’
mysql> desc dept;
±---------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±---------±------------±-----±----±--------±------+
| deptno | int | YES | | 1 | |
| deptname | varchar(20) | YES | | NULL | |
±---------±------------±-----±----±--------±------+
2 rows in set (0.00 sec)

mysql> update dept set deptname = concat('开发喵·',deptname);
Query OK, 6 rows affected (0.01 sec)
Rows matched: 7 Changed: 6 Warnings: 0

mysql> select * from dept;
±-------±---------------------+
| deptno | deptname |
±-------±---------------------+
| 1 | 开发喵·研发部 |
| 2 | 开发喵·人事部 |
| 3 | 开发喵·安保部 |
| 5 | 开发喵·宣传部 |
| 6 | 开发喵·组织部 |
| 7 | 开发喵·财务部 |
| 4 | NULL |
±-------±---------------------+
7 rows in set (0.00 sec)

mysql> select concat('abc',null) from dual;
±-------------------+
| concat(‘abc’,null) |
±-------------------+
| NULL |
±-------------------+
1 row in set (0.00 sec)

mysql> select now();
±--------------------+
| now() |
±--------------------+
| 2023-07-21 14:21:30 |
±--------------------+
1 row in set (0.01 sec)

mysql> select now() from dual;
±--------------------+
| now() |
±--------------------+
| 2023-07-21 14:22:01 |
±--------------------+
1 row in set (0.00 sec)

mysql> update dept set deptno = 8,deptname ='行政部' where deptno = 3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from dept;
±-------±---------------------+
| deptno | deptname |
±-------±---------------------+
| 1 | 开发喵·研发部 |
| 2 | 开发喵·人事部 |
| 8 | 行政部 |
| 5 | 开发喵·宣传部 |
| 6 | 开发喵·组织部 |
| 7 | 开发喵·财务部 |
| 4 | NULL |
±-------±---------------------+
7 rows in set (0.00 sec)

mysql> update dept set deptno = deptno + 1;
Query OK, 7 rows affected (0.01 sec)
Rows matched: 7 Changed: 7 Warnings: 0

mysql> select * from dept;
±-------±---------------------+
| deptno | deptname |
±-------±---------------------+
| 2 | 开发喵·研发部 |
| 3 | 开发喵·人事部 |
| 9 | 行政部 |
| 6 | 开发喵·宣传部 |
| 7 | 开发喵·组织部 |
| 8 | 开发喵·财务部 |
| 5 | NULL |
±-------±---------------------+
7 rows in set (0.00 sec)

mysql> delete from dept where deptno = 5;
Query OK, 1 row affected (0.01 sec)

mysql> select * from dept;
±-------±---------------------+
| deptno | deptname |
±-------±---------------------+
| 2 | 开发喵·研发部 |
| 3 | 开发喵·人事部 |
| 9 | 行政部 |
| 6 | 开发喵·宣传部 |
| 7 | 开发喵·组织部 |
| 8 | 开发喵·财务部 |
±-------±---------------------+
6 rows in set (0.00 sec)

mysql> create table test AS select * from dept;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> desc test;
±---------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±---------±------------±-----±----±--------±------+
| deptno | int | YES | | 1 | |
| deptname | varchar(20) | YES | | NULL | |
±---------±------------±-----±----±--------±------+
2 rows in set (0.00 sec)

mysql> select * from test;
±-------±---------------------+
| deptno | deptname |
±-------±---------------------+
| 2 | 开发喵·研发部 |
| 3 | 开发喵·人事部 |
| 9 | 行政部 |
| 6 | 开发喵·宣传部 |
| 7 | 开发喵·组织部 |
| 8 | 开发喵·财务部 |
±-------±---------------------+
6 rows in set (0.00 sec)

mysql> use company_info;
Database changed
mysql> desc user
-> ;
±------------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±------+
| name | varchar(20) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| age | int | YES | | NULL | |
| birth | date | YES | | NULL | |
| phone | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| create_time | timestamp | YES | | NULL | |
±------------±------------±-----±----±--------±------+
8 rows in set (0.00 sec)

mysql> insert into user(name,gender,age,phone,address,email) values ('王雪','女',22,123455678886,'西安','1234@qq.com');
Query OK, 1 row affected (0.00 sec)

mysql> select * from user;
±-------±-------±-----±------±-------------±--------±------------±------------+ | name | gender | age | birth | phone | address | email | create_time | ±-------±-------±-----±------±-------------±--------±------------±------------+ | 王雪 | 女 | 22 | NULL | 123455678886 | 西安 | 1234@qq.com | NULL |
±-------±-------±-----±------±-------------±--------±------------±------------+ 1 row in set (0.00 sec)

mysql> update user set birth = '2001-03-08' where name='王雪';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> update user set create_time = '2022-20-01 10:34:56' where name='王雪';
ERROR 1292 (22007): Incorrect datetime value: ‘2022-20-01 10:34:56’ for column ‘create_time’ at row 1
mysql> update user set create_time = '2022-10-01 10:34:56' where name='王雪';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from user;
±-------±-------±-----±-----------±-------------±--------±------------±--------------------+
| name | gender | age | birth | phone | address | email | create_time |
±-------±-------±-----±-----------±-------------±--------±------------±--------------------+
| 王雪 | 女 | 22 | 2001-03-08 | 123455678886 | 西安 | 1234@qq.com | 2022-10-0
1 10:34:56 |
±-------±-------±-----±-----------±-------------±--------±------------±--------------------+
1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值