SQL语句基本语法
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,首先写入可显示中文代码set character_set_client=gbk;set character_set_results=gbk;或只输一句set names gbk;创建数据库create
千家信息网最后更新 2025年11月07日SQL语句基本语法
首先写入可显示中文代码
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);checkbanji 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 '宋_'
字段
数据
类型
查询
属性
两个
内容
只有
数据库
条件
查询表
专业
代码
学生
就是
时候
结构
安卓
张三
中文
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
国家网络安全知识小学
怎样清除手机数据库中的记录
服务器无法正常玩游戏
活字格数据库导出
qq服务器连接不上微信文件
网络技术发展与现状收获
网络安全性web怎么取消
龙华高端网络数据库维护
网络安全第五空间站
精灵宝可梦服务器生存第一期
商丘市政府网络安全知识
数据库维护及处理技术
h1z1服务器在哪选
互联网科技公司的发展趋势
高速服务器有超时的吗
上海蓝晓网络技术有限公司
北京小川网络技术公司
mysql数据库备份与还原命令
数控系统是什么软件开发的
网络游戏软件开发报告
网络安全笔试题库(附答案)
网络安全审计联通
三级网络技术知识点app
网络安全和资金投入的关系
网络安全生产事故防控措施
温州常规网络技术哪家好
龙珠模组我的世界服务器
数据库学习一般用哪个软件
服务器租用带宽标准
软件开发工伤缴纳比例