您的位置:首页 > 数据库 > MySQL

mysql基本语句

2012-11-08 11:45 162 查看
—— 显示所有数据库
show databases;

—— 创建数据库
CREATE database first;

—— 删除数据库
DROP database first;

——  显示 first 数据库里的所有表
use first;
SHOW tables;

—— 创建表格
CREATE TABLE Test
(
ID   Int,
price DECIMAL,
—— 指定默认值
na    varchar(30)  default 'xxx'
)

—— 修改表格 增加列
alter table test
add
(
num  int
)

—— 修改表格 修改列
alter table test
modify num varchar(30);

—— 修改表格 删除列
alter table test
drop num;

—— mysql 专用  重命名表格
alter table test
rename mytest;

—— mysql 专用 重命名列名 并 修改列属性
alter table mytest
change na nan int;

—— 删除表格
drop table mytest

—— 删除表格的所有数据
truncate mytable

—— 定义非空约束
CREATE table test
(
name varchar(30) not null,
num  INT         null
)

—— 修改约束
alter table test
modify num int  not null

—— 唯一约束 unique
CREATE table test1
(
name varchar(30) not null unique
)

CREATE table test2
(
name varchar(30) not null ,
num INT  not null,
—— 表级定义约束 unique
unique(name),
Constraint test2_uk unique(name,num)
)

—— mysql 专用 删除约束
alter table test1
drop INDEX name;

—— 主键约束
create table mytest
(
id  INT primary key
);

create  table mytest2
(
id int ,
—— 表级定义主键约束 MYSQL 不能更改主键约束名字 即名字依然为 primary
constraint a PRIMARY key(id)
);

—— 删除主键约束
alter table mytest
drop PRIMARY key;

create table mytest
(
—— 定义自动增长
id int AUTO_INCREMENT PRIMARY KEY
)

—— MYSQL 列定义外键不会生效
create table mytest3
(
id int AUTO_INCREMENT PRIMARY KEY,
fid int REFERENCES mytest2(id)
)

—— MYSQL 表级定义 外键才会生效
create table mytest4
(
id int AUTO_INCREMENT PRIMARY KEY,
fid int,
—— 删除联系的表的记录
foreign key (fid) REFERENCES mytest2(id) on delete cascade
—— on delete set null
)

—— 创建视图
create or replace view myview
as
select *
From mytest
—— 不能修改视图数据
with check option;

—— 删除视图
drop view myview

—— 添加数据
insert into a
values (null,'ddd')aa

—— 添加多行数据 mysql 专用
insert into b
values(null,2,'xxxxx'),
(null,2,'xsxsxs')

—— 修改数据
update a
set name = 'hqw'
where ida = 1

—— 删除数据
delete from b
where idb = 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: