常用sql操作如下:
1.檢視目前資料庫的所有表
show tables;
2.建立表
create table stu(sid int,sname char(20),sage int default 20 );
3.檢視表的建立資訊
show create table stu;
4.檢視表的字段資訊
desc stu;
5.添加字段
alter table stu add gender char(4);
6.删除字段
alter table stu drop gender;
7.修改某個字段的資料類型
alter table stu modify sname varchar(20);
8.修改某個字段的名和資料類型
alter table stu change sid snumber smallint;
9.查詢資料(記錄)初體驗
select * from stu;
10.插入資料(記錄)
insert into stu values(1, "tom",20);
insert into stu (snumber,sname) values(2,"jack);
insert into stu values(3,"jane",18),(4,"Tim",19),(5,"kangkang",27);
11.修改資料(記錄)
update stu set sage = 20;
12.修改滿足條件的記錄
update set stu sname = "michael" where sname = "kangkang";
13.删除資料(記錄)
delete from stu where snumber =1;
14.添加主鍵限制
<1>建表時添加:
create table tpk(id int primary key,name char(20));
<2>建表時沒添加,建表後添加<PK_id:主鍵名,一般以PK_開頭>
create table t_test(id int ,name char(20));
alter table t_test add constraint PK_id primary key(id);
16.删除主鍵限制
alter table t_test drop primary key;
17.設定自動增長限制<必須依賴主鍵存在>
create table t_test2(id int primary key auto_increment,name char(20));
18.設定非空限制
create table tnn(id int ,name char(10) not null);
19.設定預設限制
create table tdt(id int ,name char(20) default "NoName");
20.設定唯一性限制
create table tun(id int unique, name char(20));
21.添加外鍵限制=唯一性+非空
<1>建立表的時候添加
create table fClass(id int primary key ,name char(10));
create table fStudent(id int primary key auto_increment,name char(20),cid int,foreign key(cid) references fClass(id));
<2>建完表後添加
alter table tfk add constraint FK_id foreign key (id) references tpk(id);
22.删除外鍵
alter table tfk drop foreign key FK_id;
如果你和我有共同愛好,我們可以加個好友一起交流!
