How to create a table in PHPMyAdmin? - sql

How do I (or rather, what do I write) a SQL CREATE TABLE command to create a table for Term as defined in the picture to create a table?
I am completely new to mhphpadmin (sql) and am learning this from scratch.

The general syntax is
Create Table Table_name (Column_name1 DataType, Column_name2 DataType,...)
You can create table as
CREATE TABLE Term (Term_Name VARCHAR(25), Term_StartDate DATE, TermEndDate DATE);

Related

Create temporary table in Oracle

I'm trying to create temporary table in PL/SQL developer and insert some data, but it throws error:
ORA-00905
My code:
CREATE PRIVATE TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
CREATE PRIVATE TEMPORARY TABLE introduced only in Oracle 18:
Oracle 18c added private temporary tables, which are single-session in-memory objects.
In previous version, you can create global temporary table:
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);

creating schema with a table containing serial field

How do I create (in a single SQL command) a schema and a table in it, but with the table containing a serial column (in Postgres) ?
For example, here I am attempting to create schema zoo with table animals from type animal_t with serial column animal_id:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq')
CREATE SEQUENCE animals_animal_id_seq OWNED by animals.animal_id
;
Notes:
CREATE SCHEMA only accepts CREATE TABLE or CREATE SEQUENCE, it does not accept ALTER , this is why I have to do all of this in a single SQL sentence.
Result:
-bash-4.3$ psql dev < animal.sql
DROP TYPE
CREATE TYPE
ERROR: syntax error at or near "animal_id"
LINE 3: animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_a...
^
-bash-4.3$
You have two options
simplify your statement and get rid of the object type
CREATE SCHEMA zoo
CREATE table animal
(
animal_id serial,
animal_name varchar
);
use a search path if you want to avoid to prefix the table with a schema name:
DROP TYPE IF EXISTS animal_t CASCADE;
CREATE TYPE animal_t AS (
animal_id integer,
animal_name varchar
);
CREATE SCHEMA zoo;
set search_path = zoo;
CREATE SEQUENCE animals_animal_id_seq;
CREATE TABLE animals OF animal_t
animal_id WITH OPTIONS NOT NULL DEFAULT nextval('animals_animal_id_seq');
alter sequence animals_animal_id_seq owned by animals.animal_id;
After creating the type and the schema, the current schema is set to the just created one, so all subsequent statements use zoo as the default schema.
Note that you can do this in a single transaction if that is another reason for you to use the "extended" create schema syntax.
I think the following should do it. The idea is to ALTER schema_name.thing:
CREATE SCHEMA zoo
CREATE TABLE animals OF animal_t ( animal_id WITH OPTIONS NOT NULL )
;
CREATE SEQUENCE zoo.animals_animal_id_seq OWNED BY zoo.animals.animal_id;
ALTER TABLE zoo.animals ALTER animal_id SET DEFAULT nextval('zoo.animals_animal_id_seq');

Defining a schema inside a procedure

I'm working on creating a model data base design for a retail store. I'm trying to create a single procedure which will initialize the database schema.
What I'm trying to achieve is to create a new schema from inside the procedure. My code is as follows:
begin trans
create procedure Retail_Fill
as
create schema Retail_Test;
go
create table Retail_Test.customer(
cust_id int,
cust_name varchar(30),
cust_phone int,
cust_add varchar(50),
constraint pk_customer primary key (cust_id)
);
Here the create schema statement works fine by itself. But inside the procedure it gives an error:
Invaid Syntax!CREATE SCHEMA must be the only statement in the batch
I want to know if it is at all possible to achieve this. If yes then what am I doing wrong or where is the error?
CREATE SCHEMA has to be executed as a separate batch. Batches in SQL Server Management Studio is separated by GO. That is not the case in a stored procedure. You can do what you want by using EXECUTE for the statements that needs to be in a batch of its own like CREATE SCHEMA or CREATE PROCEDURE.
create procedure Retail_Fill
as
exec('create schema Retail_Test');
create table Retail_Test.customer(
cust_id int,
cust_name varchar(30),
cust_phone int,
cust_add varchar(50),
constraint pk_customer primary key (cust_id)
);
As Mikael Eriksson said above creating DB Schema should be in a single script file or may be you can configure some deployment that will create your schema. So it will be good to avoid creating schema within procedures.
You can have these inside procedure when you have to design the schema on fly for each customer (for example).

Create a temporary table in SQL on the fly

How can I create a temporary table without first creating the columns?
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )
select name
from tempdb..sysobjects
where name like '#yak%'
drop table #yaks
It is a pain to have to define the table first.
Create a (temp) table with the same columns as another (no data copied):
select * into #TempTable
from MyTable
where 1=0
Note: Does not create any Foreign keys, indexes etc...

Syntax for Alter Column and Add Column in same query

Is it possible to both alter a column and add a new column in the same alter table query for MSQL? I've tried looking at the below MSDN article, but it was a bit confusing to understand. I could easily do it with multiple queries, but would rather do it with one query if possible. Thanks.
http://msdn.microsoft.com/en-us/library/ms190273.aspx
no you need to do it in separate batches
create table test(id int)
go
alter table Test add id2 int
go
alter table Test ALTER COLUMN id DECIMAL (5, 2)
you can however add more than 1 column at a time
alter table Test add id3 int, id4 int
go