必背的mysql语句
必背的mysql语句
/*
set names gbk;交互字符集
1--交互字符集要和我的sql语句的编码保持一致
出现乱码问题:
mysql_connect("localhost","root","printf");
mysql_query("set names utf8");
mysql_query("insert into....");
insert into use (id, name) values(1,"小小帅");
1--数据库的编码是utf8
alter database psd1312 default character set utf8;
2--数据表的编码是utf8
alter table user deafault character set utf8;
3--表中中文字段是否为utf8
alter table user modify username char(10) default charset=utf8 not null;
*/
00001:建带有默认编码的数据库(psd1305..默认编码为:gbk)
create database psd1308 default character set gbk;
00002:先判断是否存在的建表方式(course表 id int(4) ,非空自增长)
drop table course;
create table course(id int(4) auto_increment not null);
或者
create table if not exists course(id int(4) auto_increment not null);
00003:增删改
增
增加字段(demo表 uId int)
alter table demo add uId int;
增加记录 (demo表 id ,name ,sex ,age)
insert into demo(id ,name ,sex , age)values(1 , "lanol" , "female", "18");
增加唯一索引(demo表 unique)
alter table demo modify id int unique;
增加主键(demo表 primary key)
alter table demo modify id int primary key;
alter table tb change id id int(10) not null auto_increment=1;
增加缺省(demo表 not null default 0)
alter table demo modify age int not null default 0;
增加外键(t1表 t2表 foreign key references )
alter table t1 add foreign key(typeId) references t2(typeId);
删
删除字段(demo表 删id)
alter table demo drop id;
删除记录(demo表 id=2)
delete from demo where id = 2;
删除表(demo表)
drop table demo;
删数据库(数据库psd1305)
drop database psd1308;
删除唯一索引(show demo表+id为索引名)
show index from demo; alter table demo drop index id;-->id为索引名
删除主键:--->先删除自增长再删除主键
alter table tb change id id int(10);//删除自增长
alter table tb drop primary key;//删除主建
改
改字段名(demo表中的id改为uId int)
alter table demo chang