MySQL简单语法(1)

来源:互联网 发布:asp网站sql注入 编辑:程序博客网 时间:2024/06/03 08:38

创建数据库

create database DATA;

切换数据库

use DATA;

创建表

create table t1

(
id vachar(10) not null.
name text,
number char(10) not null,
a1 double not null
)
##为列添加约束

  1. 主键约束。保证实体完整性
    create table t1
    (
    user_qq varchar(20) not null primary key,
    user_name varchar(20) not null,
    user_sex char(2) not null,
    user_birthday datetime not null
    )
  2. 外键约束。保证引用完整性
    create table scores
    (
    user_qq vachar(20) not null references ‘users’(userqq),
    gno int not null references ‘games’(gno),
    score int not null
    )
    3 检查约束作用:保证域完整性
    CREATE TABLE Games
    (
    GNO int not null CHECK(GNO>0),
    GName VARCHAR(20) not null,
    GType varchar(20) not NULL
    )
    演示发现,GNO列可以添加负数,但是不可以输入其他字符。
    4 默认约束。保证域完整性
    create table ne
    (
    usersex char(2) default ‘男’,
    usermm text not null
    )
    5 自增列
    CREATE table t02
    (
    m int not null PRIMARY key auto_increment,
    h int not null
    )
    自增列必须设置为主键

删除数据表

  1. 删除无关联的数据表
    drop table data,t11
    2 删除有关联的数据表
    当两张表有主外键关系的话,应该先删除外键,我们可以通过解除关联关系,然后再删除主键数据表。
    解除关联关系(alter 后面是从表)
    alter table biao1 drop foreign key con_name
    删除表
    drop table data,t11
原创粉丝点击