zoukankan      html  css  js  c++  java
  • 数据库简单操作

    数据库简单操作

    使用数据库

    操作

    mysql

    postgresql

    连接

    mysql -u 用户名 -p 密码 数据库名

    示例:mysql -uroot -proot mysqltest

    1.psql  登陆默认超级用户

    2.psql -h 127.0.0.1 -U 用户名 -d数据库名

    示例:psql -h 127.0.0.1 -U testdb -d testdb

    切换用户

    c - 用户名;

    切换数据库

    use 数据库名;

    c 数据库名;

    查看所有数据库

    show databases;

    l

    查看所有表

    show tables;

    d

    查看表结构

    desc 表名;

    d+ 表名;

    增///查等sql语句

    基本同mysql

    退出登陆

    quit 或者q

    q

    脚本创建表

    mysql -D samp_db -u root -p < createtable.sql

    i createtable.sql;

    #!/bin/sh

    export IP=127.0.0.1

    install_db()

    {

    psql 'dbname=postgres user=postgres password=123123' -h $IP -p 4555 << EOF

    create user testusr password '1234';

    CREATE DATABASE testdb OWNER testusr;

    alter user testusr superuser;

    GRANT ALL PRIVILEGES ON DATABASE testdb TO testusr;

    c testdb;

    i createsample.sql;

    q

    EOF

    }

    install_sampledb

    -- ----------------------------

    -- Table structure for UserAuthInfo

    -- ----------------------------

    DROP TABLE IF EXISTS UserAuthInfo;

    CREATE TABLE UserAuthInfo (

      SeqNo serial NOT NULL PRIMARY KEY,

      Name varchar(16) NOT NULL DEFAULT 0,

      AuthLevel smallint NOT NULL DEFAULT 1

    );

    create unique index Name_UserAuthInfo on UserAuthInfo (Name);

    操作MYSQL数据库

    来源:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#c6

    插入数据

    insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (1, 2, 3, ...);

    其中 [] 内的内容是可选的

    查询数据

    select 列名称 from 表名称 [查询条件];

    select * from 表名称;

    更新数据

    update 表名称 set 列名称=新值 where 更新条件;

    删除数据

    delete from 表名称 where 删除条件;

    创建后表的修改

    alter table 语句用于创建后对表的修改, 基础用法如下:

    添加列

    基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];

    示例:

    在表的最后追加列 address: alter table students add address char(60);

    在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

    修改列

    基本形式: alter table 表名 change 列名称 列新名称 新数据类型;

    示例:

    将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";

    name 列的数据类型改为 char(16): alter table students change name name char(16) not null;

    删除列

    基本形式: alter table 表名 drop 列名称;

    示例:

    删除 birthday : alter table students drop birthday;

    重命名表

    基本形式: alter table 表名 rename 新表名;

    示例:

    重命名 students 表为 workmates: alter table students rename workmates;

    删除整张表

    基本形式: drop table 表名;

    示例: 删除 workmates : drop table workmates;

    删除整个数据库

    基本形式: drop database 数据库名;

    示例: 删除 samp_db 数据库: drop database samp_db;

  • 相关阅读:
    jQuery:balloon气泡提示插件
    等高布局
    html5 语音搜索
    JS编码三种方法的区别:escape、encodeURI和encodeURIComponent
    为什么要两次调用encodeURI来解决乱码问题
    关于时间差查询的一个小技巧
    MySQL对时间的处理总结
    闭包总结:从执行环境来看闭包和垃圾回收
    闭包总结:闭包的7种形式
    JavaScript里面向对象的继承:不使用构造函数实现"继承"
  • 原文地址:https://www.cnblogs.com/sunnypoem/p/9864632.html
Copyright © 2011-2022 走看看