#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表 全部記錄刪除掉 表結構還在