mysql 操作sql語句 操作資料表中的內容/記錄
阿新 • • 發佈:2018-11-02
#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)