mysql基本操作

新建数据库

create database learn;

显示数据库

show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| learn              |
| mysql              |
| performance_schema |
| stock              |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

在数据库learn 下新建表test;

use learn;
create table test(id int, title char(20), uid int);

显示数据库stock 下的表

show tables;
+-----------------+
| Tables_in_learn |
+-----------------+
| test            |
+-----------------+
1 row in set (0.00 sec)

插入两条数据

insert into  test(title,uid) VALUES ('123465','1001');
insert into  test(title,uid) VALUES ('123465','1002');

显示表test一条数据;

select * from test limit 1;
+------+--------+------+
| id   | title  | uid  |
+------+--------+------+
| NULL | 123465 | 1001 |
+------+--------+------+
1 row in set (0.00 sec)

显示表test所有数据;

select * from test 
+------+--------+------+
| id   | title  | uid  |
+------+--------+------+
| NULL | 123465 | 1001 |
| NULL | 123465 | 1002 |
+------+--------+------+
2 rows in set (0.00 sec)

给表增加一个主键uid

alter table test add primary key(uid);

replace 一条数据,因为主键1003原来没有,所以增加了一条数据

REPLACE INTO test(title,uid) VALUES ('1234657','1003');
select *from test ;
+------+---------+------+
| id   | title   | uid  |
+------+---------+------+
| NULL | 123465  | 1001 |
| NULL | 123465  | 1002 |
| NULL | 1234657 | 1003 |
+------+---------+------+
3 rows in set (0.00 sec)

发现数据被更改了

REPLACE INTO test(title,uid) VALUES ('LOVE','1001');
mysql> select *from test ;
+------+---------+------+
| id   | title   | uid  |
+------+---------+------+
| NULL | LOVE    | 1001 |
| NULL | 123465  | 1002 |
| NULL | 1234657 | 1003 |
+------+---------+------+
3 rows in set (0.00 sec)

删除表

 drop table test;
Query OK, 0 rows affected (0.01 sec)
posted @ 2022-08-19 22:53  luoganttcc  阅读(12)  评论(0)    收藏  举报