mysql 操作sql语句 操作数据表中的内容/记录

时间:2021-10-07 11:14:12
#3. 操作文件中的内容/记录

往哪张表去插入 
insert into 表名指定字段(id,name) 插入要加values(针对前面字段插入)(2,mike); insert into t1(id,name) values(2,'mike');
insert into t1 values(1,'egon1');

也可以用逗号隔开 同时插入多条记录
insert into t1(id,name) values(1,'egon1'),(2,'egon2'),(3,'egon3');

如果默认不写 按照表字段顺序 插入
增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3');
查看这张表哪个字段
使用这条命令 前提是切换到t1的数据库,如果在另外一个数据库执行这条sql语句会报错
select id,name from t1;
查看指定数据库的数据表的哪个字段
select id,name from db1.t1;

mysql> insert into t1(id,name) values(2,'mike');
Query OK, 1 row affected (0.00 sec) mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(6) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec) mysql> select id,name from t1;
+------+------+
| id | name |
+------+------+
| 2 | mike |
+------+------+
1 row in set (0.00 sec) mysql> select id,name from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | mike |
+------+------+
1 row in set (0.00 sec)
* 代表所有字段
查:select * from t1; 
mysql> insert into t1 values(1,'jack'),(3,'ben'),(4,'peter');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from db1.t1;
+------+-------+
| id | name |
+------+-------+
| 2 | mike |
| 1 | jack |
| 3 | ben |
| 4 | peter |
+------+-------+
4 rows in set (0.00 sec)


update t1 set name='sb' ;
最好指定改哪一条记录
改:update t1 set name='sb' where id=2;
mysql> update db1.t1 set name='alex' where id =1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(6) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.06 sec) mysql>
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 2 | mike |
| 1 | alex |
| 3 | ben |
| 4 | peter |
+------+-------+
4 rows in set (0.00 sec)

把t1 表删了
delete from t1
删:delete from t1 where id=1;
mysql> delete from t1 where id=2;
Query OK, 1 row affected (0.00 sec) mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | alex |
| 3 | ben |
| 4 | peter |
+------+-------+
3 rows in set (0.00 sec)


delete from t1 把 t1表 所有记录删除掉 表结构还在