刪除表
命令:drop table <表名>
例如:刪除表名爲 MyClass 的表
mysql> drop table MyClass;
插入數據
命令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
查詢表中的數據
查詢全部行
mysql> select * from MyClass;
查詢前幾行數據
例如:查看錶 MyClass 中前 2 行數據
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;
刪除表中數據
命令:delete from 表名 where 表達式
例如:刪除表 MyClass 中編號爲 1 的記錄
mysql> delete from MyClass where id=1;
修改表中數據
命令:update 表名 set 字段=新值,... where 條件
mysql> update MyClass set name='Mary' where id=1;
在表中增長字段
命令:alter table 表名 add 字段 類型 其餘;
例如:在表 MyClass 中添加了一個字段 passtest,類型爲 int(4),默認值爲 0
mysql> alter table MyClass add passtest int(4) default '0'
更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改成 YouClass
mysql> rename table MyClass to YouClass;
更新字段內容
命令:update 表名 set 字段名 = 新內容
update 表名 set 字段名 = replace(字段名, '舊內容', '新內容');
例如:文章前面加入 4 個空格
update article set content=concat(' ', content);mysql
【修改表的語法】sql
一張表,建立完畢,有了N列.ui
以後還有可能要增長或刪除或修改列 spa
Alter table 表名 add 列名稱 列類型 列參數; [加的列在表的最後]it
例: alter table m1 add birth date not null default '0000-00-00';table
Alter table 表名 add 列名稱 列類型 列參數 after 某列 [把新列加在某列後]test
例: alter table m1 add gender char(1) not null default '' after username;date
Alter table 表名 add 列名稱 列類型 列參數 first [把新列加在最前面]select
例: alter table m1 add pid int not null default 0 first; 語法
刪除列:
Alter table 表名 drop 列名
修改列類型:
Alter table 表名 modify 列名 新類型 新參數
例:alter table m1 modify gender char(4) not null default '';
修改列名及列類型
Alter table 表名 change 舊列名 新列名 新類型 新參數
例:alter table m1 change id uid int unsigned;