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,
sexchar(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,
sexchar(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;
即> 添加完字段后可以 查询表结构
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 字段名 删除一条记录
deletefrom student where score
页:
[1]