SQL语句基本语法
admin
2023-05-24 13:42:33
0

首先写入可显示中文代码

set character_set_client=gbk;
set character_set_results=gbk;

或只输一句  

set names gbk;

创建数据库

create database 库名;

查询现有数据局

show databases;

删除数据库

drop database +库名

使用库

use 库名;

创建表

create table Student(
     id int,
     name varchar(20) not null,
     age int,
     sex  char(2) not null;
     major varchar(20)
);

以学生表为例,创建主键自增表

creat table student(
     id int primary key auto_increment,//注:只有int类型且为primary key 才可以使用auto_increment.
     name varchar(20) not null,
     banji varchar(20) not null,
     banji integer default(1),  //设定默认值为1
     );

创建表后添加设定或改变默认值

例如:

alter table student modify score int;
alter table student modify score int default '1';

主键约束 

创建表时添加主键约束

creat table person(
    id int not null,
    name varchar(20) not null,
    adress varchar(50),
    primary key(id)
);

创建表后添加主键约束

alter table person add primary key(id);

外键约束

create table Score(
     p_id int,
     name varchar(20) not null,
     age int,
     sex  char(2) not null;
     major varchar(20),
     foreign key(p_id) reference persons(id)
);

创建表后添加外键约束:

alter table 表名 add foreign key (p_id) references 主表名 (id)

创建主外键关系约束

alter table score add foreign key(p_id) reference person(id);
check
banji int (banji between 1 and 3)


查询表

show tables;

在表中添加字段(就是添加各种想要的属性)(比如这里在student表中添加score)

alter table student add score double;

即 alter table 表名 add 字段 字段类型;

添加完字段后可以   查询表结构

desc Student;

即 describe 表名; 或 desc 表名;

修改表名

alter table 原表名 rename to 新表名;

修改字段名(属性名)

alter table 表名 change 原字段名  新字段名 原数据类型;
alter table student change physics physisc char(10) not null;

//注:只有int类型且为primary key 才可以使用auto_increment.

修改属性(即修改修饰这个字段的数据类型)

alter table student modify score int;


即alter table 表名 modify 字段名 数据类型;

删除一列

alter table 表名 drop 字段名

 

删除一条记录

delete  from student where score<60;
delete  from student where name=xiaohong;
delete from student where name="zhangsan" and major="yingyu";

(比如表中有两个都是zhangsan,并且没设置id为主键的话id也都是1,但是两人专业不一样,删除的时候就要加上两个条件,否则同名的都会被删除)


添加一条记录

insert into student(id,name,age,major)values(1,'张',20,80);
insert into student(id,name,age,major)values(1,"张三",20,"安卓");

(添加完可以)查看表内内容

select * from 表名  
select name from student where id=1;

多表查询

select 表名.字段名 from 表1,表2... where 表1.字段=表2.字段 and 其它查询条件;

修改某一个记录(把表上id号为1这条记录上的sex这个字段内容改为女)

update people set sex='女' where id=1

排序

select * from 表名 order by 字段名 desc;
select * from 表名 order by 字段名 asc;

修改固定词

inser into like '宋_'


相关内容

热门资讯

华为E-band微波产品斩获2... 【CNMO科技消息】 近日,华为E-band微波产品RTN 3800凭借其卓越的设计理念和技术创新,...
原创 美... 当国际空间站走到退役那一天,能不能控制它撞向中国空间站?一些美国网络社区出现这种设想,乍看像在讨论航...
华灿光电:Micro LED通... 证券之星消息,华灿光电(300323)08月04日在投资者关系平台上答复投资者关心的问题。 投资者:...
济宁的破与立:产业蝶变万亿可期... 刚刚过去的7月,济宁有两件事将机器人产业推向了新的高度。一是7月9日,珞石机器人在港交所上市,这是山...
机器人企业TOP50发布,背后... 视频制作:实习生 余天雯 8月3日,DBC德本咨询联合CIW、eNet发布《2026中国科技机器人企...
戈碧迦获得发明专利授权:“一种... 证券之星消息,根据天眼查APP数据显示戈碧迦(920438)新获得一项发明专利授权,专利名为“一种尖...
微信:即日起不再支持 8月3日,微信公众平台运营中心发布关于规范互联网渠道售卡类服务内容的公告。 微信公众平台一直致力于为...
杂活全丢给AI做,TRAE W... 一夜之间,字节、阿里、腾讯国产 AI「御三家」不约而同加码 AI 办公(Work Agent)。AI...
美国民警卫队在华盛顿特区部署延... △国民警卫队士兵在华盛顿街头巡逻(资料图)央视记者当地时间8月4日获悉,美国国防部表示,国民警卫队在...
“体感差”与“双速经济” 文/刘胜军近日,《求是》刊发题为“如何看待宏观数据与微观感受“温差””的文章,直击最近几年屡被提及的...